以下の関数を利用すると、HTAでも自身のプロセスIDを取得できます。
This file contains 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
<html> | |
<body> | |
</body> | |
<script> | |
getPID=function(){ | |
// 自身のプロセスIDを返す。 | |
// 子プロセスを作ってそのPIDを取得するために | |
// まずはプロセス起動用のオブジェクトを用意する。 | |
shell = new ActiveXObject('WScript.Shell') | |
// 子プロセスをcscriptにすると黒い画面が一瞬表示されてしまう。 | |
// 「//B」オプションを付けないとwscriptの設定画面が表示されてしまう。 | |
var childPID = shell.Exec('wscript //B').ProcessID | |
// 子の親(自身)のPIDを特定する | |
var res = new Enumerator(GetObject("winmgmts:root\\CIMV2").ExecQuery("SELECT * FROM Win32_Process where ProcessID="+childPID)) | |
return res.atEnd() ? (void 0) : res.item().ParentProcessID | |
} | |
// 起動後初回はなぜか必ず取得に失敗するので、最初に一度実行しておく。 | |
getPID() | |
document.title = getPID() | |
</script> | |
</html> |
プロセスIDを取得すると何が出来るか、というと色々ありますが、私はshell.AppActivateで自身のウィンドウをアクティブ化したかったのですが最小化されていると効かないという症状に苦戦中です。。
他にも色々用途はあると思うので、いずれ必要になった時のために公開します。
0 件のコメント:
コメントを投稿