青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

C++ Programmer's Cookbook

{C++ 基礎} {C++ 高級} {C#界面,C++核心算法} {設計模式} {C#基礎}

C++調用C#的COM(轉載)



轉自:http://www.codeproject.com/csharp/ManagedCOM.asp

Preface

COM Interoperability is the feature of Microsoft .NET that allows managed .NET code to interact with unmanaged code using Microsoft's Component Object Model semantics.

This article is geared towards C# programmers who are familiar with developing COM components and familiar with the concept of an interface. I'll review some background on COM, explain how C# interacts with COM, and then show how to design .NET components to smoothly interact with COM.

For those die-hard COM experts, there will be some things in this article that are oversimplified, but the concepts, as presented, are the important points to know for those developers supplementing their COM code with .NET components.

Introduction

.NET Interfaces and Classes

The basis for accessing .NET objects either from other .NET code or from unmanaged code is the Class. A .NET class represents the encapsulation of the functionality (methods and properties) that the programmer wants to expose to other code. A .NET interface is the abstract declaration of the methods and properties that classes which implement the interface are expected to provide in their implementations. Declaring a .NET interface doesn't generate any code, and a .NET interface is not callable directly. But any class which implements ("inherits") the interface must provide the code that implements each of the methods and properties declared in the interface definition.

Microsoft realized that the very first version of .NET needed a way to work with the existing Windows technology used to develop applications over the past 8+ years: COM. With that in mind, Microsoft added support in the .NET runtime for interoperating with COM - simply called "COM Interop". The support goes both ways: .NET code can call COM components, and COM code can call .NET components.

Using the code

Steps to create a Managed .NET C# COM Object:

  1. Open VS.NET2003->New Project->Visual C# Projects->Class Library.
  2. Project name: MyInterop.
  3. Create MyDoNetClass.cs file, and add the following lines of code:
    using System.Runtime.InteropServices;
                    using System.Windows.Forms;
  4. Create an Interface IMyDotNetInterface.
  5. Create a class MyDoNetClass.
  6. Add the following line for MyDotNetClass:
    [ClassInterface(ClassInterfaceType.None)]

Although a .NET class is not directly invokable from unmanaged code, Microsoft has provided the capability of wrapping a .NET interface in an unmanaged layer of code that exposes the methods and properties of the .NET class as if the class were a COM object. There are two requirements for making a .NET class visible to unmanaged code as a COM object:

Requirement 1:

You have to add GUIDs - Globally Unique Identifiers - into your code for the interface and the class separately, through a GUID tool.

  1. Now, create a GUID for the Interface, and add the following line for the interface:
    [Guid("03AD5D2D-2AFD-439f-8713-A4EC0705B4D9")]
  2. Now, create a GUID for the class, and add the following line for the class:
    [Guid("0490E147-F2D2-4909-A4B8-3533D2F264D0")]
  3. Your code will look like:
    using System;
                    using System.Runtime.InteropServices;
                    using System.Windows.Forms;
                    namespace MyInterop
                    {
                    [Guid("03AD5D2D-2AFD-439f-8713-A4EC0705B4D9")]
                    interface IMyDotNetInterface
                    {
                    void ShowCOMDialog();
                    }
                    [ClassInterface(ClassInterfaceType.None)]
                    [Guid("0490E147-F2D2-4909-A4B8-3533D2F264D0")]
                    class MyDotNetClass : IMyDotNetInterface
                    {
                    // Need a public default constructor for COM Interop.
                    public MyDotNetClass()
                    {}
                    public void ShowCOMDialog()
                    {
                    System.Windows.Forms.MessageBox.Show(“I am a" +
                    "  Managed DotNET C# COM Object Dialog”);
                    }
                    }
                    }
  4. Compile the solution.
  5. You will see inside the project directory->obj->debug directory, the file “MyInterop.dll” generated after compilation.

Requirement 2:

Registration of the COM Class and Interfaces

For a COM class to be accessible by the client at runtime, the COM infrastructure must know how to locate the code that implements the COM class. COM doesn't know about .NET classes, but .NET provides a general "surrogate" DLL - mscoree.dll -- which acts as the wrapper and intermediary between the COM client and the .NET class.

  1. Hard-code a specific version number in your AssemblyVersion attribute in the AssemblyInfo.cs file which is in your project.

    Example:

    [assembly: AssemblyVersion("1.0.0.0")]
  2. Create a strong-name key pair for your assembly and point to it via the AssemblyKeyFile attribute in the AssemblyInfo.cs file which is in your project. Example:
    sn -k TestKeyPair.snk
    [assembly: AssemblyKeyFile("TestKeyPair.snk")]
  3. Add your assembly to the GAC using the following command:
    gacutil /i MyInterop.dll
  4. Register your assembly for COM by using the REGASM command along with the "/tlb" option to generate a COM type library.
    REGASM MyInterop.dll /tlb:com.MyInterop.tlb
  5. Close the C# project.

Steps to create an Unmanaged C++ application to call a .NET Managed C# COM

  1. Open VS.NET2003->New Project->Visual C++ Projects->Win32->Win32 Console Project.
  2. Name: DotNet_COM_Call.
  3. Include the following line in your DoNet_COM_Call.cpp file:
    #import “<Full Path>\com.MyInterop.tlb" named_guids raw_interfaces_only
  4. Compile the solution.
  5. It will generate a “com.myinterop.tlh” file into your project->debug directory.
  6. You can open this file and see the contents. This is basically the proxy code of the C# COM code.
  7. Now, you can write the code to call the .NET Managed COM.
  8. Please add the following lines of code before calling the COM exported functions:
    CoInitialize(NULL);   //Initialize all COM Components
                    // <namespace>::<InterfaceName>
                    MyInterop::IMyDotNetInterfacePtr pDotNetCOMPtr;
                    // CreateInstance parameters
                    // e.g. CreateInstance (<namespace::CLSID_<ClassName>)
                    HRESULT hRes =
                    pDotNetCOMPtr.CreateInstance(MyInterop::CLSID_MyDotNetClass);
                    if (hRes == S_OK)
                    {
                    BSTR str;
                    pDotNetCOMPtr->ShowCOMDialog ();
                    //call .NET COM exported function ShowDialog ()
                    }
                    CoUninitialize ();   //DeInitialize all COM Components
  9. Run this console application.
  10. Expected result: a managed code (C# ) dialog should appear with the string “I am a Managed DotNET C# COM Object Dialog”.

Points of Interest

While creating an Interface for COM exported functions, creating GUIDs for the Interface and the class and registering the class are required steps, and doing all this is always interesting and fun. Calling parameterized exported functions also is very interesting.

About Atul Mani


Atul Mani Tripathi has been a professional developer since 1997 and working as a senior consultant with Cognizant Technology Solutions (CTS) .

Most of the time, his focus is on creating a clean and simple solution to development problems. He is a C++, Visual C++ and C# (Dot NET) guy who loves Visual Studio, Google, Code Project and developing personal projects.

posted on 2007-05-30 22:46 夢在天涯 閱讀(10797) 評論(2)  編輯 收藏 引用 所屬分類: CPlusPlus

評論

# re: C++調用C#的COM(轉載) 2007-06-05 09:21 夢在天涯

可以參考msdn:http://msdn2.microsoft.com/zh-cn/library/2w30w8zx(VS.80).aspx  回復  更多評論   

# re: C++調用C#的COM(轉載)[未登錄] 2013-08-20 15:58 super

How about COM event?  回復  更多評論   

公告

EMail:itech001#126.com

導航

統計

  • 隨筆 - 461
  • 文章 - 4
  • 評論 - 746
  • 引用 - 0

常用鏈接

隨筆分類

隨筆檔案

收藏夾

Blogs

c#(csharp)

C++(cpp)

Enlish

Forums(bbs)

My self

Often go

Useful Webs

Xml/Uml/html

搜索

  •  

積分與排名

  • 積分 - 1816623
  • 排名 - 5

最新評論

閱讀排行榜

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
      <noscript id="pjuwb"></noscript>
            <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
              <dd id="pjuwb"></dd>
              <abbr id="pjuwb"></abbr>
              亚洲欧美久久| 久久久久久久精| 国产精品video| 一区二区三区国产精品| 最新日韩av| 欧美另类69精品久久久久9999| 亚洲三级网站| 亚洲伦理一区| 国产精品嫩草久久久久| 欧美中文字幕视频在线观看| 欧美一区亚洲| 亚洲国产精品嫩草影院| 最新中文字幕亚洲| 国产精品九九| 久久久国产精品一区二区三区| 久久国产免费| 日韩小视频在线观看专区| 99国产精品国产精品久久| 国产精品久久久久久久久免费| 久久er99精品| 免费日韩av| 午夜精品成人在线视频| 欧美中文字幕久久| 99国产精品视频免费观看| 亚洲少妇自拍| 亚洲成色www8888| 亚洲每日在线| 国内综合精品午夜久久资源| 91久久嫩草影院一区二区| 国产精品另类一区| 欧美大片一区二区| 国产精品二区影院| 亚洲电影观看| 国产视频一区三区| 亚洲美女视频网| 樱桃国产成人精品视频| 日韩一级精品| 亚洲激情视频在线播放| 亚洲在线视频观看| 日韩小视频在线观看| 欧美专区中文字幕| 亚洲综合色丁香婷婷六月图片| 麻豆精品在线观看| 欧美在线观看www| 欧美日韩喷水| 欧美国产精品| 激情伊人五月天久久综合| aⅴ色国产欧美| 亚洲理论在线观看| 麻豆91精品| 久久综合九色综合久99| 国产精自产拍久久久久久| 亚洲精品日韩在线| 亚洲人体1000| 噜噜噜躁狠狠躁狠狠精品视频| 久久久久高清| 国产日韩视频一区二区三区| 夜夜夜久久久| 亚洲视频导航| 欧美人与性动交a欧美精品| 欧美成人亚洲| 亚洲国产欧美另类丝袜| 久久一区二区三区av| 久久综合给合| 在线视频观看日韩| 久久久久久一区二区三区| 久久精品二区亚洲w码| 国产精品女主播在线观看| 一区电影在线观看| 亚洲午夜精品久久久久久浪潮| 女人香蕉久久**毛片精品| 亚洲福利视频二区| 亚洲人成高清| 欧美日韩久久久久久| 一区二区三区欧美激情| 亚洲网站在线观看| 国产精品区一区| 欧美在线一二三区| 牛人盗摄一区二区三区视频| 在线日韩av片| 欧美剧在线免费观看网站| 一本到高清视频免费精品| 中文精品视频| 国产欧美日韩三级| 久久人体大胆视频| 亚洲国产精品悠悠久久琪琪| 一区二区三区福利| 国产农村妇女毛片精品久久莱园子| 午夜精品免费| 欧美激情第9页| 中文国产成人精品| 国产日韩一区二区三区在线播放| 午夜精品国产| 亚洲高清网站| 亚洲欧美一级二级三级| 黄色亚洲大片免费在线观看| 欧美mv日韩mv国产网站| 一区二区日韩伦理片| 久久激情一区| 99国产精品久久| 国产人成精品一区二区三| 美女视频一区免费观看| 一区二区三区不卡视频在线观看 | 99精品国产99久久久久久福利| 欧美护士18xxxxhd| 午夜精品亚洲一区二区三区嫩草| 你懂的国产精品| 亚洲一区三区在线观看| 国产亚洲综合在线| 欧美精品日韩www.p站| 性欧美8khd高清极品| 亚洲国产小视频| 久久久www成人免费毛片麻豆| 日韩网站在线| 韩国女主播一区| 欧美日韩一区不卡| 久久午夜电影| 午夜精品福利在线观看| 亚洲区免费影片| 久久一区二区视频| 亚洲综合国产激情另类一区| 亚洲成人在线网站| 国产婷婷色一区二区三区| 欧美日韩免费网站| 欧美成人一区二免费视频软件| 亚洲欧美怡红院| 99视频在线精品国自产拍免费观看| 麻豆91精品91久久久的内涵| 午夜在线成人av| 国产精品99久久久久久久久| 亚洲青色在线| 在线观看欧美| 狠狠综合久久av一区二区小说 | 国产精品伊人日日| 欧美福利小视频| 久久婷婷麻豆| 久久久91精品| 久久国产乱子精品免费女 | 欧美激情一区二区三区成人 | 亚洲一区二区三区免费视频| 亚洲高清在线| 亚洲丰满在线| 亚洲第一页中文字幕| 激情成人在线视频| 狠狠色狠狠色综合日日五| 韩国三级在线一区| 国产在线一区二区三区四区| 国产精品视频导航| 欧美日韩综合在线| 国产精品大片免费观看| 欧美日韩综合在线| 国产精品福利久久久| 国产精品入口福利| 国产人成精品一区二区三| 国产欧美日韩综合精品二区| 国产精品午夜在线观看| 国产三级欧美三级| 在线成人性视频| 亚洲国产小视频| 亚洲巨乳在线| 亚洲欧美电影院| 久久精品30| 欧美.com| 亚洲人永久免费| 亚洲网站在线播放| 欧美一区二区在线免费观看| 久久久国产精品亚洲一区 | 欧美刺激午夜性久久久久久久| 欧美成年人在线观看| 欧美久久久久久久久久| 欧美性猛交xxxx乱大交退制版| 国产女人aaa级久久久级| 激情另类综合| 99re6热只有精品免费观看 | 黄色影院成人| 亚洲人成网站在线观看播放| 亚洲午夜在线视频| 久久久久久午夜| 最新成人在线| 欧美一区二区三区视频免费| 欧美成人tv| 国产欧美日韩视频一区二区三区 | 欧美性jizz18性欧美| 国产美女精品一区二区三区| 在线精品国产欧美| 亚洲网址在线| 欧美国产在线电影| 亚洲欧美成人一区二区在线电影| 久久免费黄色| 国产精品久久久久久久午夜| 亚洲国产一区二区三区a毛片| 亚洲性图久久| 免费在线视频一区| 亚洲欧美国产高清| 欧美福利精品| 国内偷自视频区视频综合| 中文一区在线| 亚洲丶国产丶欧美一区二区三区| 亚洲欧美日本在线| 欧美日韩亚洲综合一区|