2012-04-09 41 views
1

我從第三方獲得了與他的.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

回答

5

嘗試在您的其他%包括在您的界面文件中添加以下內容。

%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