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.