Skip to content

Instantly share code, notes, and snippets.

Created September 14, 2011 15:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/1216938 to your computer and use it in GitHub Desktop.
Save anonymous/1216938 to your computer and use it in GitHub Desktop.
ctypes, C string & Python string
#!/usr/bin/env python
from ctypes import *
from ctypes.util import find_library
libc = CDLL(find_library("libc"))
libc.free.argtypes = [ c_void_p ]
libc.free.restype = None
libc.strdup.argtypes = [ c_char_p ]
libc.strdup.restype = POINTER(c_char)
orig = "oh hai! im in ur memory"
cstr = libc.strdup(orig)
pstr = ""; i = 0
while cstr[i] != '\0': pstr = pstr + cstr[i]; i = i + 1
libc.free(cstr)
print "pstr=<%s>" % (pstr)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment