【VBS & PHP】スクリプトからスタンバイ状態にする で作成した Windows Script Components を .NET から呼び出します Windows Script Components は、COM ではありますが、タイプライブラリを使用して .NET のコンポーネントとして呼び出すのは無理があるので、 それぞれの言語の特性をふまえて呼び出し処理を記述します。 VB.NETもともとが VBScript なので最も親和性が高く、 CreateObject で簡単に呼び出す事ができます。 これは、通常の Microsoft の COM も同様です。 Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click Dim obj = CreateObject("Wscript.Shell") obj.Run("notepad.exe", 1, True) Dim obj2 = CreateObject("Lbox.Standby") obj2.Standby(1) End Sub End Class C#C# ではかなり面倒な手順があります。COM とのインターフェイスの仕様を きっちり調べて把握した上で、自分でメソッドの呼び出しを構築する必要があります using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Reflection; namespace COM_ACTION { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { Type WshShell; WshShell = Type.GetTypeFromProgID("Wscript.Shell"); Object obj = Activator.CreateInstance(WshShell); Object type = 1; Object wait = true; WshShell.InvokeMember( "Run", BindingFlags.InvokeMethod, null, obj, new Object[] { "notepad.exe", type, wait } ); Type Standby; Standby = Type.GetTypeFromProgID("Lbox.Standby"); Object obj2 = Activator.CreateInstance(Standby); Standby.InvokeMember( "Standby", BindingFlags.InvokeMethod, null, obj2, new Object[] { 1 } ); } } } 既存の Microsoft の COM であれば、たいてい「参照」で .NET の コンポーネント としてのインターフェイスを作成できます。また、それによって正しい引数の指定 方法も明確にする事ができます。 C# では以下のように記述可能です。 ※ Windows Script Host Object Model を参照します using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Reflection; using IWshRuntimeLibrary; namespace COM_ACTION { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { WshShell obj = new WshShell(); Object type = 1; Object wait = true; obj.Run("notepad.exe", ref type, ref wait); } } } あと、new WshShell(); でインスタンスを作成できるので、必要無いかもしれませんが ( リモート実行には必要かも )、以下のような記述でも動作します ※ このような方法はもちろん VB.NET でも動作するはずです ※ Microsoft.VisualBasic を参照します using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Reflection; using IWshRuntimeLibrary; using Microsoft.VisualBasic; namespace COM_ACTION { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { WshShell obj = (WshShell)Interaction.CreateObject("Wscript.Shell", ""); Object type = 1; Object wait = true; obj.Run("notepad.exe", ref type, ref wait); } } } Jscript.NETこの程度の処理ですと、一見 WSH で使用する Jscript と大差ありませんが、 Jscript.NET では、実際は Framework が使え、ビルドして exe を作成して使用します standby.js
var obj,obj2; obj = new ActiveXObject("Wscript.Shell"); obj.Run("notepad.exe", 1, true ); obj2 = new ActiveXObject("Lbox.Standby"); obj2.Standby( 1 ); ビルドはコマンドラインから以下のように入力します ( /t:winexe が無ければコマンドプロンプト用になります ) ※ Framework を使用して最も容易にバッチアプリ作る手段です。 %windir%\Microsoft.NET\Framework\v2.0.50727\jsc /t:winexe standby.js |