2

I want to test wrapping a dll with ctypes. I've written the following test code and compiled it using Code::Blocks and Cygwin to a dll.

#define DLL_EXPORT extern "C" __declspec(dllexport) DLL_EXPORT int sum(int a, int b) { return a + b; } 

Note: This is the whole code. Maybe there is something missing ?

Now, I copy the TestDll.dll to my Desktop and start the Python interpreter. But when I want to load it, the interpreter just exits !

C:\Users\niklas\Desktop>python Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win 32 Type "help", "copyright", "credits" or "license" for more information. >>> import ctypes >>> dll = ctypes.WinDLL('TestDll') C:\Users\niklas\Desktop> 

However, loading any other library works, or giving an error if the library could not be found.
Can you tell me what I'm doing wrong here ?

Using Cygwin g++ 3.4.4

2 Answers 2

2

Check objdump -p TestDll.dll | grep dll to see if you've linked in "cygwin1.dll" and nm TestDll.dll | grep Dll to see if you have a DllMain. The following command should build the DLL correctly:

g++ testdll.c -mno-cygwin -shared -o TestDll.dll 

Also, you need to use CDLL for the cdecl calling convention, not WinDLL:

>>> import ctypes >>> dll = ctypes.CDLL('TestDll') >>> dll.sum(4, 5) 9 

Edit: I compiled with i686-w64-mingw32-g++.exe (4.5.3) from the Cygwin repository, but I used to use the default Cygwin gcc without a problem, given the -mno-cygwin option.

Sign up to request clarification or add additional context in comments.

1 Comment

Yippie, using ctypes.CDLL fixes the problem encountered at the comment of Ignacios post. But using Cygwin still fails, even with theese commands. And no, the dll does not have a dllmain. Why does it have to ? It works without it using MinGw. :)
1

As a complete guess, I'd say that the Cygwin-built DLL is incompatible with the MSVC-built interpreter, either because of a ABI issue or just because you can't use two different libcs. Build the DLL with MinGW instead.

1 Comment

Lol, now that's funny ^^. Works ! Well, generally. It doesn't exit anymore ! But dll.sum doesn't take 8 bytes as expected, it doesn't take any. I need to call it without any arguments and it returns 2620428. Thanks !

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.