WSH JScript から C# を実行するために
バッチを作成しています。
(↑ここは、前提条件なので崩せません。)
以下のバッチを実行した際に表示される、
コマンドプロンプトのウィンドウが閉じられないようにする
良い方法は無いでしょうか?
アイデア・実装 などアドバイスお願いします。
► スポイラーを表示
@if(0)==(0) echo off
title %~n0
cscript.exe //nologo //E:JScript "%~f0" %*
if not %errorlevel% == 0 echo;&pause
goto:EOF
@end
function GetCSharpSrc() {
return (function () {/*
using System;
using System.Windows.Forms;
class CSharpProc {
// エントリーポイント
[STAThread]
public static int Main(string[] args) {
Console.WriteLine("C# 開始");
Application.Run(new MainForm());
return 0;
}
public class MainForm : Form
{
public MainForm()
{
this.MouseClick += (sender, e) => {
Console.WriteLine("MouseClick");
};
this.MouseDoubleClick += (sender, e) => {
Console.WriteLine("MouseDoubleClick");
};
}
}
}
*/}).toString().match(/(?:\/\*(?:[\s\S]*?)\*\/)/).pop().replace(/^\/\*/, "").replace(/\*\/$/, "");
}
// エントリーポイント用即時関数
(function () {
var exec = GetCSharpExecutor();
var result = exec.Build(GetCSharpSrc());
if (result == 0) {
exec.Execute();
for (var status = exec.GetStatus(); status[0]; status = exec.GetStatus()) {
WScript.Echo(status[1]);
//WScript.Sleep(100);
}
result = exec.GetExitCode();
}
exec.Finalize();
WScript.Quit(result);
})();
// C# 実行関数
function GetCSharpExecutor() {
return new((function () {
// コンストラクタ
var CSharpExecutor = function (shell, fso) {
if (!(this instanceof CSharpExecutor)) {
return new Console(shell);
}
this.shell = shell;
this.fso = fso;
this._workDir = "";
this._exePath = "";
this._exec;
this._stdout;
}
// アクセサ
var p = CSharpExecutor.prototype;
p.SetShell = function (shell) {this.shell = shell;}
p.GetShell = function () {return this.shell;}
p.SetFileSystem = function (fso) {this.fso = fso;}
p.GetFileSystem = function () {return this.fso;}
// コマンド実行
p.RunCmd = function (cmd) {
var exec = this.shell.Exec("%ComSpec% /c " + cmd);
var stdout = exec.StdOut;
while (exec.Status == 0) {
WScript.Sleep(100);
}
return [exec.ExitCode, stdout.ReadAll()];
}
// UTF-8テキスト保存
p.SaveUTF8Text = function (path, text) {
var sw = WScript.CreateObject("ADODB.Stream");
sw.Type = 2; // テキスト
sw.charset = "utf-8"; // BOM付きUTF-8
sw.Open();
sw.WriteText(text, 1);
sw.SaveToFile(path, 2);
sw.Close();
}
// ビルド
p.Build = function (csharpSrc, references) {
// csc.exeを探す
var cmdResult = this.RunCmd("dir %windir%\\Microsoft.NET\\Framework64\\ /on /b /s | findstr csc.exe$");
var cscPathList = cmdResult[1].split(/\r\n|\r|\n/);
if (cscPathList.length == 0) {
// csc.exeが見つからなかった
return -1;
}
// ソースファイル作成
this._workDir = this.fso.BuildPath(this.fso.getParentFolderName(WScript.ScriptFullName), WScript.CreateObject("Scriptlet.TypeLib").GUID);
var folder = this.fso.CreateFolder(this._workDir);
folder.attributes = 2; // 隠し属性
var srcPath = this.fso.BuildPath(this._workDir, "_.cs");
this._exePath = this.fso.BuildPath(this._workDir, this.fso.GetBaseName(WScript.ScriptName) + ".exe");
this.SaveUTF8Text(srcPath, csharpSrc);
// コンパイル
var refOption = "";
if (references !== undefined) {
for (var idx = 0, len = references.length; idx < len; idx++) {
refOption = " /reference:" + references[idx];
}
}
var cscPath = cscPathList[cscPathList.length - 1];
var compileCmd = cscPath + " /nologo" + refOption + " /out:" + this._exePath + " " + srcPath;
cmdResult = this.RunCmd(compileCmd);
if (cmdResult[0] != 0) {
// コンパイル失敗
WScript.Echo(cmdResult[1]);
return -2;
}
return 0;
}
// 実行
p.Execute = function () {
this._exec = this.shell.Exec("%ComSpec% /c " + this._exePath);
this._stdout = this._exec.StdOut;
}
// メッセージループ
p.GetStatus = function () {
return [!this._stdout.AtEndOfStream && this._exec.Status == 0, this._stdout.ReadLine(), this._exec.ExitCode];
}
// 終了コード
p.GetExitCode = function () {
return this._exec.ExitCode;
}
// 終了処理
p.Finalize = function () {
// 作業パス削除
while (this.fso.FolderExists(this._workDir)) {
this.fso.DeleteFolder(this._workDir, true);
}
}
return CSharpExecutor;
})())(WScript.CreateObject("WScript.Shell"), WScript.CreateObject("Scripting.FileSystemObject"));
}