我使用的是基于C++的库,返回类型是char指针。我从Python调用这个C++函数。你知道吗
>我的问题是,我能删除从Python中分配的C++函数中的内存吗?你知道吗Python代码
lib.TagGetName.restype = c_char_p
lib.TagGetName.argtypes=[c_int]
data = 1
cmt = (b"this is comment for Voltage")
result = lib.TagGetName(data)
Python输出
Kernel process terminated for restart. (0)
Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:43:06) on Windows (32 bits).
This is the IEP interpreter.
Type 'help' for help, type '?' for a list of *magic* commands.
运行脚本:“D:\QTDev\测试.py““
1
SELECT sTagName FROM TagDataInfo where iTagId = 1;
Return value--> b'Voltage\xfd\xfd\xfd\xfd\xdd`\x18x'
print ("Return value-->" , result)
C++函数代码
USE_MATH char* TagGetName(int iTagId)
{
char* cName;
//string cName = "";
sqlite3_stmt *stmtCreate = NULL;
sqlite3 *m_pdbObj;
cout<<iTagId<<endl;
if (sqlite3_open_v2("D:\\QTDev\\forPythonDLL\\Neha_Test_Use\\TagData", &m_pdbObj, SQLITE_OPEN_READWRITE, NULL) != SQLITE_OK)
{
return cName;
}
char cQuery[1024] = {0};
sprintf(cQuery,"SELECT sTagName FROM TagDataInfo where iTagId = %d;",iTagId);
cout<<cQuery<<endl;
if (sqlite3_prepare_v2(m_pdbObj, cQuery, -1, &stmtCreate, 0) == SQLITE_OK)
{
sqlite3_step(stmtCreate);
if(sqlite3_column_text(stmtCreate, 0))
{
cName = new char[strlen((char*)sqlite3_column_text(stmtCreate, 0))+1];
memset(cName,0x00,(strlen((char*)sqlite3_column_text(stmtCreate, 0))+1));
memcpy(cName,(char*)sqlite3_column_text(stmtCreate, 0),strlen((char*)sqlite3_column_text(stmtCreate, 0)));
cout<<"Cname="<<cName<<endl;
cout<<"Database Name="<<(char*)sqlite3_column_text(stmtCreate, 0)<<endl;
}
sqlite3_finalize(stmtCreate);
}
else
{
return cName;
}
sqlite3_exec(m_pdbObj, "COMMIT", 0, 0, 0);
return cName;
}
作为一个旁白,请记住Python正在使用UTF-16,C++可能使用UTF-8,并且需要一些字符串转换(cType可以正确地执行)
相关问题 更多 >
编程相关推荐