tags:

views:

112

answers:

1

I have DLL, interface on C++ for work with he. In bcb, msvc it works fine. I want to use Python-scripts to access function in this library. Generate python-package using Swig.

File 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

%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)

setup build
setup install

using this 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)

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

uRegistrationMessage.cpp (implementation in 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)

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

I find bug. If you using DLL, you must write calling conventions in an explicit form like this:

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.

Xeningem