成功注入到了大部分进程中,但是入口函数好像没有执行.第一个代码是程序,第二个代码是DLL
源码
#include
#include
int main(int argc, PCHAR argv[]){
HHOOK kbhook;
HMODULE mydll = LoadLibrary("mydll.dll");
HMODULE myproc = GetProcAddress(mydll, "myproc");
kbhook = SetWindowsHookEx(WH_KEYBOARD, myproc, mydll, 0);
if (kbhook == NULL){
printf("SetWindowsHookEx failed %d\n", GetLastError());
} else
{
printf("执行SetWindowsHookEx完成\n");
}
//消息循环
MSG msg;
while (GetMessage(&msg, NULL, 0, 0)){
TranslateMessage(&msg);
DispatchMessage(&msg);
};
UnhookWindowsHookEx(kbhook);
getchar();
return 0;
}
源码
#include
#include
HWND hwnd = NULL;
DWORD dwPid = 0;
BOOL APIENTRY DLLMain(HMODULE hModule, DWORD fdwReason, LPVOID lpvReserved){
switch (fdwReason){
case DLL_PROCESS_ATTACH://进程加载DLL
break;
case DLL_THREAD_ATTACH://线程加载DLL
break;
case DLL_THREAD_DETACH://线程卸载DLL
break;
case DLL_PROCESS_DETACH://进程卸载DLL
break;
}
return TRUE;
}
__declspec(dllexport) int APIENTRY add(int a, int b){
return a + b;
}
LRESULT WINAPI myproc(int code, WPARAM w, LPARAM l){
//只是使用钩子注入DLL到进程
if (hwnd){
PostMessage(hwnd, code, w, l);
} else{
HMODULE hMod = GetModuleHandle(TEXT("mydll.dll"));
if (hMod != NULL)
{
TCHAR szBuffer[MAX_PATH] = { 0 };
GetModuleFileName(hMod, szBuffer, sizeof(szBuffer) / sizeof(TCHAR)-1);
MessageBox(NULL, szBuffer, "消息2", MB_OK);
}
hwnd = FindWindow(NULL, TEXT("D:\\学习代码\\精通 windows api\\hookhelloworld\\Release\\hookhelloworld.exe"));
PostThreadMessage(hwnd, code, w, l);
if (hwnd)
MessageBox(NULL, "获取句柄成功", "消息2", MB_OK);
else
MessageBox(NULL, "获取句柄失败", "消息2", MB_OK);
}
return 1;// CallNextHookEx(NULL, code, w, l);
}