在Python

时间:2015-10-27 20:22:51

标签: python c++ error-handling callback char

我正在开发一个应该在Python中使用的DLL。我有一个回调函数来发送我的参数(在单独的标题中定义):

typedef int(*call_nBest)(char **OutList, float* confList, int nB);

所以,我正在以这种方式使用这个回调:

#define TEXT_BUFFER_MAX_SIZE 50
call_nBest nBestList;
void Xfunction(const char* aLineThatWillBeConvertedInAList){
    char **results;
    float *confidences;
    confidences=new float[nBest];
    results=new char*[nBest];
    for(int i=0; i<nBest; i++) results[i]=new char[TEXT_BUFFER_MAX_SIZE];

    MakeLine2List(aLineThatWillBeConvertedInAList,results,confidences); 

    /*At this function I am having the error :(*/
    nBestList(results,confidences,nBest); // Passing the values to my callback

    for(int i=0; i<nBest; i++) delete [] results[i];
    delete [] confidences;
    delete [] results;

}

我正以这种方式出口:

__declspec(dllexport) int ResultCallback(call_nBest theList){
    nBestList = theList;
    return(0);
}

我以这种方式在另一个C ++应用程序中首先测试了我的回调:

int MyCallback(char **OutLi, float* confLi, int nB){
    printf("\n The nB results: %d \n",nB);
    for(int n=0; n<nB; n++){
        std::cout << *(confLi+n) << "\t" << OutLi[n] << "\n";
    }
    return(0);
}

main()中,我以这种方式给出回调:

ResultCallback(MyCallback);

它运作良好。但我不知道如何适应Python。我试过这个:

注意:我改变了最后一种方式,因为我解决了一些错误,但我仍然遇到错误。这是我加载myDLL

的当前方式
from ctypes import *
def callbackU(OutList,ConList,nB):
    for i in range(nB):
        print(OutList[i][0:50]) #I don't know how to print the values
return 0

myDLL = cdll.LoadLibrary("MyLibrary.dll")

calling = CFUNCTYPE(c_int,POINTER(POINTER(c_char)),POINTER(c_float),c_int)
theCall= calling(callbackU)
myDLL.ResultCallback(theCall)

myDLL.StartProcess(); #In this process the given callback will be invoqued

错误

现在我有这个错误:

  

未处理的异常:System.AccessViolationException:尝试   读或写受保护的内存。这通常表明其他   记忆已腐败。在Xfunction(SByte *   aLineThatWillBeConvertedInAList)

     

问题签名:

     

问题事件名称:APPCRASH
   应用程序名称:python.exe
   应用版本:0.0.0.0
   申请时间戳:54f9ed12
   故障模块名称:MSVCR100.dll
   故障模块版本:10.0.40219.325
   故障模块时间戳:10.0.40219.325
   例外代码:c0000005
   异常抵消:00001ed7
   操作系统版本:6.3.9600.2.0.0.256.4
   地区ID:1033
   附加信息1:5861
   附加信息2:5861822e1919d7c014bbb064c64908b2
   附加信息3:a10f
   附加信息4:a10ff7d2bb2516fdc753f9c34fc3b069

我做过的事情几乎就是我想要的事情:

首先我更改了这个回调Python函数:

def callbackU(OutList,ConList,nB):
    for i in range(nB):
        print(i)
return 0

所有工作都没有错误,我可以在控制台中看到这一点(在这种情况下nB10):

0
1
...
9

其次,我改变了这个功能:

def callbackU(OutList,ConList,nB):
    for i in range(nB):
        print (cast(OutList,c_char_p))
return 0

并且,哦,这只打印列表中的第一个单词(nB次)

1 个答案:

答案 0 :(得分:3)

Do you want something like this?

def callbackU(OutList, ConList, nB):
    for i in range(nB):
        print("{}\t{}".format(ConList[i], cast(OutList[i], c_char_p)))
    return 0

From what I understand you're just trying to match the output of your Python callbackU function with your C++ MyCallback function.

Python has a variety of string formatting functionality that can be confusing at first, but pays homage to printf string formatting.

Since OutList has type LP_LP_c_char (pointer to pointer of c_char, vs "NULL terminated char *" c_char_p), we'd best turn it into a native Python data type like so:

def callbackU(OutList, ConList, nB):
    for i in range(nB):
        out_list_item = cast(OutList[i], c_char_p).value
        print("{}\t{}".format(ConList[i], out_list_item))
    return 0