Skip to content

Instantly share code, notes, and snippets.

@yajd
Created February 26, 2014 23:42
Show Gist options
  • Save yajd/9241197 to your computer and use it in GitHub Desktop.
Save yajd/9241197 to your computer and use it in GitHub Desktop.
fgets alicjab version - ctypes works but its weird. copy paste this to scratchpad with environment set to browser
Cu.import("resource://gre/modules/ctypes.jsm");
var libc = ctypes.open("msvcrt.dll");
var FILE = new ctypes.StructType("FILE").ptr; //made var otherwise in scratchpad cant run multiple times, this can be CONST its no issue
var fopen = libc.declare("fopen", // symbol name
ctypes.default_abi, // cdecl calling convention
FILE, // return type (FILE*)
ctypes.char.ptr, // first arg (const char*)
ctypes.char.ptr); // second arg (const char*)
var fgets = libc.declare("fgets",
ctypes.default_abi,
ctypes.char.ptr,
ctypes.char.ptr,
ctypes.int32_t,
FILE);
var myfile = fopen("C:\\Users\\3K2KYC1\\Desktop\\FirefoxPortable\\newfile.in", "r");
var SIZE = 100;
var line = new ctypes.char(SIZE);
var ret = fgets(line.address(), SIZE, myfile);
//newfile.in already exists and its contents is "ABC" no quotes
//line - after running code line is set to "ctypes.char(65)" and 65 is character code of A but how do you read line?
//ret - after running code ret is set to "ctypes.char.ptr(ctypes.UInt64("0x1e70e808"))" this also makes no sense to me how do you get a string out of this?
Services.wm.getMostRecentWindow(null).alert(line);
Services.wm.getMostRecentWindow(null).alert(ret);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment