簡體   English   中英

來自DLL的運行功能上的訪問沖突

[英]Access violation on run function from dll

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

我發現了錯誤。 如果使用DLL,則必須以顯式形式編寫調用約定,如下所示:

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

我追加了調用約定,現在一切正常。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM