pleiszenburg / zugbruecke

Calling routines in Windows DLLs from Python scripts running under Linux, MacOS or BSD
https://zugbruecke.readthedocs.io/en/latest/
GNU Lesser General Public License v2.1
108 stars 11 forks source link

How to get function? #71

Closed hanusek closed 4 years ago

hanusek commented 4 years ago

Hello. How to bind function?

import zugbruecke
from zugbruecke import *
from zugbruecke.wintypes import *
from zugbruecke.core import *

dll_path = 'my_path_foo_bar.dll'
dll = zugbruecke.windll.LoadLibrary(dll_path)
connect = dll.K5DB_Connect
connect.restype = zugbruecke.c_bool
connect.argtypes = ...
DWORD __declspec(dllexport) K5DB_Connect (      
    HWND hwndCallback,                  // handle of the parent window for callbacks
    DWORD msgCallback,                  // wished callback message
    DWORD dwFlags,                      // 1 if self-notifications wished
    DWORD dwData,                       // reserved for extension
    LPCSTR szClientName                 // client name
    ); // RETURN: client ID or NULL if error
s-m-e commented 4 years ago

First of all, do not use wildcard imports in Python. There are legitimate edge cases where they make sense - but this is not.

Second, there is no need to import anything from zugbruecke.core. The core contains intentionally undocumented internal APIs.

Your code should approximately look like this:

import zugbruecke as ctypes
from zugbruecke.wintypes import (
    HWND, # ctypes.c_void_p
    DWORD, # ctypes.c_ulong
    LPCSTR, # ctypes.c_char_p
)

dll_path = 'my_path_foo_bar.dll'
dll = ctypes.cdll.LoadLibrary(dll_path) # very likely `cdll` because of `__declspec`
connect = dll.K5DB_Connect
connect.argtypes = (
    HWND, # handle of the parent window for callbacks
    DWORD, # wished callback message
    DWORD, # 1 if self-notifications wished
    DWORD, # reserved for extension
    LPCSTR, # client name
)
connect.restype = DWORD
hanusek commented 4 years ago

Works. Thanks.