Windows系統(tǒng)Python直接調(diào)用C++ DLL的方法
環(huán)境:Window 10,VS 2019, Python 2.7.12, 64bit
1,打開 VS 2019,新建C++ Windows 動(dòng)態(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,進(jìn)入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
總結(jié)
以上所述是小編給大家介紹的Windows系統(tǒng)Python直接調(diào)用C++ DLL的方法,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
- Python擴(kuò)展C/C++庫的方法(C轉(zhuǎn)換為Python)
- pybind11: C++ 工程提供 Python 接口的實(shí)例代碼
- Python3安裝模塊報(bào)錯(cuò)Microsoft Visual C++ 14.0 is required的解決方法
- C、C++、Java到Python,編程入門學(xué)習(xí)什么語言比較好
- 基于Python和C++實(shí)現(xiàn)刪除鏈表的節(jié)點(diǎn)
- Python嵌入C/C++進(jìn)行開發(fā)詳解
- ubunt18.04LTS+vscode+anaconda3下的python+C++調(diào)試方法
- 使用C++調(diào)用Python代碼的方法詳解
- 使用C++調(diào)用Python代碼的方法步驟
- python或C++讀取指定文件夾下的所有圖片
- python和C++共享內(nèi)存?zhèn)鬏攬D像的示例
相關(guān)文章
Python實(shí)現(xiàn)數(shù)據(jù)濾波的示例詳解
這篇文章主要為大家詳細(xì)介紹了Python實(shí)現(xiàn)數(shù)據(jù)濾波的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,有需要的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-03-03
python中dir()與__dict__屬性的區(qū)別淺析
這篇文章主要給大家介紹了關(guān)于python中dir()與__dict__屬性的區(qū)別的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-12-12
淺談tensorflow中幾個(gè)隨機(jī)函數(shù)的用法
今天小編就為大家分享一篇淺談tensorflow中幾個(gè)隨機(jī)函數(shù)的用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-07-07
Python解壓可迭代對(duì)象賦值給多個(gè)變量詳解
這篇文章主要為大家介紹了Python賦值多個(gè)變量,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2021-12-12
ubuntu安裝jupyter并設(shè)置遠(yuǎn)程訪問的實(shí)現(xiàn)
Jupyter?Notebook是Ipython的升級(jí)版,而Ipython可以說是一個(gè)加強(qiáng)版的交互式Shell,本文主要介紹了ubuntu安裝jupyter并設(shè)置遠(yuǎn)程訪問的實(shí)現(xiàn),感興趣的可以了解一下2022-03-03

