Calling Managed .NET C# COM Objects from Unmanaged C++

February 8th, 2010 Phanix

從 VC++ 中呼叫 C# 所寫的 COM 物件,主要的可以分成三項需要注意的事情。

1. 怎麼讓 C# class library 在編譯出 DLL 的同時同時產生 COM 物件
2. 怎麼讓系統認得這個這個 COM 物件
3. 怎麼在 VC++ 中用這個 COM 物件

這個網頁裡頭寫得非常清楚,同時還有範例下載。

在C#中define struct並以 .dll 方式給C++用

June 30th, 2009 Phanix

原本以為在 C# 中使用 struct 又要匯出成 .dll 給 c++ 需要有什麼特殊處理,結果發現其實不用…

namespace TESTCOMObject
{
    //其他程式碼
    //...

    public struct tag_Data
    {
        public string strName;
        public string strValue;
    }
}

而在 C++ 裡頭要使用就只要 import 之後就可以放心用了

TESTCOMObject::tag_Data aa[50];
aa[7].strName = "xxabc";
aa[7].strValue = "oo";
aa[17].strName = "xxabcyy";
aa[17].strValue = "oo";

TESTCOMObject::tag_Data bb;
bb.strName = "hahaha";
bb.strValue = "hehehe";