[英]Access violation on run function from dll
I have DLL, interface on C++ for work with he. 我有DLL,可以在C ++上与他一起工作。 In bcb, msvc it works fine. 在bcb中,msvc可以正常工作。 I want to use Python-scripts to access function in this library. 我想使用Python脚本访问该库中的函数。 Generate python-package using Swig. 使用Swig生成python-package。
File setup.py 文件setup.py
import distutils
from distutils.core import setup, Extension
setup(name = "DCM",
version = "1.3.2",
ext_modules = [Extension("_dcm", ["dcm.i"], swig_opts=["-c++","-D__stdcall"])],
y_modules = ['dcm'])
file dcm.i dcm.i文件
%module dcm
%include <windows.i>
%{
#include <windows.h>
#include "../interface/DcmInterface.h"
#include "../interface/DcmFactory.h"
#include "../interface/DcmEnumerations.h"
%}
%include "../interface/DcmEnumerations.h"
%include "../interface/DcmInterface.h"
%include "../interface/DcmFactory.h"
run these command (python is associated with extension .py) 运行以下命令(python与扩展名.py相关联)
setup build
setup install
using this DLL 使用此DLL
import dcm
f = dcm.Factory() #ok
r = f.getRegistrationMessage() #ok
print "r.GetLength() ", r.GetLength() #ok
r.SetLength(0) #access violation
On last string I get access violation. 在最后一个字符串上,我遇到访问冲突。 And I have access violation on every function using input parameters. 而且,使用输入参数的每个函数都有访问冲突。
DcmInterface.h (interface) DcmInterface.h (接口)
class IRegistrationMessage
{
public:
...
virtual int GetLength() const = 0;
virtual void SetLength(int value) = 0;
...
};
uRegistrationMessage.cpp (implementation in DLL) uRegistrationMessage.cpp (在DLL中实现)
class TRegistrationMessage : public IRegistrationMessage
{
public:
...
virtual int GetLength() const
{
return FLength;
}
virtual void SetLength(int Value)
{
FLength = Value;
FLengthExists = true;
}
...
};
Factory 厂
DcmFactory.h (using DLL in client code) DcmFactory.h (在客户端代码中使用DLL)
class Factory
{
private:
GetRegistrationMessageFnc GetRegistration;
bool loadLibrary(const char *dllFileName = "dcmDLL.dll" )
{
...
hDLL = LoadLibrary(dllFileName);
if (!hDLL) return false;
...
GetRegistration = (GetRegistrationMessageFnc) GetProcAddress( hDLL, "getRegistration" );
...
}
public:
Factory(const char* dllFileName = "dcmDLL.dll")
{
loadLibrary(dllFileName);
}
IRegistrationMessage* getRegistrationMessage()
{
if(!GetRegistration) return 0;
return GetRegistration();
};
};
I find bug. 我发现了错误。 If you using DLL, you must write calling conventions in an explicit form like this: 如果使用DLL,则必须以显式形式编写调用约定,如下所示:
class IRegistrationMessage
{
public:
...
virtual int _cdecl GetLength() const = 0;
virtual void _cdecl SetLength(int value) = 0;
...
};
I append calling conventions and now all work fine. 我追加了调用约定,现在一切正常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.