I have C++ code which has a function which takes, three arguments
- Pointer argument
- LPWSTR argument
- (LPWSTR variable)reference as argument Below is the C++ Code syntax
HRESULT WINAPIV GIDItem (Address* pa, LPWSTR cmdvalue, LPWSTR* string_value)
I am able to load the dll in Ctypes
import ctypes
uDl = ctypes.CDLL(r"test1")
uDl_function=uDl.CAIA # we are creating function pointer
pa=uDl_function('something')
uD = ctypes.CDLL(r"test.dll")
uD_function =uD.GIDItem # we are creating function pointer
string_value= ctypes.c_wchar_p()
cmdvalue=ctypes.c_wchar_p("CM")
dI=uD_function(ctypes.POINTER(pa),cmdvalue,ctypes.byref(string_value))
I am getting below error,
dI=uD_function(ctypes.POINTER(pa),cmdvalue,ctypes.byref(string_value))
TypeError: must be a ctypes type
I was just looking some article about that DLL, in C++ the Dll been called like below
fpGIDItem (pA, L"CMD", &cD)
When you look to the above code "CMD" is the cmdvalue and string value is sent with &cD
Please help me over this
1 Answer 1
ctypes.POINTER(pa) is a type instead of an instance which is why it is giving the error you see.
If you create a working example of the use in C, it would be easier to identify how to write it in Python, but I'll take a stab at it.
Set .argtypes and restype on the function correctly so ctypes will type check for you.
Something like the following should work.
from ctypes import *
dll = CDLL(r'test1')
dll.CAIA.argtypes = c_wchar_p,
dll.CAIA.restype = c_void_p # generic void* should work.
dll2 = ctypes.CDLL(r'test.dll')
dll2.GIDItem.argtypes = c_void_p,c_wchar_p,POINTER(c_wchar_p)
dll2.GIDItem.restype = c_long
pa = dll.CAIA('something')
string_value = c_wchar_p()
result = dll2.GIDItem(pa,'CM',byref(string_value))
pais declared. Am I missing something?pa.Thanks for looking to thisPOINTERis a type and you need an instance of a type. Since the first parameter isAddress*is can probably be justpaif that is what is returned from the first function.byref(string_value)is actually passing awchar_t**which isn't what you want either. Just passstring_value. Update your question with the full C prototypes of both functions if you want a better answer.