Created
May 17, 2012 12:35
-
-
Save ka-ka-xyz/2718628 to your computer and use it in GitHub Desktop.
WSH(JScript)で多重起動を判定するサンプル。
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
var pid = checkDuplicate(); | |
WScript.Echo("起動: "+ pid); | |
/* | |
* 多重起動を判定するfunction | |
* 多重起動時、または判定失敗時にはダイアログを表示し、スクリプトを終了します。 | |
* スクリプトの起動に成功した場合にはプロセスIDを戻します。 | |
*/ | |
function checkDuplicate(){ | |
var env = WScript.CreateObject("WScript.Shell").Environment("Process"); | |
var windir = env("SystemRoot"); | |
var wmiObj = GetObject("WinMgmts:Root\\Cimv2"); | |
var processes = wmiObj.ExecQuery("Select * From Win32_Process"); | |
var processenum = new Enumerator(processes); | |
var flag = 0; | |
var pid; | |
for(; !processenum.atEnd(); processenum.moveNext()){ | |
var item = processenum.item(); | |
if(item.CommandLine != null | |
&& item.CommandLine.toLowerCase().indexOf("\"" + | |
windir.toLowerCase() + "\\system32\\wscript.exe") == 0 | |
&& item.CommandLine.indexOf(WScript.ScriptFullName) > 0 ){ | |
flag++; | |
pid = item.ProcessId; | |
} | |
} | |
if(flag > 1){ | |
WScript.Echo(WScript.ScriptName + "は既に起動されています。"); | |
WScript.Quit(0); | |
} else if(typeof pid === "undefined"){ | |
WScript.Echo("多重起動判定に失敗しました。"); | |
WScript.Quit(0); | |
} else { | |
return pid; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment