Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将DLL与python一起使用(使用ctypes),不起作用_C++_Python_Dll_Ctypes - Fatal编程技术网

C++ 将DLL与python一起使用(使用ctypes),不起作用

C++ 将DLL与python一起使用(使用ctypes),不起作用,c++,python,dll,ctypes,C++,Python,Dll,Ctypes,我正在尝试编写一个可以用Python(2.7)导入的DLL,但我在“使其工作”方面遇到了困难。当我使用windle()或windle.LoadLibrary()在Python中加载库并测试导出的函数时,我得到的输出是空的。如果我将一个参数添加到TestFunction()中,它会引发一个ValueError,它指出可能有太多的参数(实际上不是) python文件: from ctypes import * x = windll.LoadLibrary('./pymod.dll') print

我正在尝试编写一个可以用Python(2.7)导入的DLL,但我在“使其工作”方面遇到了困难。当我使用
windle()
windle.LoadLibrary()
在Python中加载库并测试导出的函数时,我得到的输出是空的。如果我将一个参数添加到
TestFunction()
中,它会引发一个
ValueError
,它指出可能有太多的参数(实际上不是)

python文件:

from ctypes import *

x = windll.LoadLibrary('./pymod.dll')
print x.TestFunction(123) #Should return 123.
#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
#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;
}
main.h:

from ctypes import *

x = windll.LoadLibrary('./pymod.dll')
print x.TestFunction(123) #Should return 123.
#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
#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;
}

已解决:问题是呼叫约定错误

我的第一个猜测是,您使用的调用约定与Python假设的不同

int DLL_EXPORT TestFunction(int data);
该声明可能意味着将使用
cdecl
调用约定;而
windell
的使用使Python相信应该使用
stdcall
约定。这有效地改变了参数传递的方式,从而使Python认为您传递了错误数量的参数。这一点在报告中有所说明

如果是这种情况,您可以执行以下任一操作:

  • 将C代码中的调用约定更改为
    stdcall
    (用于Windows系统DLL)。如果你使用的是MSVC,他们有
  • 将Python代码期望的调用约定更改为
    cdecl
    。这是通过使用
    cdll
    而不是
    windl
    来完成的

  • 我无法直接回答您的问题,但如果您想制作Python模块,我建议使用Boost.Python。只是一个想法。你能粘贴准确的错误吗?我认为它应该指定多余/缺少的字节数。确切的错误是
    ValueError:调用过程时可能使用了太多的参数(多余4个字节)
    您可以使用我的答案:为什么“/pymod.dll”?是windows、linux还是cygwin?