0

I have a known function type definition of wintypes.HANDLE, wintypes.LPVOID with a return value of wintypes.DWORD.

Using ctypes I've defined the type and function and tried to make the call with a handle and lpvoid reference:

ftype = CFUNCTYPE(wintypes.HANDLE, wintypes.LPVOID, wintypes.DWORD)
function = ftype(address)

base = wintypes.LPVOID(0x0)
ptr = function(GetCurrentProcess(), byref(base))

However, when executing I receive an error: ctypes.ArgumentError: argument 2: <class 'TypeError'>: wrong type

Can someone please help me identify the problem?

What type should I be passing, if not the defined lpvoid?

8
  • Well, a byref(base) would be void **. Wouldn't you just pass base? Commented Aug 1, 2021 at 3:16
  • Tried that too, same error message argument 2: <class 'TypeError'>: wrong type Commented Aug 1, 2021 at 4:03
  • There are three arguments, you're only supplying 2. Commented Aug 1, 2021 at 4:58
  • Last argument is the return type Commented Aug 1, 2021 at 5:04
  • No, the FIRST argument is the return type. Still, I admit i was wrong. Commented Aug 1, 2021 at 5:06

1 Answer 1

2

Here's a minimal example. I created a DLL with the function signature described and extracted the address to demo the Python code.

#include <windows.h>
#include <stdio.h>

#ifdef _WIN32
#   define API __declspec(dllexport)
#else
#   define API
#endif

API DWORD function(HANDLE h, LPVOID pv) {
    printf("h=%p pv=%p\n", h, pv);
    return 1;
}
from ctypes import *
from ctypes import wintypes

dll = CDLL('./test')
address = addressof(dll.function)

# return value is the FIRST parameter to CFUNCTYPE
ftype = CFUNCTYPE(wintypes.DWORD, wintypes.HANDLE, wintypes.LPVOID)

# ftype(address) doesn't expect an unwrapped C address, but a Python function,
# so I used from_address() instead.
function = ftype.from_address(address)

ret = function(0x123,None) # None can be used for a null pointer
print(ret)

Output:

h=0000000000000123 pv=0000000000000000
1
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.