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

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 夢在天涯 閱讀(10779) 評論(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

搜索

  •  

積分與排名

  • 積分 - 1811980
  • 排名 - 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>
              亚洲三级电影全部在线观看高清| 久久午夜电影| 日韩图片一区| 亚洲一区二区精品在线| 国产精品久久久久高潮| 久久国内精品自在自线400部| 亚洲一区二区三区乱码aⅴ| 国产亚洲一区在线| 亚洲激情国产精品| 国产亚洲aⅴaaaaaa毛片| 国产精品白丝av嫩草影院 | 久久精品综合网| 久久成人18免费观看| 99成人免费视频| 亚洲国产欧美一区二区三区同亚洲 | 亚洲小说欧美另类婷婷| 久热精品在线| 在线亚洲一区| 亚洲少妇最新在线视频| 欧美成人嫩草网站| 久久精品国产欧美亚洲人人爽| 亚洲午夜日本在线观看| 亚洲免费视频网站| 亚洲欧美国产视频| 久久野战av| 久久久精彩视频| 欧美成人一区二区三区片免费| 久久久精品tv| 亚洲第一中文字幕| 欧美激情日韩| 99精品欧美一区| 亚洲福利专区| 亚洲欧洲精品一区| 亚洲开发第一视频在线播放| 亚洲精品永久免费精品| 亚洲国产精品成人va在线观看| 91久久国产综合久久| 久久久久国色av免费观看性色| 国产精品一区三区| 好看的日韩av电影| 最新亚洲电影| 久久久久国产精品人| 欧美国产一区在线| 久久www成人_看片免费不卡| 国产精品久久国产愉拍 | 一区二区三区精密机械公司 | 亚洲国产小视频| 久久综合九色综合欧美狠狠| 国产一区二区三区视频在线观看| 性欧美18~19sex高清播放| 亚洲图中文字幕| 国产精品久久久久久久久久免费| 日韩网站在线| 一区二区日韩精品| 国产精品亚洲综合天堂夜夜| 欧美在线资源| 欧美一区免费| 日韩视频在线观看国产| 亚洲精品免费观看| 国产精品国产三级国产普通话三级| 激情综合久久| 欧美国产一区在线| 国产精品视频一| 亚洲二区三区四区| 亚洲激情图片小说视频| 91久久在线视频| 久久精品国产综合精品| 亚洲激精日韩激精欧美精品| 亚洲啪啪91| 日韩视频在线免费| 亚洲永久免费精品| 篠田优中文在线播放第一区| 久久av红桃一区二区小说| 日韩视频免费在线观看| 日韩亚洲国产精品| 亚洲欧美久久| 亚洲欧美国产精品va在线观看| 欧美区一区二区三区| 在线观看亚洲精品视频| 在线成人激情黄色| 亚洲一区二区三区精品视频| 欧美大片一区二区三区| 亚洲精品中文字幕在线观看| 午夜精品福利在线观看| 久久av一区二区三区漫画| 欧美日韩色一区| 久久久精品国产免大香伊| 一本色道久久综合亚洲精品小说| 久久资源在线| 亚洲精品美女在线| 欧美亚洲一区| 久久久久国产精品厨房| 性欧美8khd高清极品| 国产视频观看一区| 欧美国产激情| 欧美精品免费播放| 亚洲精选大片| 午夜一区二区三区在线观看| 亚洲精品国产拍免费91在线| 国产麻豆午夜三级精品| 亚洲一区二区三区精品在线观看| 亚洲欧美日本另类| 久久青青草原一区二区| 欧美激情91| 亚洲第一福利视频| 午夜精品福利一区二区三区av| 亚洲综合精品自拍| 91久久精品国产91性色tv| 久久精品水蜜桃av综合天堂| 欧美国产综合视频| 老牛国产精品一区的观看方式| 国产精品成人久久久久| 亚洲精品免费在线| 亚洲国产日本| 欧美一区二区三区免费在线看| 日韩视频一区二区三区在线播放免费观看 | 亚洲高清久久久| 亚洲福利视频网站| 巨胸喷奶水www久久久免费动漫| 亚洲特色特黄| 欧美在线观看视频一区二区| 亚洲欧美日韩在线一区| 欧美日韩不卡| 欧美国产精品| 亚洲国产欧美一区二区三区同亚洲| 久久久久国产一区二区三区| 久久蜜臀精品av| 影视先锋久久| 久久精品国产成人| 亚洲欧美在线网| 韩国av一区二区| 国产精品日韩精品欧美在线| 一区二区黄色| 欧美成人按摩| 午夜天堂精品久久久久| 国产一区二区三区四区hd| 男人天堂欧美日韩| 久久精品2019中文字幕| 亚洲一二三区视频在线观看| 国产老肥熟一区二区三区| 中文在线不卡| 欧美亚洲一级片| 在线综合亚洲欧美在线视频| 欧美视频一区二区在线观看 | 黄色成人小视频| 欧美成人免费在线视频| 亚洲视频香蕉人妖| 欧美激情bt| 免费一级欧美片在线观看| 久久国产综合精品| 亚洲免费小视频| 午夜精品久久久久久久久久久久久| 欧美日韩黄视频| 欧美成人tv| 亚洲你懂的在线视频| 一本一本久久a久久精品综合麻豆| 欧美午夜精品久久久久久浪潮| 亚洲网站在线观看| 99一区二区| 亚洲校园激情| 欧美一区午夜视频在线观看| 亚洲在线播放| 久久综合狠狠综合久久激情| 欧美日韩三区| 女仆av观看一区| 欧美日韩综合在线| 国产亚洲精品久| 一区二区欧美日韩| 影音国产精品| 亚洲欧美日韩国产综合在线 | 免费在线看成人av| 亚洲免费av观看| 一区二区视频免费在线观看| 蜜臀av一级做a爰片久久| 亚洲电影有码| 日韩午夜中文字幕| 国产日韩欧美在线看| 久久综合伊人77777蜜臀| av成人黄色| 你懂的一区二区| 亚洲精品中文在线| 国产精品亚洲综合一区在线观看| 欧美一区二区国产| 亚洲全部视频| 久久夜色撩人精品| 在线视频日韩精品| 亚洲黄色免费电影| 国产欧美欧美| 欧美四级在线观看| 欧美二区在线看| 久久婷婷久久一区二区三区| 一区二区三区毛片| 欧美国产精品一区| 欧美一区二区私人影院日本 | 午夜综合激情| 亚洲精品国产精品乱码不99按摩 | 久久久久九九九九| 亚洲欧美成人综合| 亚洲精品一区在线| 亚洲电影观看|