Archive for 5月 21st, 2012

  1. Hello, COM(C++/CLI) World!

    Posted on 5月 21st, 2012 by cx20

    COM(C++/CLI)

    COM(Component Object Model)はマイクロソフトの提唱するプログラム部品の仕様である。
    COM を用いて開発された部品であれば言語を問わず利用することができる。
    以下は C++/CLI による COM クライアントの例となっている。

    ソースコード

    using namespace System;
    using namespace System::Runtime::InteropServices;
    using namespace Shell32;
     
    int main(array<System::String ^> ^args)
    {
        Shell^ shell = gcnew Shell;
        Folder^ folder;
        Object^ vRootFolder = (long)ShellSpecialFolderConstants::ssfWINDOWS;
        folder = shell->BrowseForFolder(0, "Hello, COM(C++/CLI) World!", 0, vRootFolder);
        if (folder != nullptr)
        {
            Marshal::ReleaseComObject(folder);
        }
        Marshal::ReleaseComObject(shell);
        return 0;
    }

    コンパイル方法

    C:¥> SET PATH=C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin;%PATH%
    C:¥> tlbimp %SystemRoot%\system32\shell32.dll /out:Shell32.dll
    C:¥> cl /FU Shell32.dll Hello.cs /clr

    実行結果

    +----------------------------------------+
    |Browse For Folder                    [X]|
    +----------------------------------------+
    | Hello, COM(C#) Wolrd!                  |
    |                                        |
    | +------------------------------------+ |
    | |[Windows]                           | |
    | | +[addins]                          | |
    | | +[AppCompat]                       | |
    | | +[AppPatch]                        | |
    | | +[assembly]                        | |
    | |     :                              | |
    | |     :                              | |
    | |     :                              | |
    | +------------------------------------+ |
    | [Make New Folder]    [  OK  ] [Cancel] |
    +----------------------------------------+