Skip to content Skip to sidebar Skip to footer

Ff Addon: How To Declare C Function Fgets In Javascript

I'm developing firefox plugin and I want to read file using WinApi. I manage to connect with WinApi and it works fine. var lib = ctypes.open('user32.dll'); var msgBox = lib.declar

Solution 1:

heres an example of a bunch of different ctype declarations: https://builder.addons.mozilla.org/package/161408/latest/

if you still cant figure it out post it up and ill try to get fget working.

but do with namier recommended, use the cross platform options thats best, but for leearning sake we can do this fget when i have some time if you dont figure it out

Solution 2:

So here is my solution. I decided to use getc function instead of fgets and I manage to read file.

Components.utils.import("resource://gre/modules/ctypes.jsm");
var libc = ctypes.open("msvcrt.dll");

// int getc(FILE *stream);var getc = libc.declare("getc",
                        ctypes.default_abi,
                        ctypes.int32_t,
                        FILE);
var file = fopen("newfile.in", "w");
var readFile = readFileToString(file)

functionreadFileToString(file) {
  var result = '';
  var c = 0;
  while((c = getc(file)) != -1) {
    result += String.fromCharCode(c); 
  }
  console.log("result = " + result);
  return result;
}

Solution 3:

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");
varlibc= ctypes.open("msvcrt.dll");

varFILE=newctypes.StructType("FILE").ptr; //made var otherwise in scratchpad cant run multiple times, this can be CONST its no issuevarfopen= 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*)varfgets= libc.declare("fgets",                    
                        ctypes.default_abi,           
                        ctypes.char.ptr,              
                        ctypes.char.ptr,              
                        ctypes.int32_t,               
                        FILE);          


varmyfile= fopen("C:\\Users\\3K2KYC1\\Desktop\\FirefoxPortable\\newfile.in", "r");

varSIZE=100;
varline=newctypes.char(SIZE);
varret= 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");
varlibc= ctypes.open("msvcrt.dll");


varstruct_FILE=newctypes.StructType("FILE"); //set to var only so can run multiple times in scratchpad, can change back to constvarfopen= 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*)varfgetc= libc.declare("fgetc", // symbol name
    ctypes.default_abi, // cdecl calling convention
    ctypes.int.ptr, // return type (FILE*)
    struct_FILE.ptr); // second arg (const char*)varfgets= 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 pointervarmyfile= fopen("C:\\Users\\3K2KYC1\\Desktop\\FirefoxPortable\\newfile.in", "r");
//Services.wm.getMostRecentWindow(null).alert(myfile);varline=newctypes.jschar;
varret= fgets(line.address(), 2, myfile);

Services.wm.getMostRecentWindow(null).alert(line);
Services.wm.getMostRecentWindow(null).alert(ret);
  • newfile.in already exists and its contents is "ABC" no quotes
  • using size 2 as 2nd arg on line 30
  • line - is set to "ctypes.jschar("A")" no quotes, which is the first char in the file, but i said get 2 char why didnt it give first 2 chars if try 3 it gives something weird
  • ret - is set to "ctypes.jschar.ptr(ctypes.UInt64("0xfba2b32"))"

  • if use size of 3

  • line - "ctypes.jschar("\u4241")"
  • ret - "ctypes.jschar.ptr(ctypes.UInt64("0x8fefce8"))"

Post a Comment for "Ff Addon: How To Declare C Function Fgets In Javascript"