타사에서 .lib 및 .h 파일과 함께 제공되는 DLL을 가지고 있습니다 ("test.dll", "test .lib "및"test.h ")SWIG 및 Python을 통해 타사 .dll 공유 라이브러리에서 함수에 액세스
이 전달 된 DLL에는 Python 스크립트에서 액세스해야하는 일부 기능이 포함되어 있습니다. 이 때문에 SWIG 및 MSVC2010을 사용하여 확장명 (.pyd)을 만들어야합니다. (제 3 자 파일을 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에 "라이브러리). ,
/* 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;
}
이 꿀꺽 꿀꺽를 사용하려면 : "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"파일에 넣어 나는 MSVC 프로젝트에 다음 꿀꺽 꿀꺽 인터페이스 파일 .I 추가해야합니다 : 나는 "사용자 지정 도구를 구축"에 넣어,이 .I 파일의 "등록 정보"에서 MSVC 프로젝트에서
/* 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"
를 -> "명령 선 ", 다음 :
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)"
OK! 나는 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
사람이 제발 도와 주 시겠어요? 어떤 아이디어라도 감사하겠습니다!
감사합니다.
그래서 헤더 파일, dll 파일 및 swig를 사용하여 파이썬 래퍼를 생성 할 수 있습니까? – bicepjai