I call C function in python using ctypes,but Python can't get correct address passed by C function.
import ctypes
if __name__ == '__main__':
api = ctypes.CDLL('path_to_my_so')
api.CreateSDK.restype = ctypes.c_void_p
context = api.CreateSDK()
print (context)
print (ctypes.c_void_p(context).value)
api.Detect(context)
#include "sdk.h"
#include <iostream>
using namespace std;
class SDK{
public:
SDK():data(100){};
int data;
};
ModelHandle CreateSDK(){
ModelHandle sdk = new SDK();
cerr << "address of sdk to give:" << sdk << endl;
return sdk;
}
void Detect(ModelHandle sdk){
cerr << "address of given sdk:" << sdk << endl;
cerr << static_cast<SDK*>(sdk)->data << endl;
}
#pragma once
extern "C"{
typedef void* ModelHandle;
ModelHandle CreateSDK();
void Detect(ModelHandle sdk);
}
I excpet address output by C are the same,but here is the thing I got:
94731076196768
94731076196768
address of sdk to give:0x56284c2559a0
address of given sdk:0x4c2559a0