日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

Windows系统Python直接调用C++ DLL

發布時間:2023/12/10 c/c++ 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Windows系统Python直接调用C++ DLL 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

環境:Window 10,VS 2019, Python 2.7.12, 64bit

1,打開 VS 2019,新建C++ Windows 動態鏈接庫工程 Example,加入下列文件,如果Python是64位的則在VS中 Solution platforms 選擇 x64 編譯成64位的 DLL;

Example.h

1 #pragma once 2 3 #ifndef CPP_EXPORTS 4 #define CPP_EXPORTS 5 #endif 6 7 #ifdef CPP_EXPORTS 8 #define CPP_API _declspec(dllexport) 9 #else 10 #define CPP_API _declspec(dllimport) 11 #endif 12 13 #include <iostream> 14 using namespace std; 15 16 #ifdef __cplusplus 17 extern "C" 18 { 19 #endif 20 21 CPP_API int __cdecl getInt(); 22 CPP_API const char* __cdecl getString(); 23 CPP_API void __cdecl setString(const char* str); 24 25 #ifdef __cplusplus 26 } 27 #endif

?

Example.cpp

1 #include "pch.h" 2 #include "Example.h" 3 4 CPP_API int __cdecl getInt() 5 { 6 return 5; 7 } 8 9 CPP_API const char* __cdecl getString() 10 { 11 return "hello"; 12 } 13 14 CPP_API void __cdecl setString(const char* str) 15 { 16 cout << str << endl; 17 }

?

編譯,得到 Example.dll

?

2, 打開 Command,cd 到?Example.dll 所在目錄,輸入 Python2,進入python環境

>>> 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

轉載于:https://www.cnblogs.com/gujf2016/p/11282352.html

總結

以上是生活随笔為你收集整理的Windows系统Python直接调用C++ DLL的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。