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 Happy-Ferret/2451c10a5bb091196ed65b601c45bfda to your computer and use it in GitHub Desktop.
Save Happy-Ferret/2451c10a5bb091196ed65b601c45bfda to your computer and use it in GitHub Desktop.
_ff-addon-snippet-WinAPI-ShowWindowHide - Using js-ctypes and WinAPI method of ShowWindow to hide a window. [windows] [jsctypes]
Cu.import('resource://gre/modules/ctypes.jsm')
var wintypesInit = function() {
// BASIC TYPES (ones that arent equal to something predefined by me)
this.BOOL = ctypes.bool;
this.HWND = ctypes.voidptr_t;
this.INT = ctypes.int;
// CONSTANTS
this.SW_HIDE = 0;
this.SW_SHOW = 5;
}
var ostypes = new wintypesInit();
// start - skeleton, shouldn't have to edit
var lib = {};
function _lib(path) {
//ensures path is in lib, if its in lib then its open, if its not then it adds it to lib and opens it. returns lib
//path is path to open library
//returns lib so can use straight away
if (!(path in lib)) {
//need to open the library
//default it opens the path, but some things are special like libc in mac is different then linux or like x11 needs to be located based on linux version
switch (path) {
default:
try {
lib[path] = ctypes.open(path);
} catch (e) {
console.error('Integration Level 1: Could not get open path:', path, 'e:' + e);
throw new Error('Integration Level 1: Could not get open path:"' + path + '" e: "' + e + '"');
}
}
}
return lib[path];
}
var dec = {};
function _dec(declaration) { // it means ensureDeclared and return declare. if its not declared it declares it. else it returns the previously declared.
if (!(declaration in dec)) {
dec[declaration] = preDec[declaration](); //if declaration is not in preDec then dev messed up
}
return dec[declaration];
}
// end - skeleton, shouldn't have to edit
// start - predefine your declares here
var preDec = { //stands for pre-declare (so its just lazy stuff) //this must be pre-populated by dev // do it alphabateized by key so its ez to look through
ShowWindow: function() {
/* https://msdn.microsoft.com/en-us/library/windows/desktop/ms633548%28v=vs.85%29.aspx
* BOOL WINAPI ShowWindow(
* __in_ HWND hWnd,
* __in_ int nCmdShow
* );
*/
return _lib('user32.dll').declare('ShowWindow', ctypes.winapi_abi,
ostypes.BOOL, // return
ostypes.HWND, // hWnd
ostypes.INT // nCmdShow
);
}
}
// end - predefine your declares here
// start - helper functions
// end - helper functions
function shutdown() {
// do in here what you want to do before shutdown
for (var l in lib) {
lib[l].close();
}
}
function main() {
//do code here
// get hwnd
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 hwndString = baseWindow.nativeHandle;
var hwnd = ctypes.voidptr_t(ctypes.UInt64(hwndString));
// end get hwnd
var rez_ShowWindow = _dec('ShowWindow')(hwnd, ostypes.SW_HIDE);
console.info('rez_ShowWindow:', rez_ShowWindow, rez_ShowWindow.toString(), uneval(rez_ShowWindow));
if (rez_ShowWindow == true) {
console.log('window succesfully hidden');
} else if (rez_ShowWindow == false) {
console.warn('window failed to hide, it may already be hidden');
} else {
throw new Error('ShowWindow returned not false or true, this should never happen, if it did it should crash');
}
}
try {
main();
} catch(ex) {
throw ex;
} finally {
shutdown();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment