Skip to content

Instantly share code, notes, and snippets.

@Noitidart
Created January 24, 2015 23:14
Show Gist options
  • Save Noitidart/5b1bfb7015b8b87dd4cd to your computer and use it in GitHub Desktop.
Save Noitidart/5b1bfb7015b8b87dd4cd 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();
}
@Noitidart
Copy link
Author

README

Rev1

  • Works - hides window from taskbar also
  • Following X11_Base structure/abstraction layer
  • Important note, I may need to hook the window to keep it hidden, apparently windows receive messages a lot and it will be shown again after some time, so instead of continually sending the hide message we have to hook the window with SetWindowHookEx and watch for message of WM_WINDOWPOSCHANGING and apply the hide again to ensure it reamins hidden

@Noitidart
Copy link
Author

Same code as Rev1 just with the abstraction layer removed:

Cu.import('resource://gre/modules/ctypes.jsm')
var user32 = ctypes.open('user32.dll');
var TYPES = {
    BOOL: ctypes.bool,
    HWND: ctypes.voidptr_t,
    INT: ctypes.int
};
var CONSTS = {
    SW_HIDE: 0,
    SW_SHOW: 5
};
var ShowWindow = user32.declare('ShowWindow', ctypes.winapi_abi, TYPES.BOOL, TYPES.HWND, TYPES.INT);


    // 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 = ShowWindow(hwnd, CONSTS.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');
    }

user32.close();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment