Created
June 13, 2018 05:18
-
-
Save doxas/a88588059c83cdc9f7612d379397b8d5 to your computer and use it in GitHub Desktop.
inline c# spawn
This file contains hidden or 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
/** | |
* Windows のプロセス ID を受け取り対象プロセスのウィンドウを前面に出す | |
* @param {number} pid - process id | |
*/ | |
function setForegroundWindowFromProcess(pid){ | |
let className = 'hoge' + Date.now(); | |
let source = ` | |
Add-Type @" | |
using System; | |
using System.Diagnostics; | |
using System.Runtime.InteropServices; | |
using System.Text; | |
public class ${className} { | |
[DllImport("user32")] | |
private static extern bool EnumWindows(WNDENUMPROC lpEnumFunc, IntPtr lParam); | |
private delegate bool WNDENUMPROC(IntPtr hWnd, IntPtr lParam); | |
[DllImport("user32")] | |
private static extern bool IsWindowVisible(IntPtr hWnd); | |
[DllImport("user32", CharSet = CharSet.Auto)] | |
private static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount); | |
[DllImport("user32")] | |
private static extern int GetWindowThreadProcessId(IntPtr hWnd, out int lpdwProcessId); | |
[DllImport("user32")] | |
private static extern bool SetForegroundWindow(IntPtr hWnd); | |
public static void Main(){ | |
EnumWindows(EnumerateWindow, IntPtr.Zero); | |
} | |
private static bool EnumerateWindow(IntPtr hWnd, IntPtr lParam){ | |
if(IsWindowVisible(hWnd)){ | |
PrintCaptionAndProcess(hWnd); | |
} | |
return true; | |
} | |
private static void PrintCaptionAndProcess(IntPtr hWnd){ | |
StringBuilder caption = new StringBuilder(0x1000); | |
GetWindowText(hWnd, caption, caption.Capacity); | |
Console.Write("'{0}' ", caption); | |
int processId; | |
GetWindowThreadProcessId(hWnd, out processId); | |
Process p = Process.GetProcessById(processId); | |
if(processId == ${pid}){ | |
Console.Write("(^o^)"); | |
SetForegroundWindow(hWnd); | |
} | |
Console.Write("({0})", p.ProcessName); | |
Console.WriteLine("[{0}]", processId); | |
} | |
} | |
"@ | |
[${className}]::main() | |
`; | |
let ps = child_process.spawn('powershell', ['-Command', source]); | |
ps.stdout.on('data', (data) => { | |
console.log(`stdout: ${data}`); | |
}); | |
ps.stderr.on('data', (data) => { | |
console.error(`stderr: ${data}`); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment