2011-08-28 3 views
0

안녕하세요. 누구나 도와 주려고 미리 감사드립니다. 나는 CBT 윈도우 훅을 설정하려고하는데, Im은 전역 적으로 설정할 때 잘 작동하지만, 하나의 스레드에 연결할 때마다 실패합니다. 내가 아는 한, 임은 책에서 모든 것을하고있다. - 관리되지 않는 DLL에서 후크 프로 시저를 노출했다. - 내 응용 프로그램, dll 및 스레드 프로세스는 모두 32 비트이다. - 사용하는 스레드 ID가 정확하다 spy ++ 사용)SetWindowsHookEx가 스레드 ID와 함께 작동하지 않습니다.

C++ 코드에서 단 하나의 스레드를 연결하려고하면 관리 할 수 ​​있습니다 ... 관리되지 않는 코드에서만 단일 스레드를 연결할 수 있습니까?

[DllImport("user32.dll", SetLastError = true)] 
    static extern IntPtr SetWindowsHookEx (int hookType, UIntPtr lpfn, IntPtr hMod, uint dwThreadId); 

[DllImport("kernel32", CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)] 
    public static extern UIntPtr GetProcAddress (IntPtr hModule, string procName); 

[DllImport("kernel32", SetLastError = true, CharSet = CharSet.Unicode)] 
    public static extern IntPtr LoadLibrary (string libraryName); 

const int WH_CBT = 5; 

    void SetHook() 
    { 
     IntPtr dll = LoadLibrary(LIBRARY); 
     UIntPtr proc = GetProcAddress(dll, PROC); 
     uint threadId = GetAppWindowThreadId(); 
     //assume that the threadId of the external window is correct, as I said I verified with spy++ 
     //and assume that dll and proc both get correct values 
     IntPtr hookAddress = SetWindowsHookEx(WH_CBT , proc, dll, threadId); 
     //hookAddress is 0 
    } 
+1

Marshal.GetLastWin32Error는 무슨 말을해야합니까? –

+0

nothing ... (의미 0) – Orran

답변

3
[DllImport("user32.dll", SetLastError = true)] 
static extern IntPtr SetWindowsHookEx (int hookType, UIntPtr lpfn, 
             IntPtr hMod, ulong dwThreadId); 

그 선언은 잘못이다 :

여기 어쨌든 내 코드입니다. 마지막 인수의 유형 (dwThreadId)은 C#의 uint 인 DWORD입니다.

이상하게도 PInvokeStackImbalance 디버거를 얻지 못했습니다. 이것은 사용자가 64 비트 운영 체제에서 실행 중임을 나타냅니다. 추가로 몇 가지 추가 실패 모드를 추가합니다. 삽입하는 DLL은 프로세스와 프로세스의 정확한 비트로 컴파일되지 않은 코드를 포함해야합니다. 필요에 따라 프로젝트의 플랫폼 대상을 설정하십시오. 코드에서 모든 오류 검사가 누락되어 작동하지 않는 이유를 알 수 없으므로 오류 반환 코드를받을 때 새로운 Win32Exception()을 throw해야합니다.

+0

처음에는 ulong 대신에 uint가 사용되었고, uint와도 제대로 작동하지 않았습니다. – Orran

+0

나머지 답변을 꼭 읽으십시오. –

+0

Marshal.GetLastWin32Error returns 0 ... – Orran

관련 문제