Message371844
Please provide complete code that can be compiled as is, and the required build environment. I wrote a similar example in C, compiled for x64 with MSVC, and it worked fine for me. Also, please provide more details about the error -- a traceback in a native debugger, exception analysis, or a dump file.
One detail that stands out to me is that you set the ctypes callback return type to c_void_p -- instead of void (None). But that shouldn't cause a crash. Another concern is your use of WINFUNCTYPE (stdcall) and WinDLL instead of CFUNCTYPE (cdecl) and CDLL. In x64 they're the same, but it's still important to use the function's declared calling convention, for the sake of x86 compatibility. Make sure you're really using stdcall.
Here's the code that worked for me:
c/test.c:
#include <windows.h>
typedef void listen_fn(int, FILETIME);
static void
subscribe_thread(listen_fn *cb)
{
int i = 0;
FILETIME systime;
while (1) {
GetSystemTimeAsFileTime(&systime);
cb(i++, systime);
Sleep(1000);
}
}
void __declspec(dllexport)
subscribe(listen_fn *cb)
{
CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)subscribe_thread,
cb, 0, NULL);
}
test.py:
import ctypes
from ctypes import wintypes
lib = ctypes.CDLL('c/test.dll')
listen_fn = ctypes.CFUNCTYPE(None, ctypes.c_int, wintypes.FILETIME)
@listen_fn
def cb(val, ft):
print(f"callback {val:03d} {ft.dwHighDateTime} {ft.dwLowDateTime}")
print('press enter to quit')
lib.subscribe(cb)
input() |
|
Date |
User |
Action |
Args |
2020-06-19 00:28:48 | eryksun | set | recipients:
+ eryksun, paul.moore, amaury.forgeotdarc, belopolsky, tim.golden, meador.inge, zach.ware, steve.dower, itsgk92 |
2020-06-19 00:28:48 | eryksun | set | messageid: <1592526528.49.0.297608781504.issue41021@roundup.psfhosted.org> |
2020-06-19 00:28:48 | eryksun | link | issue41021 messages |
2020-06-19 00:28:48 | eryksun | create | |
|