Hello, COM(C#) World!
Posted on 5月 17th, 2012 by cx20
COM(C#)
COM(Component Object Model)はマイクロソフトの提唱するプログラム部品の仕様である。
COM を用いて開発された部品であれば言語を問わず利用することができる。
以下は C# による COM クライアント(事前バインディングならびに実行時バインディング)の例となっている。
ソースコード(事前バインディング)
using System;
using System.Runtime.InteropServices;
using Shell32;
class Hello
{
static void Main(String[] args)
{
Shell shell = new Shell();
Object vRootFolder = (long)ShellSpecialFolderConstants.ssfWINDOWS;
Folder folder = shell.BrowseForFolder(0, "Hello, COM(C#) World!", 0, vRootFolder);
if (folder != null)
{
Marshal.ReleaseComObject(folder);
}
Marshal.ReleaseComObject(shell);
}
} |
using System;
using System.Runtime.InteropServices;
using Shell32;
class Hello
{
static void Main(String[] args)
{
Shell shell = new Shell();
Object vRootFolder = (long)ShellSpecialFolderConstants.ssfWINDOWS;
Folder folder = shell.BrowseForFolder(0, "Hello, COM(C#) World!", 0, vRootFolder);
if (folder != null)
{
Marshal.ReleaseComObject(folder);
}
Marshal.ReleaseComObject(shell);
}
}
コンパイル方法(事前バインディング)
C:¥> SET PATH=C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin;%PATH%
C:¥> tlbimp %SystemRoot%\system32\shell32.dll /out:Shell32.dll
C:¥> csc /r:Shell32.dll Hello.cs |
C:¥> SET PATH=C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin;%PATH%
C:¥> tlbimp %SystemRoot%\system32\shell32.dll /out:Shell32.dll
C:¥> csc /r:Shell32.dll Hello.cs
ソースコード(実行時バインディング)
using System;
using System.Reflection;
using System.Runtime.InteropServices;
class Hello
{
static void Main(String[] args)
{
Type objType = Type.GetTypeFromProgID("Shell.Application");
Object shell = Activator.CreateInstance(objType);
Object[] param = { 0, "Hello, COM(C#) World!", 0, 36 };
Object folder = shell.GetType().InvokeMember(
"BrowseForFolder", BindingFlags.InvokeMethod, null, shell, param );
if (folder != null)
{
Marshal.ReleaseComObject(folder);
}
Marshal.ReleaseComObject(shell);
}
} |
using System;
using System.Reflection;
using System.Runtime.InteropServices;
class Hello
{
static void Main(String[] args)
{
Type objType = Type.GetTypeFromProgID("Shell.Application");
Object shell = Activator.CreateInstance(objType);
Object[] param = { 0, "Hello, COM(C#) World!", 0, 36 };
Object folder = shell.GetType().InvokeMember(
"BrowseForFolder", BindingFlags.InvokeMethod, null, shell, param );
if (folder != null)
{
Marshal.ReleaseComObject(folder);
}
Marshal.ReleaseComObject(shell);
}
}
コンパイル方法(実行時バインディング)
実行結果
+----------------------------------------+
|Browse For Folder [X]|
+----------------------------------------+
| Hello, COM(C#) Wolrd! |
| |
| +------------------------------------+ |
| |[Windows] | |
| | +[addins] | |
| | +[AppCompat] | |
| | +[AppPatch] | |
| | +[assembly] | |
| | : | |
| | : | |
| | : | |
| +------------------------------------+ |
| [Make New Folder] [ OK ] [Cancel] |
+----------------------------------------+ |
+----------------------------------------+
|Browse For Folder [X]|
+----------------------------------------+
| Hello, COM(C#) Wolrd! |
| |
| +------------------------------------+ |
| |[Windows] | |
| | +[addins] | |
| | +[AppCompat] | |
| | +[AppPatch] | |
| | +[assembly] | |
| | : | |
| | : | |
| | : | |
| +------------------------------------+ |
| [Make New Folder] [ OK ] [Cancel] |
+----------------------------------------+
Tags: COM
Categories: .NET, C#, COM, library