Skip to content

Instantly share code, notes, and snippets.

@gfhuertac
Created October 23, 2019 00:43
Show Gist options
  • Save gfhuertac/cdecba63b6c03270af8895607b680785 to your computer and use it in GitHub Desktop.
Save gfhuertac/cdecba63b6c03270af8895607b680785 to your computer and use it in GitHub Desktop.
var ffi = require('ffi');
var ref = require('ref');
var struct = require('ref-struct');
var winapi = {};
winapi.bool = ref.types.bool;
winapi.int = ref.types.int;
winapi.ulong = ref.types.ulong;
winapi.void = ref.types.void;
winapi.PVOID = ref.refType(winapi.void);
winapi.HANDLE = winapi.PVOID;
winapi.HWND = winapi.HANDLE;
winapi.WCHAR = ref.types.char;
winapi.LPCWSTR = ref.types.CString;
winapi.UINT = ref.types.uint;
var ACCESS_MASK = ref.types.uint;
var SECURITY_ATTRIBUTES = struct('SECURITY_ATTRIBUTES', {
'nLength': 'ulong',
'lpSecurityDescriptor': winapi.PVOID,
'bInheritHandle': 'bool'
});
var LPSECURITY_ATTRIBUTES = ref.refType(SECURITY_ATTRIBUTES);
var STARTUPINFO = struct('STARTUPINFO', {
'cb': 'ulong',
'lpReserved': 'string',
'lpDesktop': 'string',
'lpTitle': 'string',
'dwX': 'ulong',
'dwY': 'ulong',
'dwXSize': 'ulong',
'dwYSize': 'ulong',
'dwXCountChars': 'ulong',
'dwYCountChars': 'ulong',
'dwFillAttribute': 'ulong',
'dwFlags': 'ulong',
'wShowWindow': 'short',
'cbReserved2': 'short',
'lpReserved2': 'IntPtr',
'hStdInput': winapi.HWND,
'hStdOutput': winapi.HWND,
'hStdError': winapi.HWND
});
var LPSTARTUPINFO = ref.refType(STARTUPINFO);
var kernel32 = ffi.Library('kernel32', {
'ExpandEnvironmentStringsA': [
'void', [ 'string', 'string', 'uint' ]
],
'GetCurrentThreadId': [
'ulong', []
],
'GetLastError': [
'ulong', []
],
'WinExec': [
'int32', [ 'string' ]
]
});
var user32 = ffi.Library('user32', {
'CloseDesktop': [
'bool', [ 'int32' ]
],
'CreateDesktopA': [
winapi.PVOID, [ 'string', 'string', 'pointer', 'ulong', ACCESS_MASK, LPSECURITY_ATTRIBUTES ]
],
'GetThreadDesktop': [
winapi.PVOID, [ 'ulong' ]
],
'MessageBoxA': [
'int32', [ 'int32', 'string', 'string', 'int32' ]
],
'OpenDesktopA': [
winapi.PVOID, [ 'string', 'ulong', 'bool', ACCESS_MASK ]
],
'OpenInputDesktop': [
winapi.PVOID, [ 'ulong', 'bool', ACCESS_MASK ]
],
'SetThreadDesktop': [
'bool', [ winapi.PVOID ]
],
'SwitchDesktop': [
'bool', [ winapi.PVOID ]
],
});
var DESKTOP_SWITCHDESKTOP = 256;
var GENERIC_ALL = 983551;
var MAX_PATH = 260;
var explorer_path = Buffer.alloc(MAX_PATH);
explorer_path.type = ref.types.char;
kernel32.ExpandEnvironmentStringsA("%windir%\\explorer.exe", explorer_path, MAX_PATH-1);
console.log(explorer_path.toString())
var hdeskOriginalThread = user32.GetThreadDesktop(kernel32.GetCurrentThreadId());
var hdeskOriginalInput = user32.OpenInputDesktop(0, false, DESKTOP_SWITCHDESKTOP);
var desktopHandle = user32.OpenDesktopA('lila', 0, false, GENERIC_ALL);
if (desktopHandle == 0)
desktopHandle = user32.CreateDesktopA('lila', null, null, 0, GENERIC_ALL, null);
console.log("Desktop handle:: ", desktopHandle);
var res1 = user32.SetThreadDesktop(desktopHandle);
console.log("SetThreadDesktop result:: ", res1);
var res2 = user32.SwitchDesktop(desktopHandle);
console.log("SwitchDesktop result:: ", res2);
user32.MessageBoxA(0, "sss", "sss", 0);
var res3 = user32.SwitchDesktop(hdeskOriginalInput);
console.log("SwitchDesktop result:: ", res3);
var res4 = user32.SetThreadDesktop(hdeskOriginalThread);
console.log("SetThreadDesktop result:: ", res4);
/*
console.log('running explorer');
var startup_info = STARTUPINFO();
startup_info.cb = sizeof(startup_info);
startup_info.lpDesktop = desktop_name;
//We need to create an explorer.exe in the context of the new desktop for start menu, etc
CreateProcessA(explorer_path, NULL, NULL, NULL, FALSE, 0, NULL, NULL, &startup_info, &process_info);
*/
var res3 = user32.CloseDesktop(desktopHandle);
console.log("CloseDesktop result:: ", res3);
if (!res3) {
var res4 = kernel32.GetLastError();
console.log("Last error code:: ", res4);
}
{
"name": "w32a",
"version": "1.0.0",
"description": "Demo for win32-api",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"ffi": "github:lxe/node-ffi#node-12",
"ref": "github:lxe/ref#node-12",
"ref-struct": "github:lxe/ref-struct#node-12",
"win32-def": "^3.3.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment