将函数参数从Python传递给C ++

时间:2015-08-24 04:30:22

标签: python c++ ctypes

如何将多个参数(如intfloat)从Python传递到基于C ++的DLL。我已经尝试过以下方式

但它不适合我...

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输出如下 - >

 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.
Running script: "C:\Users\Administrator\Desktop\test.py"
hello
------------------------------
Traceback (most recent call last):
 File "C:\Users\Administrator\Desktop\test.py", line 40, in <module>
  iUnit = lib.TagSetInfo(Voltage,Value)
ctypes.ArgumentError: argument 1: <class 'TypeError'>: Don't know how to     convert parameter 1

>>> 

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;
}

0 个答案:

没有答案