Python 調(diào)用VC++的動態(tài)鏈接庫(DLL)
更新時間:2008年09月06日 14:33:29 作者:
Python下調(diào)用VC++的動態(tài)鏈接庫的腳本
1. 首先VC++的DLL的導出函數(shù)定義成標準C的導出函數(shù):
#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中調(diào)用如下:
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
關(guān)于C語言中的一些參數(shù)類型詳見:http://www.python.org/doc/2.5/lib/node454.html
3. 輸出結(jié)果:
5
Hello DLL
復制代碼 代碼如下:
#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中調(diào)用如下:
復制代碼 代碼如下:
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
關(guān)于C語言中的一些參數(shù)類型詳見:http://www.python.org/doc/2.5/lib/node454.html
3. 輸出結(jié)果:
5
Hello DLL
相關(guān)文章
PyTorch開源圖像分類工具箱MMClassification詳解
MMClassification是一款基于PyTorch的開源圖像分類工具箱,集成了常用的圖像分類網(wǎng)絡,將數(shù)據(jù)加載,模型骨架,訓練調(diào)參,流程等封裝為模塊調(diào)用,便于在模型間進行轉(zhuǎn)換和比較,也高效簡潔的實現(xiàn)了參數(shù)調(diào)整2022-09-09Jupyter Notebook運行代碼無反應問題及解決方法
這篇文章主要介紹了Jupyter Notebook運行代碼無反應問題及解決方法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-01-01Python multiprocessing.Manager介紹和實例(進程間共享數(shù)據(jù))
這篇文章主要介紹了Python multiprocessing.Manager介紹和實例(進程間共享數(shù)據(jù)),本文介紹了Manager的dict、list使用例子,同時介紹了namespace對象,需要的朋友可以參考下2014-11-11