已实现的内容
- c++参数为字符串指针
- python向c++设置回调函数
编译C++ 动态库
文件列表
- ControllerInterface.h // 测试类
- maindll.h // 动态库头
- maindll.cpp // 代码
代码
ControllerInterface.h
#ifndef __CONTROLLERINTERFACE_H__
#define __CONTROLLERINTERFACE_H__
#include <chrono>
#include <iostream>
#include <functional>
#include <thread>
// 必须要用c的方式申明函数指针
typedef void (*CallBackTest)(char*);
class ControllerInterface
{
public:
ControllerInterface():m_func(NULL){
}
char* Test(char* jsonData)
{
std::cout << "just test:" << jsonData << std::endl;
return "testjjjj";
}
void SetCallBack(CallBackTest func)
{
m_func = func;
}
void runThread()
{
std::thread tt([&](){
int i = 0;
while (1) {
if (m_func) {
std::string s(std::to_string(i));
s = "test****" + s;
i++;
m_func(const_cast<char*>(s.data()));
}
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}
});
tt.detach();
}
private:
CallBackTest m_func;
};
maindll.h
#ifndef __MAINDLL_H__
#define __MAINDLL_H__
#include "ControllerInterface.h"
#include <map>
#include <string>
#include <functional>
// 申明动态库接口
extern "C" {
// 申明调用接口
char* callback(char* pFuncName, char* pJsonData);
// 申明dll初始化
void init();
// 申明回调函数
void SetCallBack(CallBackTest func);
}
// 实际处理逻辑
typedef std::function<char* (char*)> callFuncType;
std::map<std::string, callFuncType> g_mapCallFuncs;
static ControllerInterface g_ControllerInterface;
char* hello(char* pJsonData);
#endif
maindll.cpp
#include "maindll.h"
#include <iostream>
#include <map>
#include <functional>
#include <string>
using namespace std;
char* callback(char* pFuncName, char* pJsonData)
{
cout << pFuncName << ";" << pJsonData << endl;
std::string strFuncName(pFuncName);
auto node = g_mapCallFuncs.find(strFuncName);
if (node != g_mapCallFuncs.end()) {
return node->second(pJsonData);
}else {
cout << "error call:" << pFuncName << ";" << pJsonData << endl;
}
return "";
}
void init()
{
g_mapCallFuncs["hello"] = hello;
g_mapCallFuncs["test"] = std::bind(&ControllerInterface::Test, g_ControllerInterface,std::placeholders::_1);
}
void SetCallBack(CallBackTest func)
{
g_ControllerInterface.SetCallBack(func);
}
char* hello(char* pJsonData)
{
cout << "callfunc:" << pJsonData << endl;
return pJsonData;
}
Linux 编译动态库
- g++ maindll.cpp -fPIC -shared -o libmaindll.so -std=c++11
python 调用c++动态库
文件列表
- loadcdll.py # 加载并声明动态库的函数
- test.py # 测试
python 代码
loadcdll.py
import os
from ctypes import *
# 获取执行文件路径 需要将动态库拷到和python执行文件相同的目录
exefilepath = os.path.dirname(__file__)
# 加载动态库
pDll = CDLL(os.path.join(exefilepath, "libmaindll.so"))
# 申明动态库函数
dllcallback = pDll.callback
dllinit = pDll.init
# 入参列表
dllcallback.argtypes = [c_char_p, c_char_p]
# 返回值
dllcallback.restype = c_char_p
# 初始化动态库
def InitDLL():
dllinit()
def CallbackDLL(callFuncName:str, ParamsJsonData:str):
# 入参要转成byte字符,返回值也是byte需要解码成字符串
return dllcallback(callFuncName.encode('utf-8'), ParamsJsonData.encode('utf-8')).decode('utf-8')
def SetCallBackDLL(callbackfunc):
# 设置回调函数的返回值以及参数类型类型
testCallBackFunc = CFUNCTYPE(None,c_char_p)(callbackfunc) #回调函数类型定义
dllSetCallBack(testCallBackFunc)
test.py
from loadcdll import *
InitDLL()
# 测试字符串的
CallbackDLL("test", "justtesthello")
def reciveDLLData(data):
# 返回的是byte数据
print (data.decode())
pass
SetCallBackDLL(reciveDLLData)
while 1:
time.sleep(20)
python调用c++函数字符类型设置说明
参考:https://www.cnblogs.com/wuchang/archive/2010/04/04/1704456.html