| FAQ Index - Search - Recent Changes - Everything - Add entry |
23.38. How can I access data returned with a gpointer?
In some cases, for example in a callback function, you may get data as a gpointer instead of a python data type, making such data unreacheable from your python program.
Although the gpointer data type provides no way of accesing the underlying pointer, there is a hack to do it using the gpointer's conversion to string and reach the data using the ctypes module.
Have a look at the following example, using the gtkmozembed widget. This function is the callback for the 'open_uri' widget signal. The second argument 'uri' should be a string, but all you'll get is a gpointer. The gpointer is converted to string to get the pointer value in [13:-1] characters as an hex number, which may be converted to an integer. Using ctypes this integer is typecasted to a pointer to c style string (c_char_p), which is converted to a python string by the value method.
def mozilla_open_uri(widget, uri, *args):
p=int(str(uri)[13:-1],16)
url=ctypes.cast(p,ctypes.c_char_p).value
print(url)
return False
