2015-07-21 195 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属性中看到

相关问题