Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save madhawa-se/2c7afc284e94ce4aa919 to your computer and use it in GitHub Desktop.
Save madhawa-se/2c7afc284e94ce4aa919 to your computer and use it in GitHub Desktop.
_ff-addon-snippet-WINAPI_SendMessageWMCOPYDATA - How to use SendMessage. [jsctypes] [winapi]
Cu.import('resource://gre/modules/Services.jsm');
Cu.import('resource://gre/modules/ctypes.jsm');
if (ctypes.voidptr_t.size == 4 /* 32-bit */ ) {
var is64bit = false;
} else if (ctypes.voidptr_t.size == 8 /* 64-bit */ ) {
var is64bit = true;
} else {
throw new Error('huh??? not 32 or 64 bit?!?!');
}
var user32 = ctypes.open('user32.dll');
var SetCursorPos = user32.declare('SetCursorPos', ctypes.winapi_abi,
ctypes.bool, // return
ctypes.int, // x
ctypes.int // y
);
var POINT = ctypes.StructType('_tagPoint', [
{x: ctypes.long},
{y: ctypes.long}
]);
var LPPOINT = POINT.ptr;
var GetCursorPos = user32.declare('GetCursorPos', ctypes.winapi_abi,
ctypes.bool, // return
LPPOINT // lpPoint
);
var LONG_PTR = is64bit ? ctypes.int64_t : ctypes.long;
var LRESULT = LONG_PTR;
var UINT_PTR = is64bit ? ctypes.uint64_t : ctypes.unsigned_int;
var WPARAM = UINT_PTR;
var LPARAM = LONG_PTR;
var SendMessage = user32.declare('SendMessageW', ctypes.winapi_abi,
LRESULT, // return
ctypes.voidptr_t, // hWnd
ctypes.unsigned_int, // Msg
WPARAM, // wParam
LPARAM // lParam
);
var WM_GETTEXT = 0x000D;
var WM_GETTEXTLENGTH = 0x000E;
var DWORD = ctypes.uint32_t;
var LPDWORD = DWORD.ptr;
var WCHAR = ctypes.jschar
function doIt() {
var browserWindow = Services.wm.getMostRecentWindow('navigator:browser');
if (!browserWindow) {
throw new Error('No browser window found');
}
var baseWindow = browserWindow.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIWebNavigation)
.QueryInterface(Ci.nsIDocShellTreeItem)
.treeOwner
.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIBaseWindow);
var hToString = baseWindow.nativeHandle;
var hTo = ctypes.voidptr_t(ctypes.UInt64(hToString));
var rez_SM = SendMessage(hTo, 0x000E, 0, 0);
console.log('rez_SM:', rez_SM, rez_SM.toString());
var titleLength = parseInt(rez_SM.toString());
console.info('the title of the window WITHOUT null term is this long:', titleLength);
var buffer = WCHAR.array(titleLength+1)(); // because docs say we should allocate a buffer that is title length plus null term
var addressOfBuffer_asLPARAM = ctypes.cast(buffer.address(), LPARAM);
console.log('addressOfBuffer_asLPARAM:', addressOfBuffer_asLPARAM.toString());
var rez_SM = SendMessage(hTo, WM_GETTEXT, buffer.length, addressOfBuffer_asLPARAM);
console.log('rez_SM:', rez_SM, rez_SM.toString()); // return of SendMessage with WM_GETTEXT is the length of the stirng without the null term
console.log('buffer.readString()', buffer.readString());
}
doIt();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment