Eryk Sun <eryk...@gmail.com> added the comment:

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()

----------
components: +Windows
nosy: +eryksun, paul.moore, steve.dower, tim.golden, zach.ware

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue41021>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to