Windows系統(tǒng)Python直接調用C++ DLL的方法
環(huán)境:Window 10,VS 2019, Python 2.7.12, 64bit
1,打開 VS 2019,新建C++ Windows 動態(tài)鏈接庫工程 Example,加入下列文件,如果Python是64位的則在VS中 Solution platforms 選擇 x64 編譯成64位的 DLL;
Example.h
#pragma once
#ifndef CPP_EXPORTS
#define CPP_EXPORTS
#endif
#ifdef CPP_EXPORTS
#define CPP_API _declspec(dllexport)
#else
#define CPP_API _declspec(dllimport)
#endif
#include <iostream>
using namespace std;
#ifdef __cplusplus
extern "C"
{
#endif
CPP_API int __cdecl getInt();
CPP_API const char* __cdecl getString();
CPP_API void __cdecl setString(const char* str);
#ifdef __cplusplus
}
#endif
Example.cpp
#include "pch.h"
#include "Example.h"
CPP_API int __cdecl getInt()
{
return 5;
}
CPP_API const char* __cdecl getString()
{
return "hello";
}
CPP_API void __cdecl setString(const char* str)
{
cout << str << endl;
}
編譯,得到 Example.dll
2, 打開 Command,cd 到 Example.dll 所在目錄,輸入 Python2,進入python環(huán)境
>>> from ctypes import *
>>> dll = CDLL("Example.dll")
>>> print dll.getInt()
5
>>> getStr = dll.getString
>>> getStr.restype = c_char_p
>>> pChar = getStr()
>>> print c_char_p(pChar).value
hello
>>> setStr = dll.setString
>>> setStr.argtypes = [c_char_p]
>>> pStr = create_string_buffer("hello")
>>> setStr(pStr)
hello
-1043503984
總結
以上所述是小編給大家介紹的Windows系統(tǒng)Python直接調用C++ DLL的方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉載,煩請注明出處,謝謝!
- Python擴展C/C++庫的方法(C轉換為Python)
- pybind11: C++ 工程提供 Python 接口的實例代碼
- Python3安裝模塊報錯Microsoft Visual C++ 14.0 is required的解決方法
- C、C++、Java到Python,編程入門學習什么語言比較好
- 基于Python和C++實現(xiàn)刪除鏈表的節(jié)點
- Python嵌入C/C++進行開發(fā)詳解
- ubunt18.04LTS+vscode+anaconda3下的python+C++調試方法
- 使用C++調用Python代碼的方法詳解
- 使用C++調用Python代碼的方法步驟
- python或C++讀取指定文件夾下的所有圖片
- python和C++共享內存?zhèn)鬏攬D像的示例
相關文章
Python實現(xiàn)數(shù)據(jù)濾波的示例詳解
這篇文章主要為大家詳細介紹了Python實現(xiàn)數(shù)據(jù)濾波的相關知識,文中的示例代碼講解詳細,具有一定的借鑒價值,有需要的小伙伴可以跟隨小編一起學習一下2024-03-03
python中dir()與__dict__屬性的區(qū)別淺析
這篇文章主要給大家介紹了關于python中dir()與__dict__屬性的區(qū)別的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2018-12-12
ubuntu安裝jupyter并設置遠程訪問的實現(xiàn)
Jupyter?Notebook是Ipython的升級版,而Ipython可以說是一個加強版的交互式Shell,本文主要介紹了ubuntu安裝jupyter并設置遠程訪問的實現(xiàn),感興趣的可以了解一下2022-03-03

