Use a DLL with python (using ctypes), not working
Question: I'm trying to write a DLL that I can import in Python (2.7), and I'm having
difficulties "making it work". When I load the library in Python using
`WinDLL()` or `windll.LoadLibrary()`, and test the exported function the
output i get is empty. If I add an argument to `TestFunction()` it raises a
`ValueError` which states that there is probably to many arguments (it's
really not).

**python-file:**

    
    
    from ctypes import *
    
    x = windll.LoadLibrary('./pymod.dll')
    print x.TestFunction(123) #Should return 123.
    

**main.h:**

    
    
    #ifndef __MAIN_H__
    #define __MAIN_H__
    #include <windows.h>
    #define DLL_EXPORT __declspec(dllexport)    
    
    #ifdef __cplusplus
    extern "C"
    {
    #endif
    
    int DLL_EXPORT TestFunction(int data);
    
    #ifdef __cplusplus
    }
    #endif
    
    #endif
    

and **main.cpp:**

    
    
    #include "main.h"
    
    int DLL_EXPORT TestFunction(int x = 0) {
        return x;
    }
    
    BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) {
        switch (fdwReason){
            case DLL_PROCESS_ATTACH:
                break;
            case DLL_PROCESS_DETACH:
                break;
            case DLL_THREAD_ATTACH:
                break;
            case DLL_THREAD_DETACH:
                break;
        }
        return TRUE;
    }
    

**Solved** : Issue was wrong calling convention.


Answer: My first guess would be that you are using a different calling convention than
your Python assumes.

    
    
    int DLL_EXPORT TestFunction(int data);
    

That declaration probably means that `cdecl` calling convention will be used;
while use of `windll` makes Python believe that the `stdcall` convention
should be used. That effectively changes the way that arguments are passed,
and thus makes Python believe that you have passed the wrong number of
arguments. This is noted in the [ctypes
documentation](http://docs.python.org/library/ctypes.html#calling-functions).

If that's the case, you can do either of the following:

  1. Change the calling convention in the C code to `stdcall` (which is used in Windows system DLLs). If you are using MSVC, they have [a doc on developing DLLs](http://msdn.microsoft.com/en-us/library/office/bb687850.aspx);
  2. Change the calling convention expected by the Python code to `cdecl`. This is done by using `cdll` instead of `windll`.

