-1
我正在使用Detours來截取對Windows庫函數waveInOpen的調用。在這個函數中,你可以選擇傳遞一個回調函數,當一個聲音緩衝區被填滿時,這個回調函數被調用。用自定義替換回調函數,然後調用原始回調函數
我想用自己的回調函數替換程序傳遞給waveInOpen的回調函數。當一個緩衝區被填充並且我的自定義回調函數被調用時,我想在之後調用原始函數。
但是,我有問題,我掛鉤的程序崩潰,每當我調用原始回調函數。
爲什麼會發生這種情況,並且有沒有辦法在不崩潰程序的情況下調用原始回調函數?
這裏是DLL的樣子:
#include <windows.h>
...
MMRESULT (WINAPI* Real_waveInOpen)(LPHWAVEIN phwi, UINT uDeviceID, LPCWAVEFORMATEX pwfx, DWORD_PTR dwCallback, DWORD_PTR dwCallbackInstance, DWORD fdwOpen) = waveInOpen;
MMRESULT WINAPI Mine_waveInOpen(LPHWAVEIN phwi, UINT uDeviceID, LPCWAVEFORMATEX pwfx, DWORD_PTR dwCallback, DWORD_PTR dwCallbackInstance, DWORD fdwOpen);
INT APIENTRY DllMain(HMODULE hDLL, DWORD Reason, LPVOID Reserved) {
switch(Reason) {
case DLL_PROCESS_ATTACH:
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourAttach(&(PVOID&)Real_waveInOpen, Mine_waveInOpen);
DetourAttach(&(PVOID&)Real_waveInAddBuffer, Mine_waveInAddBuffer);
DetourTransactionCommit();
break;
case DLL_PROCESS_DETACH:
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourDetach(&(PVOID&)Real_waveInOpen, Mine_waveInOpen);
DetourDetach(&(PVOID&)Real_waveInAddBuffer, Mine_waveInAddBuffer);
DetourTransactionCommit();
break;
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
}
return TRUE;
}
// this is a pointer to the callback function the original program is passing to waveInOpen
void (CALLBACK* RealwaveInProc)(HWAVEIN hwi, UINT uMsg, DWORD_PTR dwInstance, DWORD_PTR dwParam1, DWORD_PTR dwParam2);
// this is my custom callback function
void CALLBACK MywaveInProc(HWAVEIN hwi, UINT uMsg, DWORD_PTR dwInstance, DWORD_PTR dwParam1, DWORD_PTR dwParam2) {
RealwaveInProc(hwi, uMsg, dwInstance, dwParam1, dwParam2);
}
// this is the windows-library-function I am intercepting
MMRESULT WINAPI Mine_waveInOpen(LPHWAVEIN phwi, UINT uDeviceID, LPCWAVEFORMATEX pwfx, DWORD_PTR dwCallback, DWORD_PTR dwCallbackInstance, DWORD fdwOpen) {
RealwaveInProc = reinterpret_cast<void (CALLBACK*)(HWAVEIN,UINT,DWORD_PTR,DWORD_PTR,DWORD_PTR)>(dwCallback);
MMRESULT r = Real_waveInOpen(phwi, uDeviceID, pwfx, (DWORD_PTR)MywaveInProc, dwCallbackInstance, CALLBACK_FUNCTION);
return r;
}
extern "C" __declspec(dllexport) int meconnect(int code, WPARAM wParam, LPARAM lParam) {
return CallNextHookEx(NULL, code, wParam, lParam);
}
你確定有原始的回調函數嗎?你不檢查它是否爲空。 –
感謝您的提示!原來它真的是空的。我不明白爲什麼它爲空。怎麼可能? – moccajoghurt
您可以通過閱讀文檔的簡單方法找出該問題的答案。請記住,如果您在所有進程中掛接了系統API,則需要支持其全部功能。你嘗試的東西非常脆弱,我懷疑你會成功地使它穩定。你也應該知道你的代碼不是線程安全的。 –