문제

나는 C ++의 인터페이스와 함께 DLL을 가지고있다. BCB에서는 MSVC가 잘 작동합니다. 이 라이브러리에서 Python-Scripts를 사용하여 기능에 액세스하고 싶습니다. 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은 Extension .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();
    };
};
도움이 되었습니까?

해결책

버그를 찾습니다. DLL을 사용하는 경우 다음과 같은 명시 적 형태로 전화 규칙을 작성해야합니다.

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

나는 컨벤션에 전화를 가할 수 있으며 이제는 모두 잘 작동합니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top