vs2015开发python_VS2015 编写C++ DLL库及C++、 C#、python 调用
VS2015生成c++dll
C++ 程序調用 dll
C# 程序調用 dll
1. VS2015 生成C++dll
可以有兩種方法通過_declspec(dllexport)
extern "C" __declspec(dllexport) int __stdcall add(int a, int b);嫌上面太麻煩,windows系統下可用 .def文件
其中:extern "c" 防止導出的函數名字是亂碼
使用 def導出dll步驟如下:
新建Login.h
//登錄接口
extern "C" int _stdcall Login(char* username, char* password);
//退出接口 無返回值
extern "C" bool _stdcall Logout();
新建Login.cpp
int _stdcall Login(char* name, char* pwd) {
}
新建模塊定義文件Login.def
LIBRARY Logindll
EXPORTS
Login @ 1
Logout @ 2
Heartbeat @3
生成文件Debug 和Release下 Login.dll Login.lib
2.C++ 調用
需要3個文件 Login.h Login.dll Login.lib
根據Debug和Release 分別把三個文件拷貝到對應工程目錄下
步驟:右鍵添加現有項 選擇Login.h
右鍵添加現有項 選擇Login.lib
在使用的地方 #include “Login.h”
直接使用函數
{
Login("user","pwd");
}
3.C# 調用
需要1個文件 Login.dll將Login.dll 拷貝到相對應的debug或release目錄下
使用時代碼如下
[DllImport("Logindll.dll", EntryPoint = "Login", CallingConvention = CallingConvention.StdCall)]
extern static int Login(string name,string pwd);
4. python調用
需要2個文件 Login.dll Login.lib將上面兩個文件拷貝到.py文件同名目錄
通過ctypes調用
注意python是x86 x64 和dll的版本保持一致
from ctypes import *
import os
CUR_PATH=os.path.dirname(__file__)
dllPath=os.path.join(CUR_PATH,"Logindll.dll")
pDll=cdll.LoadLibrary(dllPath)
pResutl= pDll.Logout()
print (pResutl)
注意:
x86 x64 debug release 庫的對應關系
參考:CSDN-專業IT技術社區-登錄?blog.csdn.net在VS2015中用C++編寫可被其它語言調用的動態庫DLL - 優秀afa - 博客園?www.cnblogs.com
總結
以上是生活随笔為你收集整理的vs2015开发python_VS2015 编写C++ DLL库及C++、 C#、python 调用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python语言的解释性特点指的是编写的
- 下一篇: 零压力学python_《零压力学Pyth