从Python传递函数参数到C++

2024-09-29 20:24:29 发布

您现在位置:Python中文网/ 问答频道 /正文

如何从Python到C++的DLL传递多个参数,如^ {CD1>},^ {CD2>}。我已经试过以下方法了

但这对我没用。。。在

import ctypes
from ctypes import Structure, c_int, c_double, windll
from ctypes import *
from ctypes import wintypes
from collections import namedtuple


lib = cdll.LoadLibrary("F:\\QT SCADA\\forPythonDLL\\Neha_Test\\Debug\\Neha_Test.dll")
print("hello")

class Box(Structure):
    _fields_ = [
    ("length", c_int),
    ("breadth", c_int),
    ("height", c_int)]


print ("-"*30)

p = "Voltage"
t = "245"

#lib.TagSetInfo.restype = ctypes.c_int
Volt = "voltage"

Voltage = POINTER(Volt)
Value = '532' 
#Voltage=ctypes.c_char_p

iUnit = lib.TagSetInfo(Voltage,Value) 

Python输出如下-->

^{pr2}$

C++ [DLL]函数代码如下:

extern "C" USE_MATH int TagSetInfo(char *cName, char *cValue)
{
    cout << "Database is : " << cName<<cValue <<endl;
    int iUnit = 0;
    sqlite3_stmt               *stmtCreate = NULL;
    string                      strCreateSQL;
    sqlite3                     *m_pdbObj;

  if (sqlite3_open_v2("F:\\QT SCADA\\forPythonDLL\\Neha_Test_Use\\TagData", &m_pdbObj, SQLITE_OPEN_READWRITE, NULL) != SQLITE_OK)
    {
        //GLOGERR(L"Error opening new database in Database::CreateDB");
        return 0;
    } 

   bool bResult =true;
   char cQuery[1024] = {0};
   //update TagDataInfo set iValue= 123 where sTagName = "Voltage"; 
   iUnit = atoi(cValue);
   sprintf(cQuery,"update TagDataInfo set iValue = %d where sTagName = '%s';",iUnit,cName);
    strCreateSQL.append(cQuery);

    if (sqlite3_prepare_v2(m_pdbObj, cQuery, -1, &stmtCreate, 0) == SQLITE_OK)
        {
            int nSQLiteCode = sqlite3_step(stmtCreate);

            /*if(sqlite3_column_text(stmtCreate, 0))
            {
                iUnit = atoi((char*)sqlite3_column_text(stmtCreate, 0));
            }*/
            sqlite3_finalize(stmtCreate);
        }
        else 
        {
            return 0; 
        }

    sqlite3_exec(m_pdbObj, "COMMIT", 0, 0, 0);
    return iUnit;
}

Tags: fromtestimportlibctypessqlite3intvoltage

热门问题