Created
September 14, 2011 15:54
-
-
Save anonymous/1216938 to your computer and use it in GitHub Desktop.
ctypes, C string & Python string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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