通过SWIG和Python从第三方.dll共享库访问函数

问题描述:

我从第三方获得了与他的.lib和.h文件一起发布的DLL(假设文件是​​:“test.dll”,“test .lib“和”test.h“)通过SWIG和Python从第三方.dll共享库访问函数

此传递的DLL包含一些函数,我应该从Python脚本访问它们。 为此,我必须使用SWIG和MSVC2010构建扩展名(.pyd)。 (我将第三方文件复制到MSVC项目的目录中)

要了解有关“test.h”文件的概述,这就是它的外观(为简单起见,我只放了一个函数“CreateFile() “,它返回一个文件句柄):

/* File : test.h */ 

#if !defined (TEST_H) 
     #define TEST_H 

#if defined (_MSC_VER) 
    #pragma warning(disable: 4103) 
    #pragma pack(push, 8) 
#elif defined (__BORLANDC__) 
    #pragma option push -a8 
    #pragma nopushoptwarn 
    #pragma nopackwarning 
#endif 

#include <wtypes.h> 

/*---------------------------------------------------------------------------- 
| BL API 
-----------------------------------------------------------------------------*/ 
#if defined (DLL_EXPORTS) 
    #define BLAPI(ret)      ret __stdcall 
#else 
    #define BLAPI(ret) __declspec(dllimport) ret __stdcall 
#endif 

/*---------------------------------------------------------------------------- 
| API 
-----------------------------------------------------------------------------*/ 
#if defined (__cplusplus) 
extern "C" { 
#endif 

BLAPI(HANDLE) CreateFile(LPCTSTR lpFileName, DWORD dwDesiredAccess); 

#if defined (__cplusplus) 
} 
#endif 


/*---------------------------------------------------------------------------- 
| 
-----------------------------------------------------------------------------*/ 

#if defined (_MSC_VER) 
    #pragma pack(pop) 
#elif defined (__BORLANDC__) 
    #pragma option pop 
    #pragma nopushoptwarn 
    #pragma nopackwarning 
#endif 

#endif // TEST_H 

我打算做一个类来包装这些第三方函数(在”test.dll“库中实现)。该 “myInterface.h” 文件是这样的:

/* File : myInterface.h */ 

#include <windows.h> 
#include "test.h"   // <--- third party header 

class myInterfaceClass { 
public: 
    myInterfaceClass() { 
    } 

    virtual ~myInterfaceClass() { 
    }; 

    HANDLE hFile; 
    BOOL errorCode; 

    BOOL OpenFile(LPCTSTR lpFileName); // <-- function wrapper 

}; 

...和类的实现,我把它改成 “myInterface.cxx” 文件:

/* File : myInterface.cxx */ 

#include "myInterface.h" 
#include "test.h"   // <--- third party header 
#include <windows.h> 

BOOL myInterfaceClass::OpenFile(LPCTSTR lpFileName) 
{ 
    errorCode = TRUE; 
    // open file 
    hFile = CreateFile(lpFileName, GENERIC_READ); // <--- third party function call 
    errorCode = (INVALID_HANDLE_VALUE == hFile); 

    return errorCode; 
} 

要使用痛饮,我必须添加到MSVC工程以下SWIG接口文件.I:

/* File : myInterface.i */ 
%module myInterface 

%{ 
#include "myInterface.h" 
#include "test.h" 
%} 

/* Let's just grab the original header file here */ 
%include "myInterface.h" 
%include "test.h" 

在MSVC项目,在这个.i文件的“属性”,我把“自定义生成工具” - >“命令线“,如下:

echo PYTHON_INCLUDE: %PYTHON_INCLUDE% 
echo PYTHON_LIB: %PYTHON_LIB% 

rem 
rem WARNING!: Use quotes (" ") on path names to avoid errors ! 
rem 

echo on 
echo. 
echo. "%(FullPath)" 
echo. 
"%SWIG_PATH%\swig.exe" -c++ -python -I%SWIG_PATH%\lib -Itest "%(FullPath)" 

行! 当我尝试建立的PYD扩展,我得到这个错误:

Error 1 error : Syntax error in input(1). D:\ADXWorkZone\testSwig\test.h 33 1 myInterface 

...但并没有什么错“test.h”文件。我使用相同的文件(没有任何修改)来实现与经典DLL相同的C++包装类,并且它运行良好。

项目规格:

Properties -> C/C++ -> General -> Additional Include Directories: $(PYTHON_INCLUDE) 
Properties -> Linker -> General -> Output File: _myInterface.pyd 
Properties -> Linker -> Input -> Additional Dependencies: $(PYTHON_LIB);test.lib 

谁能帮帮我,好吗? 任何想法将不胜感激!

谢谢!

+0

所以与头文件,dll文件和swig,我们可以生成python包装? – bicepjai 2015-04-15 20:09:06

尝试在您的其他%包括在您的界面文件中添加以下内容。

%include <windows.i> 

SWIG不递归到嵌套包含,并且这提供定义如BOOL和_declspec否则混淆SWIG。

+0

**你是男人!谢谢!**它的工作原理!大!两天苦苦挣扎。我知道这是来自SWIG界面,但我不知道在哪里。现在,我有... :)谢谢! – roadx 2012-04-10 07:01:46

+0

不客气!不要忘了勾选答案,如果它对你有用。 – 2012-04-10 13:46:44

+0

我想他忘了.... – Jiminion 2016-05-18 14:11:28