2009-10-27 89 views
1

我有DLL,C++接口与他一起工作。在bcb中,msvc工作正常。我想使用Python脚本来访问这个库中的函数。 使用Swig生成python-package。从DLL运行函数访问冲突

文件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(); 
    }; 
}; 
+0

也许你可以从生成的代码中添加调用'Factory :: SetLength()'的行,并从'DcmFactory.h'中添加原始声明? – 2009-10-27 18:54:05

+0

... arg1 = reinterpret_cast (argp1); ecode2 = SWIG_AsVal_int(OBJ1,&val2); 如果(!SWIG_IsOK( “在方法“”'IRegistrationMessage_SetLength“ ” '参数 “ ”2“,” 类型的“' ”INT“ ecode2)){ SWIG_exception_fail(SWIG_ArgError(ecode2), “'”); } arg2 = static_cast < int >(val2); (arg1) - > SetLength(arg2); ... – Xeningem 2009-10-28 08:14:35

回答

0

我发现bug。 如果您使用DLL,您必须编写调用约定在一个明确的形式是这样的:

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

我追加调用约定,现在一切工作的罚款。