c++.net调用c#类库

参考: http://www.codeproject.com/csharp/com_object_in_c_.asp
1.直接调用
//#using "../debug/CSLib.dll"   等于在工程中添加引用,addrefernce
using namespace ClassLibrary2;
ClassLibrary2::Form1^    xxForm = gcnew Form1();
2.c#写的DLL做成COM,生成的tlb供c++。net调用;
namespace ClassLibraryCCharp
{
    [Guid("40699C45-4741-4507-971E-E0A32488CE42")]
    //[InterfaceType(ComInterfaceType.InterfaceIsDual)]
    public interface InterfaceTest
    {
        void SetParentHandler(Int32 nParentHandler);
        Int32 GetWindowHandler();
        void CreateForm(Int32 nParentHandler);
        void MoveFormSize(int x, int y, int nHeght, int nWidth);
        void CloseWindow();
    }
}
namespace ClassLibraryCCharp
{
    public class TestImp : InterfaceTest
    {
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern int SetParent(int hWndChild, int hWndNewParent);

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern int GetParent(int hWndChild);

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern bool MoveWindow(int hWnd, int x, int y, int width, int height, bool bRepait);

        public TestImp()
        {
        }

        private Form _xxForm = null;

        public void SetParentHandler(Int32 nParentHandler)
        {
        }

        public Int32 GetWindowHandler()
        {
            if (_xxForm != null)
                return _xxForm.Handle.ToInt32();
            return 0;
        }

        public void CreateForm(Int32 nParentHandler)
        {
            _xxForm = new AForm();
            SetParent(_xxForm.Handle.ToInt32(), nParentHandler);
            _xxForm.Show(new ExternWindow(nParentHandler));           
        }

        public void MoveFormSize(int x, int y, int nHeght, int nWidth)
        {
            if (_xxForm != null)
            {
                _xxForm.Left = 0;
                _xxForm.Top = 0;
                _xxForm.Width = nWidth;
                _xxForm.Height = nHeght;
            }
        }
        public void CloseWindow()
        {
            if (_xxForm != null)
            {
                _xxForm.Close();
            }
        }
    }

    sealed class ExternWindow : IWin32Window
    {
        IntPtr m_Hanler;
        public ExternWindow(long nHandler)
        {
            m_Hanler = (IntPtr)(nHandler);
        }

        public IntPtr Handle
        {
            get { return m_Hanler; }
            set { m_Hanler = value; }
        }
    }

}
注册为com interrop。
必须生成强程序集名称
//sn -k Database_COM_Key.snk
//[assembly: AssemblyKeyFile("Database_COM_Key.snk")]
c++调用 #import "../ClassLibraryCCharp/bin/debug/ClassLibraryCCharp.tlb" raw_interfaces_only using namespace ClassLibraryCCharp;                 CoInitialize(NULL);                 InterfaceTestPtr pTest(__uuidof(TestImp));                 pTest->CreateForm((long)hWnd);                 pTest->MoveFormSize(0, 0, 100,100);                 long xxFormHanlder = 0;                 pTest->GetWindowHandler(&xxFormHanlder);                    CoUninitialize();
相关文章
相关标签/搜索