ctypes: How to call unexported functions in a dll

  • Thread starter Coert Klaver (DT)
  • Start date
C

Coert Klaver (DT)

Hi,

I am using ctypes in python 3 on a WXP machine

Loading a dll and using its exported functions works fine.

Now I want to use a function in the dll that is not exported.

In C this can be done by just casting the address in the dll of that
function to an apropriate function pointer and call it (you need to be
sure about the address). Can a similar thing be done directly with
ctypes?

A work around I see is writing in wrapper dll in c that loads the main
dll, exports a function that calls the unexported function in the main
dll, but I don't find that an elegant solution.

Thanks for any hint in the right direction.

BR

Coert
 
T

Thomas Heller

Am 05.01.2010 12:19, schrieb Coert Klaver (DT):
Hi,

I am using ctypes in python 3 on a WXP machine

Loading a dll and using its exported functions works fine.

Now I want to use a function in the dll that is not exported.

In C this can be done by just casting the address in the dll of that
function to an apropriate function pointer and call it (you need to be
sure about the address). Can a similar thing be done directly with
ctypes?

A work around I see is writing in wrapper dll in c that loads the main
dll, exports a function that calls the unexported function in the main
dll, but I don't find that an elegant solution.

No need for a workaround.

One solution is to first create a prototype for the function by calling WINFUNCTYPE
or CFUNCTYPE, depending on the calling convention: stdcall or cdecl, then call the
prototype with the address of the dll function which will return a Python callable.
Demonstrated by the following script using GetProcAddress to get the address of
the GetModuleHandleA win32 api function:

Thomas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top