2015-07-21 197 views
1

我目前正在努力獲得一些python代碼來定位DLL文件中的函數。 我已經看過幾篇文章,關於這個和各種方法似乎並沒有爲我工作。 請忽略代碼可能是使用Python中的GUI庫實現的事實,這對我來說不是一種選擇。在python中使用DLL - 無法找到函數或類

所以我的頭文件如下:

#pragma once 

#ifdef MOBILEFUNC_EXPORTS 
#define MOBILEFRONTEND_API __declspec(dllexport) 
#else 
#define MOBILEFRONTEND_API __declspec(dllimport) 
#endif 

#ifdef __cplusplus 
    class mobileFrontEnd{ 
    public: 
     char* getPath(); 
    }; 
#else typedef struct _frontEnd frontEnd; 
#endif 

#ifdef __cplusplus 
extern "C" { 
#endif 
MOBILEFRONTEND_API mobileFrontEnd *frontend_create(); 
MOBILEFRONTEND_API char* getPath(); 

#ifdef __cplusplus 
} 
#endif 



using namespace System; 
using namespace System::Windows::Forms; 


namespace frontEnd { 

    class MOBILEFRONTEND_API mobileFrontEnd 
    { 
    public: 
     static char* getPath(); 
    }; 
} 

而我主要的C++文件是:

#include "stdafx.h" 
#include "mobileFrontEnd.h" 

using namespace::Runtime::InteropServices; 

namespace frontEnd 
{ 

    mobileFrontEnd *frontend_create() 
    { 
     mobileFrontEnd *self = new mobileFrontEnd; 
     return self; 
    } 

    char* mobileFrontEnd::getPath() 
    { 
     FolderBrowserDialog^ openDialog1 = gcnew FolderBrowserDialog(); 
     if (openDialog1->ShowDialog() == DialogResult::OK) 
     { 
      String^ path = openDialog1->SelectedPath; 
      return (char*)(void*)Marshal::StringToHGlobalAnsi(path); 
     } 
     else 
     { 
      return 0; 
     } 
    } 
} 

的DLL進口使用python中CDLL或WINDLL功能,但任何企圖訪問函數或類導致錯誤,說明類/函數不存在。 我沒有任何真正的python代碼,因此我一直試圖在python命令提示符下檢查它。 我是否錯過了一些東西以確保它能正確導出功能?

一些Python代碼編輯: 所以類似這樣的事情(從http://eli.thegreenplace.net/2008/08/31/ctypes-calling-cc-code-from-python

import ctypes 
>>> test_dll = ctypes.CDLL("C:\\Users\\xxxx\\Documents\\Visual Studio 2012\\Projects\\mobileFrontEnd\\Release\\mobilefrontend.dll") 
>>> test_cb = test_dll.getPath(); 

Traceback (most recent call last): 
    File "<pyshell#3>", line 1, in <module> 
    test_cb = test_dll.getPath(); 
    File "C:\Python27\lib\ctypes\__init__.py", line 378, in __getattr__ 
    func = self.__getitem__(name) 
    File "C:\Python27\lib\ctypes\__init__.py", line 383, in __getitem__ 
    func = self._FuncPtr((name_or_ordinal, self)) 
AttributeError: function 'getPath' not found 
>>> 

編輯2: 同樣以防萬一它是不是從代碼清晰(使用Windows窗體)該DLL在Visual Studio 2012快遞編譯,包括「公共庫運行時的支持

+0

很有可能,您能發表幾行Python代碼,以便我們看到問題可能是什麼?你可能還想看看['cffi'](https://cffi.readthedocs.org/en/latest/)是否符合你的需求。 – 101

+0

cffi很有趣,但是我會在另一個程序中運行這個腳本,因此只能使用它支持的庫。 Ctypes是支持的,我目前沒有在程序中測試它只是空閒環境 – minime

+0

你可以檢查DLL實際上是用DLL檢查工具(不是Python)以正確的名稱導出該函數嗎? – 101

回答

0

使用下面的代碼似乎是用於訪問功能的工作:

import ctypes 

test_dll = ctypes.CDLL("**path to dll**") 

test1 = ctypes.WINFUNCTYPE(None) 
test2 = test1(("getPath",test_dll)) 
test2() 

不知道爲什麼函數無法在test_dll屬性中看到

相關問題