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