1

I have C++ code which has a function which takes, three arguments

  1. Pointer argument
  2. LPWSTR argument
  3. (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

12
  • I can't tell where pa is declared. Am I missing something? Commented Apr 23, 2020 at 18:03
  • I just updated the code for pa.Thanks for looking to this Commented Apr 23, 2020 at 18:08
  • You probably need to include a minimal working example. In the meantime see if this helps. Commented Apr 23, 2020 at 18:19
  • I don't have c++ dll code, i just know the input type, function name, and return Commented Apr 23, 2020 at 18:21
  • 2
    POINTER is a type and you need an instance of a type. Since the first parameter is Address* is can probably be just pa if that is what is returned from the first function. byref(string_value) is actually passing a wchar_t** which isn't what you want either. Just pass string_value. Update your question with the full C prototypes of both functions if you want a better answer. Commented Apr 25, 2020 at 0:19

1 Answer 1

2

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))
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, a lot Mark! It worked well and fine.. It's getting the expected result. But I have the only thing if you how to enable the logs so that that I can log any error when we have any error when we called function from ctypes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.