来自dll的运行函数访问冲突

时间:2009-10-27 15:48:44

标签: c++ python swig

我有DLL,C ++接口可以和他一起工作。在bcb,msvc它工作正常。我想使用Python脚本来访问此库中的函数。 使用Swig生成python-package。

文件 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'])

文件 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"

运行这些命令(python与扩展名.py相关联)

setup build
setup install

使用此DLL

import dcm

f = dcm.Factory() #ok

r = f.getRegistrationMessage() #ok
print "r.GetLength() ", r.GetLength() #ok
r.SetLength(0) #access violation

在最后一个字符串中,我遇到了访问冲突。我使用输入参数对每个函数都有访问冲突。

DcmInterface.h (界面)

class IRegistrationMessage
{
public:
...
    virtual int GetLength() const = 0;
    virtual void SetLength(int value) = 0;
...
};

uRegistrationMessage.cpp (在DLL中实现)

class TRegistrationMessage : public IRegistrationMessage
{
public:
...
virtual int GetLength() const
    {
        return FLength;
    }
    virtual void SetLength(int Value)
    {
        FLength = Value;
        FLengthExists = true;
    }
...
};

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();
    };
};

1 个答案:

答案 0 :(得分:0)

我发现了bug。 如果使用DLL,则必须以如下显式形式编写调用约定:

class IRegistrationMessage
{
public:
...
    virtual int _cdecl GetLength() const = 0;
    virtual void _cdecl SetLength(int value) = 0;
...
};

我追加调用约定,现在一切正常。