2014-10-04 6 views
2

dll-injection으로 메모장에 연결하려고합니다. exe가 실행되고 메모장 (내가 성공적으로 말할 수있는 양식)과 일부 키를 누른 후, 키 누름이 반복문이나 대기열 (메모장이 응답하지 않음)에 달라 붙는 것처럼 보입니다. exe unhooks 후 메모장 응답하고 모든 키를 텍스트 필드에 나타납니다.루프/대기열에 WH_KEYBOARD가 고정 된 SetWindowsHookEx가

EXE

#include <iostream> 
#include <fstream> 
#include <windows.h> 
#include <stdio.h> 

HHOOK  hHook  = NULL; 
HWND  handle = NULL; 
HMODULE  dll  = NULL; 
HOOKPROC address = NULL; 
DWORD  thread_id = 0; 

using namespace std; 

int main(){ 

    handle=FindWindow(NULL,L"Untitled - Notepad"); 
    if(handle==NULL){ 
     cout<<"Window not found"<<endl; 
     getchar(); 
     return 0; 
    } 

    thread_id=GetWindowThreadProcessId(handle,NULL); 
    if(thread_id==0){ 
     cout<<"ID not found"<<endl; 
     getchar(); 
     return 0; 
    } 

    dll = LoadLibrary(TEXT("X:\\qt\\hook\\debug\\hook.dll")); 
    if(dll==NULL){ 
     cout<<"hook.dll not found"<<endl; 
     getchar(); 
     return 0; 
    } 

    address=(HOOKPROC)GetProcAddress(dll,"[email protected]"); 
    if(address==NULL){ 
     cout<<"Address not found"<<endl; 
     getchar(); 
     return 0; 
    } 

    hHook=SetWindowsHookEx(WH_KEYBOARD,address,dll,thread_id); 
    if(hHook==NULL){ 
     cout<<"hook was not set"<<endl; 
     return 0; 
    } 

    cout<<"Program successfully hooked"<<endl; 
    cout<<"Press enter to unhook the function and stop the program"<<endl; 
    getchar(); 
    UnhookWindowsHookEx(hHook); 

    return 0; 
} 

SetWindowsHookEx와 UnhookWindowsHookEx 간의 메시지 루프 추가 DLL은

#include "hook.h" 
#include <windows.h> 
#include <iostream> 
#include <fstream> 

using namespace std; 

extern "C"{ 
    __declspec(dllexport) LRESULT CALLBACK CallWndProc(int nCode,WPARAM wParam,LPARAM lParam){ 

     if(nCode<0){ 
      return CallNextHookEx(NULL,nCode,wParam,lParam); 
     } 

     ofstream file; 
     file.open("X:\\qt\\klog\\debug\\function.txt"); 
     file<<"Function keyboard_hook called\n"; 
     file.close(); 
     return CallNextHookEx(NULL,nCode,wParam,lParam); 
    } 
} 

BOOL APIENTRY DllMain(HMODULE hDLL, DWORD Reason, LPVOID Reserved){ 

    switch(Reason) { 
    case DLL_PROCESS_ATTACH: break; 
    case DLL_PROCESS_DETACH: break; 
    case DLL_THREAD_ATTACH: break; 
    case DLL_THREAD_DETACH: break; 
    } 

    return TRUE; 
} 
+0

를 고정 : //msdn.mi crosoft.com/en-us/library/windows/desktop/ms644984(v=vs.85).aspx) page :'이 훅은 그것을 설치 한 쓰레드의 문맥에서 호출 될 수 있습니다. 후크를 인스톨 한 thread에 메세지를 송신 해 호출합니다. 따라서 후크를 설치 한 스레드는 메시지 루프를 가져야합니다. ' –

+0

고마워요 @ 500 내가 어떻게 그걸 놓쳤는 지 모르겠습니다. – DevilBinder

답변

1

에서 [KeyboardProc 콜백 함수 (HTTP 가입일

while(GetMessage(&Msg, NULL, 0, 0) > 0) 
{ 
    TranslateMessage(&Msg); 
    DispatchMessage(&Msg); 
}