0xCraft
0xCraft

Reputation: 11

How to set value of argument using python callback for C program

My Python code, there is a callback function for C:

global cbGetIMSI
CB_GET_IMSI  = CFUNCTYPE(None, c_char_p)
cbGetIMSI   = CB_GET_IMSI(py_getIMSI)
def py_getIMSI(imsi):
    global tc
    tc  = import(mod)
    imsi = tc.getIMSI()
    print '####### imsi = ' + imsi

and call C function to register:

lib_inf = cdll.LoadLibrary(self.libinf.get())
lib_inf.inf_regCbGetImsi(cbGetIMSI)

My C Code:

typedef void (*inf_PyCbGetImsi)(char *);
int inf_regCbGetImsi(inf_PyCbGetImsi cbFn)
{
    DBG("enter [%s()]", FUNCTION);
    if (!cbFn)
    {
        return -1;
    }
    g_pyCB.getImsi = cbFn;
    return 0;
}

static int GetIMSI()
{
    int ret = 0;
    int i = 0;
    unsigned char aIMSI[15];
    DBG("[enter %s()]", __FUNCTION__);

    memset(aIMSI, 0, sizeof(aIMSI));
    if (g_pyCB.getImsi)
    {
        g_pyCB.getImsi(aIMSI);

    }
    DBG("[%s()] aIMSI = [%s]", __FUNCTION__, aIMSI);  

    return ret;
}

Run log in Python:

####### imsi = 001010123456789
in C Program:
[GetIMSI()] aIMSI = []

Why has the value of the argument not changed, and how would I fix it? Thanks!

Upvotes: 1

Views: 177

Answers (2)

0xCraft
0xCraft

Reputation: 11

ref to:Using ctypes to write callbacks that pass-in pointer to pointer parameters to be used as output-parameter

def like this : CFUNCTYPE(c_byte, POINTER(POINTER(c_char)))

Upvotes: 0

KokaKiwi
KokaKiwi

Reputation: 605

The problem is in your line:

imsi = tc.getIMSI()

Here you don't set the value of imsi, you just re-define it as another "something", which is a behaviour you would have in C too.

If you want to set the value of imsi, you should copy the value in it by using something like strcpy, which is usable by loading the libc and using something like libc.strcpy(imsi, tc.getIMSI())

Upvotes: 1

Related Questions