ok i had some time so i played around with it and i need your help fixing it.
this is your version i fixed it up. copy paste this to scratchpad set it to environment > browser and run it
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);
Services.wm.getMostRecentWindow(null).alert(line);
Services.wm.getMostRecentWindow(null).alert(ret);
Results of this test:
- 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?
Then I changed some stuff up and made it my way but I'm having similar problems i cant read more than one character.
Cu.import("resource://gre/modules/ctypes.jsm");
var libc = ctypes.open("msvcrt.dll");
var struct_FILE = new ctypes.StructType("FILE"); //set to var only so can run multiple times in scratchpad, can change back to const
var fopen = libc.declare("fopen", // symbol name
ctypes.default_abi, // cdecl calling convention
struct_FILE.ptr, // return type (FILE*)
ctypes.char.ptr, // first arg (const char*)
ctypes.char.ptr); // second arg (const char*)
var fgetc = libc.declare("fgetc", // symbol name
ctypes.default_abi, // cdecl calling convention
ctypes.int.ptr, // return type (FILE*)
struct_FILE.ptr); // second arg (const char*)
var fgets = libc.declare("fgets", // symbol name
ctypes.default_abi, // cdecl calling convention
ctypes.jschar.ptr, // return char pointer
ctypes.jschar.ptr, // first arg char pointer
ctypes.int, //second arg int
struct_FILE.ptr); // third arg FILE pointer
var myfile = fopen("C:\\Users\\3K2KYC1\\Desktop\\FirefoxPortable\\newfile.in", "r");
//Services.wm.getMostRecentWindow(null).alert(myfile);
var line = new ctypes.jschar;
var ret = fgets(line.address(), 2, myfile);
Services.wm.getMostRecentWindow(null).alert(line);
Services.wm.getMostRecentWindow(null).alert(ret);