Home > Backend Development > Python Tutorial > Python 调用VC++的动态链接库(DLL)

Python 调用VC++的动态链接库(DLL)

WBOY
Release: 2016-06-16 08:47:28
Original
1437 people have browsed it

1. 首先VC++的DLL的导出函数定义成标准C的导出函数:

复制代码 代码如下:

#ifdef LRDLLTEST_EXPORTS
#define LRDLLTEST_API __declspec(dllexport)
#else
#define LRDLLTEST_API __declspec(dllimport)
#endif

extern "C" LRDLLTEST_API int Sum(int a , int b);
extern "C" LRDLLTEST_API void GetString(char* pChar);

//a + b
LRDLLTEST_API int Sum(int a , int b)
{
return a + b;
}

//Get a string
LRDLLTEST_API void GetString(char* pChar)
{
strcpy(pChar, "Hello DLL");
}


2. Python中调用如下:
复制代码 代码如下:

from ctypes import *

fileName="LRDllTest.dll"
func=cdll.LoadLibrary(fileName)
str = create_string_buffer(20)
n = func.Sum(2, 3)
func.GetString(str)

print n
print str.raw

关于C语言中的一些参数类型详见:http://www.python.org/doc/2.5/lib/node454.html

3. 输出结果:
5
Hello DLL
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template