Search code examples
firefox-addonjsctypes

EnumProps on Windows not readable


I wrote up some code to enumerate properties of the current window. You can copy and paste this to scratchpad and run it. It's enumerating, but it's not readable strings.

Also it just enumerated 4 properties for the current firefox window, does that sound right or is that not all of them?

Code:

Cu.import('resource://gre/modules/ctypes.jsm');
var lib = {
  user32: ctypes.open('user32.dll')
}

var HANDLE = ctypes.size_t;
var HWND = ctypes.int32_t;
var BOOL = ctypes.bool;
var LPARAM = ctypes.size_t;
var LPCTSTR = ctypes.char.ptr;

var CallBackABI;
var WinABI;
if (ctypes.size_t.size == 8) {
  CallBackABI = ctypes.default_abi;
  WinABI = ctypes.default_abi;
} else {
  CallBackABI = ctypes.stdcall_abi;
  WinABI = ctypes.winapi_abi;
}

var PropEnumProc = ctypes.FunctionType(CallBackABI, BOOL, [HWND, LPCTSTR, HANDLE]);
var EnumProps = lib.user32.declare('EnumPropsW', WinABI, ctypes.int, HWND, PropEnumProc.ptr);

// code to run it

function doEnumProps(tHwnd) {
  var i = 0;
  var SearchPD = function(hwnd, lpctstr, hnd) {    
    console.log('i:', i)
    i++;

    //console.log('hwnd:', hwnd)
    console.log('lpctstr:', lpctstr.contents)
    console.log('hnd:', hnd.toString())
    return true; //let enum continue till nothing to enum
  }

  SearchPD_ptr = PropEnumProc.ptr(SearchPD);
  var wnd = LPARAM(0);
  console.time('EnumProps');
  //EnumProps(ctypes.cast(wnd.address(), LPARAM), SearchPD_ptr);
  EnumProps(tHwnd, SearchPD_ptr);
  console.timeEnd('EnumProps');

}

var tWin = window; //Services.wm.getMostRecentWindow(null); // tWin means target_window
var tBaseWin = tWin.QueryInterface(Ci.nsIInterfaceRequestor)
                    .getInterface(Ci.nsIWebNavigation)
                    .QueryInterface(Ci.nsIDocShellTreeItem)
                    .treeOwner.QueryInterface(Ci.nsIInterfaceRequestor)
                    .getInterface(Ci.nsIBaseWindow);
var cHwnd = ctypes.int32_t(ctypes.UInt64(tBaseWin.nativeHandle));

console.time('doEnumProps');
var pids = doEnumProps(cHwnd);
console.timeEnd('doEnumProps');

for (var l in lib) {
  lib[l].close();
}

Solution

  • Fixed by making it var LPCTSTR = ctypes.jschar.ptr; instead of ctypes.char.ptr

    and doing console.log('lpctstr:', lpctstr.readString()) the readString instead of contents