Python 調用C++,返回char*

 

C++代碼

int Test( 
        const char*             input,
        char**                  output)
    {
        std::string str("From C++") 
        strcpy (*output,str.c_str());
        output[str.size()] = '\0';
        return 0;
    }

Python代碼

 

        strtemp = c_char_p()

        strtemp.value = b'' 
# 如果沒有這一行,出現錯誤:
# OSError: exception: access violation writing 0x0000000000000000
        
        print('111111')
        print(strtemp)
        print(byref(strtemp))
        print('111111')
        
        self.mydll.Test.restype = c_int
        accnum = '123'
        ret = self.mydll.Test(accnum.encode('utf-8'), byref(strtemp))
        print('ret=', ret)
        print('python strtemp=', strtemp.value.decode('utf-8'))
   

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章