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'))
   

 

你可能感兴趣的:(Python,C++)