Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Happy-Ferret/2dd806a75e91b4d017cbac8b95373719 to your computer and use it in GitHub Desktop.
Save Happy-Ferret/2dd806a75e91b4d017cbac8b95373719 to your computer and use it in GitHub Desktop.
_ff-addon-snippet-WinAPI_ShellExec - Launch things without waiting for response. [winapi] [jsctypes]
Cu.import('resource://gre/modules/ctypes.jsm')
var wintypesInit = function() {
var ifdef_UNICODE = true;
this.WINABI = ctypes.winapi_abi; //should do 64bit testing
// BASIC TYPES (ones that arent equal to something predefined by me)
this.BOOL = ctypes.bool;
this.CHAR = ctypes.char;
this.DWORD = ctypes.unsigned_long;
this.INT = ctypes.int;
this.LPVOID = ctypes.voidptr_t;
this.PVOID = ctypes.voidptr_t
this.UINT = ctypes.unsigned_int;
this.ULONG = ctypes.unsigned_long;
this.WCHAR = ctypes.jschar;
// ADV
this.HANDLE = this.PVOID;
this.LPCSTR = this.CHAR.ptr; // typedef __nullterminated CONST CHAR *LPCSTR;
this.LPCWSTR = this.WCHAR.ptr;
// SUPER ADV
this.HINSTANCE = this.HANDLE;
this.HKEY = this.HANDLE;
this.HWND = this.HANDLE;
this.LPCTSTR = ifdef_UNICODE ? this.LPCWSTR : this.LPCSTR;
/* typedef struct _SHELLEXECUTEINFO {
* DWORD cbSize;
* ULONG fMask;
* HWND hwnd;
* LPCTSTR lpVerb;
* LPCTSTR lpFile;
* LPCTSTR lpParameters;
* LPCTSTR lpDirectory;
* int nShow;
* HINSTANCE hInstApp;
* LPVOID lpIDList;
* LPCTSTR lpClass;
* HKEY hkeyClass;
* DWORD dwHotKey;
* union {
* HANDLE hIcon;
* HANDLE hMonitor;
* } DUMMYUNIONNAME;
* HANDLE hProcess;
* } SHELLEXECUTEINFO, *LPSHELLEXECUTEINFO;
*/
this.SHELLEXECUTEINFO = ctypes.StructType('_SHELLEXECUTEINFO', [
{ 'cbSize': this.DWORD },
{ 'fMask': this.ULONG },
{ 'hwnd': this.HWND },
{ 'lpVerb': this.LPCTSTR },
{ 'lpFile': this.LPCTSTR },
{ 'lpParameters': this.LPCTSTR },
{ 'lpDirectory': this.LPCTSTR },
{ 'nShow': this.INT },
{ 'hInstApp': this.HINSTANCE },
{ 'lpIDList': this.LPVOID },
{ 'lpClass': this.LPCTSTR },
{ 'hkeyClass': this.HKEY },
{ 'dwHotKey': this.DWORD },
{ 'hIcon': this.HANDLE }, // union {HANDLE hIcon; HANDLE hMonitor;} DUMMYUNIONNAME; // i picked hIcon because i might be able to get winxp to seperate its groups ia
{ 'hProcess': this.HANDLE }
]);
// CONSTANTS
this.SPI_GETSCREENSAVERRUNNING = 0x0072;
}
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
CoInitializeEx: function() {
/* http://msdn.microsoft.com/en-us/library/windows/desktop/ms695279%28v=vs.85%29.aspx
* HRESULT CoInitializeEx(
* __in_opt_ LPVOID pvReserved,
* __in_ DWORD dwCoInit
* );
*/
return _lib('Ole32.dll').declare('CoInitializeEx', ostypes.WINABI,
ostypes.HRESULT, // result
ostypes.LPVOID, // pvReserved
ostypes.DWORD // dwCoInit
);
},
CoUninitialize: function() {
/* http://msdn.microsoft.com/en-us/library/windows/desktop/ms688715%28v=vs.85%29.aspx
* void CoUninitialize(void);
*/
return _lib('Ole32.dll').declare('CoUninitialize', ostypes.WINABI,
ostypes.VOID // return
);
},
ShellExecuteEx: function() {
/* https://msdn.microsoft.com/en-us/library/windows/desktop/bb762154%28v=vs.85%29.aspx
* BOOL ShellExecuteEx(
* __inout_ SHELLEXECUTEINFO *pExecInfo
* );
*/
return _lib('shell32.dll').declare('ShellExecuteExW', ostypes.WINABI,
ostypes.BOOL, // return
ostypes.SHELLEXECUTEINFO.ptr // *pExecInfo
);
}
}
// end - predefine your declares here
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
try {
//var rez_CoInitializeEx = ostypes.API('CoInitializeEx')(null, 0x2 | 0x4);
//console.info('rez_CoInitializeEx:', rez_CoInitializeEx.toString(), uneval(rez_CoInitializeEx));
//if (ctypes.winLastError != 0) { console.error('Failed rez_CoInitializeEx, winLastError:', ctypes.winLastError); }
var fullPath = '\\';
var sei = ostypes.SHELLEXECUTEINFO();
sei.cbSize = ostypes.SHELLEXECUTEINFO.size;
var cStr = ostypes.LPCTSTR.targetType.array()(fullPath)
sei.lpFile = cStr;
//sei.lpVerb = ostypes.TYPE.LPCTSTR.targetType.array()('open');
sei.nShow = 1; //ostypes.SW_SHOWNORMAL;
var rez_ShellExecuteEx = _dec('ShellExecuteEx')(sei.address());
console.info('rez_ShellExecuteEx:', rez_ShellExecuteEx.toString(), uneval(rez_ShellExecuteEx));
if (ctypes.winLastError != 0) { console.error('Failed rez_ShellExecuteEx, winLastError:', ctypes.winLastError); }
console.info('sei:', sei.toString());
console.log('sei.hInstApp:', sei.hInstApp.toString());
} finally {
//var rez_CoUninitialize = ostypes.API('CoUninitialize')();
}
}
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