我是一名C ++初学者(3-4个月),我正试图依靠Windows挂钩。我有一个DLL的错误,我正在尝试注入,一段时间后,我意识到我的DllMain没有被调用!我查看了StackOverflow上的几乎所有线程,无法弄清楚我的问题。我发现通过将变量初始化为5,在DllMain中将其更改为1并在函数中输出变量。变量永远不会改变。这是代码:
int i = 5;
BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved )
{
i=1;
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
hDll = (HINSTANCE) hModule;
break;
case DLL_THREAD_ATTACH: break;
case DLL_THREAD_DETACH: break;
case DLL_PROCESS_DETACH: break;
}
return TRUE;
}
bool InstallHook(){
cout << "INSTALLING HOOK... " << endl;
cout << i << endl;
hHook = SetWindowsHookEx(WH_CBT, (HOOKPROC) CBTProc, hDll, 0);
return hHook != NULL;
}
这是我加载DLL ...
typedef bool (*InstallHook)();
typedef void (*UninstallHook)();
InstallHook ih;
UninstallHook uh;
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
uh();
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
// Bunch of code to initialize a simple window until this:
HINSTANCE hDll = LoadLibrary("e:\\projects\\DLL\\ToInject.dll");
ih = (InstallHook)GetProcAddress(hDll, "InstallHook");
uh = (UninstallHook)GetProcAddress(hDll, "UninstallHook");
if (!ih()){
cout << "SUCCESS" << endl;
}else{
cout << "FAILED" << endl;
}
// other stuff to create a window
return Msg.wParam;
}
输出:
INSTALLING HOOK...
5 // We can see here that the DLL never changed the value of i to 1.
SUCCESS
UNINSTALL HOOK...
答案 0 :(得分:6)
按照以下步骤制作一个工作示例(这是针对visual studio):
的 dllmain.cpp 强> 的
// dllmain.cpp : Defines the entry point for the DLL application.
#include "stdafx.h"
#include <string>
std::string test = "not Loaded";
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
test = "loaded"; //You also change on this location the value of a variable
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
MessageBoxA(NULL,"test","test",NULL);
break;
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
extern "C" __declspec (dllexport) bool example()
{
MessageBoxA(NULL,test.c_str(),"test",NULL);
return true;
}
的 Main.cpp的强> 的
#include <windows.h>
typedef bool (*testFunction)();
testFunction dllFunction;
int main()
{
HINSTANCE hDll = LoadLibraryA("example.dll");
if(hDll)
{
dllFunction = (testFunction)GetProcAddress(hDll, "example");
dllFunction();
}
return 0;
}
它在这里工作。我得到一个带有测试的MessageBox和一次加载。您可以将此代码与您的代码进行比较。我希望你能找到问题。
答案 1 :(得分:2)
我在同一个程序中看到WinMain
和cout
。
请解释您已创建的项目类型。
你可以看看这里的钩子示例 - http://www.codeproject.com/Articles/5002/Mousey-Roll-Over-and-Park
答案 2 :(得分:2)
// Create consts for firewall types.
const int NET_FW_PROFILE2_DOMAIN = 1;
const int NET_FW_PROFILE2_PRIVATE = 2;
const int NET_FW_PROFILE2_PUBLIC = 4;
// Create the firewall type.
Type FWManagerType = Type.GetTypeFromProgID("HNetCfg.FwPolicy2");
// Use the firewall type to create a firewall manager object.
dynamic FWManager = Activator.CreateInstance(FWManagerType);
// Get the firewall settings.
bool CheckDomain =
FWManager.FirewallEnabled(NET_FW_PROFILE2_DOMAIN);
bool CheckPrivate =
FWManager.FirewallEnabled(NET_FW_PROFILE2_PRIVATE);
bool CheckPublic =
FWManager.FirewallEnabled(NET_FW_PROFILE2_PUBLIC);
如果您使用C ++编译它,则必须添加&#34; extern&#34; C&#34; &#34;在DllMain功能中