Python獲取C++中返回的char*字段的兩種思路
有時候需要獲取C++函數(shù)中返回來的不定長的char*字符串,目前我找到兩種解決問題的思路,具體實現(xiàn)如下:
一、將char*放入結(jié)構(gòu)體中
c++函數(shù)如下:
typedef struct FileData { long long lenth; char* data; }; bool GetData(FileData *data) { if (data == nullptr) { return false; } data->data = new char[255]; memset(data->data, 0, 255); const char* laosi = "wdaweqweqweqweqweqweqweqwdvdggggsdfsferqwerawerqwerqwerqerqwedsd"; memcpy(data->data, laosi, strlen(laosi)); data->lenth = strlen(laosi); return true; }
python代碼如下:
import ctypes import os import platform from threading import Thread,Lock CharPtr = ctypes.POINTER(ctypes.c_char) class FileData(ctypes.Structure): _fields_=[ ("lenth",ctypes.c_longlong), ("data",CharPtr) ] FileDataPtr = ctypes.POINTER(FileData) def GetDll(): if platform.system().lower() == 'windows': isWinPlat_ = True else: isWinPlat_ = False currentPath_ = os.getcwd().replace('\\','/') if isWinPlat_: dll_ = ctypes.CDLL(currentPath_ + '/PyTest.dll') else: dll_ = ctypes.CDLL(currentPath_ + '/PyTest.so') return dll_ pytestdll_ = GetDll() def GetData(): getDataF = pytestdll_.GetData getDataF.argtypes=[FileDataPtr] getDataF.restype = ctypes.c_bool dataa = FileData() ret = getDataF(ctypes.byref(dataa)) print(dataa.lenth) len = dataa.lenth for i in range(len): print(dataa.data[i]) charArr = ctypes.c_char*dataa.lenth char_arr = charArr(*dataa.data[:dataa.lenth]) print(char_arr.raw) return ret GetData()
二、將char*作為返回值
c++代碼如下:
char* GetDatas(long long& lenth) { char* data = new char[255]; memset(data, 0, 255); const char* laosi = "wdaweqweqweqweqweqweqweqwdvdggggsdfsferqwerawerqwerqwerqerqwedsd"; memcpy(data, laosi, strlen(laosi)); lenth = strlen(laosi); return data; }
python代碼如下:
import ctypes import os import platform from threading import Thread,Lock CharPtr = ctypes.POINTER(ctypes.c_char) class FileData(ctypes.Structure): _fields_=[ ("lenth",ctypes.c_longlong), ("data",CharPtr) ] FileDataPtr = ctypes.POINTER(FileData) def GetDll(): if platform.system().lower() == 'windows': isWinPlat_ = True else: isWinPlat_ = False currentPath_ = os.getcwd().replace('\\','/') if isWinPlat_: dll_ = ctypes.CDLL(currentPath_ + '/PyTest.dll') else: dll_ = ctypes.CDLL(currentPath_ + '/PyTest.so') return dll_ pytestdll_ = GetDll() def GetDataS(): getDatasF = pytestdll_.GetDatas getDatasF.argtypes=[ctypes.POINTER(ctypes.c_longlong)] getDatasF.restype = ctypes.c_char_p lengths = ctypes.c_longlong(0) ret = getDatasF(ctypes.byref(lengths)) print(lengths.value) print(ret) GetDataS()
到此這篇關(guān)于Python獲取C++中返回的char*字段的兩種思路的文章就介紹到這了,更多相關(guān)Python獲取C++返回char*字段內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python回調(diào)函數(shù)中使用多線程的方法
這篇文章主要介紹了python回調(diào)函數(shù)中使用多線程的方法,需要的朋友可以參考下2017-12-12keras-siamese用自己的數(shù)據(jù)集實現(xiàn)詳解
這篇文章主要介紹了keras-siamese用自己的數(shù)據(jù)集實現(xiàn)詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06python3之讀取redis數(shù)據(jù)帶有‘b’的問題
這篇文章主要介紹了python3之讀取redis數(shù)據(jù)帶有‘b’的問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09PyCharm 2020.2.2 x64 下載并安裝的詳細(xì)教程
這篇文章主要介紹了PyCharm 2020.2.2 x64 下載并安裝的詳細(xì)教程,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-10-10python實現(xiàn)分析apache和nginx日志文件并輸出訪客ip列表的方法
這篇文章主要介紹了python實現(xiàn)分析apache和nginx日志文件并輸出訪客ip列表的方法,涉及Python操作日志文件的技巧,非常具有實用價值,需要的朋友可以參考下2015-04-04解決安裝新版PyQt5、PyQT5-tool后打不開并Designer.exe提示no Qt platform plug
這篇文章主要介紹了解決安裝新版PyQt5、PyQT5-tool后打不開并Designer.exe提示no Qt platform plugin的問題,需要的朋友可以參考下2020-04-04python定時檢查某個進(jìn)程是否已經(jīng)關(guān)閉的方法
這篇文章主要介紹了python定時檢查某個進(jìn)程是否已經(jīng)關(guān)閉的方法,涉及Python進(jìn)程與時間的相關(guān)操作技巧,需要的朋友可以參考下2015-05-05