Last active
August 29, 2015 14:11
-
-
Save Noitidart/dba9bc4b7572bd3349d1 to your computer and use it in GitHub Desktop.
_ff-addon-snippet-WinAPI_SendMessage - How to use SendMessage. [jsctypes] [winapi]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
README
Rev1
Taken from this topic here: http://forums.mozillazine.org/viewtopic.php?f=19&t=650630&p=12527153&hilit=ctypes#p12527153
The C version of this code looks like this (from topic linked above):
Rev2
WINAPI_
to file and gist nameRev3
readString
so if the title has any non-UTF8 characters it will fail, but thats a non-ctypes fix, just fix up readString to do manually or use readStringRepalceMalformed if you dont care about actually reading the bad chars.Rev4
Rev5