Note that there are some explanatory texts on larger screens.

plurals
  1. POKeyboardHookProc in DLL doesn't do anything when called from python
    text
    copied!<p>I've been trying to write a DLL in C.</p> <p>Install hook sets up the KeyboardProc. Calling the <code>InstallHook()</code> and <code>UninstallHook()</code> functions from Python always returns a 0, which I guess is because my callback function <code>KeyboardProc</code> isn't working.</p> <p>The following is my C code for the DLL:</p> <pre><code>#include "stdafx.h" #include &lt;windows.h&gt; #include &lt;string.h&gt; #include &lt;ctype.h&gt; #include &lt;stdio.h&gt; #include "ourdll.h" //#pragma comment(linker, "/SECTION:.SHARED,RWS") //#pragma data_seg(".SHARED") HHOOK hKeyboardHook = 0; int keypresses = 0; HMODULE hInstance = 0; //#pragma data_seg() BOOL WINAPI DllMain (HANDLE hModule, DWORD dwFunction, LPVOID lpNot) { hInstance = hModule; //Edit return TRUE; } LRESULT CALLBACK KeyboardProc(int hookCode, WPARAM vKeyCode, LPARAM flags) { if(hookCode &lt; 0) { return CallNextHookEx(hKeyboardHook, hookCode, vKeyCode, flags); } keypresses++;; return CallNextHookEx(hKeyboardHook, hookCode, vKeyCode, flags); } __declspec(dllexport) void InstallHook(void) { hKeyboardHook = SetWindowsHookEx(WH_KEYBOARD, KeyboardProc, hInstance, 0); } __declspec(dllexport) int UninstallHook(void) { UnhookWindowsHookEx(hKeyboardHook); hKeyboardHook = 0; return keypresses; } </code></pre> <p>The Python code to use this is as follows:</p> <pre><code>&gt;&gt;&gt; from ctypes import * &gt;&gt;&gt; dll = CDLL('C:\...\OurDLL.dll') &gt;&gt;&gt; dll.InstallHook() </code></pre> <p>[Type something at this point]</p> <pre><code>&gt;&gt;&gt; result = dll.UninstallHook() &gt;&gt;&gt; result 0 </code></pre> <p>EDIT: I should probably mention that I've also tried out a <code>LowLevelKeyboardHook</code>. I understand that the LowLevel hook is global and will catch all keystrokes, but that just caused my <code>dll.InstallHook()</code> Python code to freeze for a second or two before returning zero.</p> <p>I am no expert in C, so any help would be greatly appreciated. Thanks.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload