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

C++ Programmer's Cookbook

{C++ 基礎(chǔ)} {C++ 高級(jí)} {C#界面,C++核心算法} {設(shè)計(jì)模式} {C#基礎(chǔ)}

C++調(diào)用C#的COM(轉(zhuǎn)載)



轉(zhuǎn)自: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 夢(mèng)在天涯 閱讀(10797) 評(píng)論(2)  編輯 收藏 引用 所屬分類(lèi): CPlusPlus

評(píng)論

# re: C++調(diào)用C#的COM(轉(zhuǎn)載) 2007-06-05 09:21 夢(mèng)在天涯

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

# re: C++調(diào)用C#的COM(轉(zhuǎn)載)[未登錄](méi) 2013-08-20 15:58 super

How about COM event?  回復(fù)  更多評(píng)論   

公告

EMail:itech001#126.com

導(dǎo)航

統(tǒng)計(jì)

  • 隨筆 - 461
  • 文章 - 4
  • 評(píng)論 - 746
  • 引用 - 0

常用鏈接

隨筆分類(lèi)

隨筆檔案

收藏夾

Blogs

c#(csharp)

C++(cpp)

Enlish

Forums(bbs)

My self

Often go

Useful Webs

Xml/Uml/html

搜索

  •  

積分與排名

  • 積分 - 1816629
  • 排名 - 5

最新評(píng)論

閱讀排行榜

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
              久久久91精品国产一区二区三区 | 亚洲三级网站| 99re亚洲国产精品| 欧美丝袜一区二区三区| 小黄鸭精品密入口导航| 免费成年人欧美视频| 日韩亚洲欧美成人一区| 欧美性猛片xxxx免费看久爱| 亚洲免费在线观看| 免播放器亚洲| 一二三区精品| 韩日精品中文字幕| 欧美国产精品人人做人人爱| 一区二区不卡在线视频 午夜欧美不卡'| 午夜久久电影网| ●精品国产综合乱码久久久久| 欧美另类亚洲| 久久精品91| av成人激情| 久久在线免费观看视频| 99国产精品国产精品毛片| 国产日韩成人精品| 欧美成人午夜激情在线| 香蕉久久夜色精品国产| 亚洲区欧美区| 久久亚洲综合| 亚洲视频在线观看网站| 一区二区亚洲| 国产精品你懂的在线| 欧美不卡一区| 欧美一区二区观看视频| 日韩天堂在线视频| 欧美成人黑人xx视频免费观看| 亚洲一区二区三区精品在线| 亚洲第一精品夜夜躁人人躁| 国产精品视频导航| 欧美人妖另类| 美女主播视频一区| 欧美一区二区三区四区夜夜大片| 亚洲精品资源美女情侣酒店| 久色婷婷小香蕉久久| 欧美一区二区久久久| 一区二区av在线| 亚洲高清视频在线观看| 国内精品久久久久久| 国产精品久久久久久av下载红粉| 男人的天堂亚洲| 久久精品国产亚洲aⅴ| 亚洲一级二级在线| 亚洲精品中文字| 亚洲二区视频| 亚洲高清在线精品| 欧美成人自拍| 欧美国产日韩一区二区在线观看| 久久国产精品亚洲77777| 亚洲欧美另类在线观看| 亚洲一区免费| 亚洲欧美精品suv| 亚洲欧美日韩国产一区| 亚洲一区精彩视频| 亚洲一区二区欧美| 亚洲午夜久久久久久久久电影网| 亚洲精选视频免费看| 亚洲精品视频在线观看免费| 亚洲国产一区二区三区青草影视| 亚洲电影免费观看高清完整版| 国内一区二区三区| 一区视频在线看| 亚洲第一精品夜夜躁人人爽 | 欧美理论大片| 欧美劲爆第一页| 欧美另类极品videosbest最新版本| 免费在线播放第一区高清av| 免费在线亚洲欧美| 欧美精品国产精品日韩精品| 欧美精品久久久久久久免费观看| 欧美成熟视频| 欧美视频中文字幕在线| 国产精品久久久久久久午夜片| 国产精品久久77777| 国产欧美va欧美va香蕉在| 国产欧美精品一区二区三区介绍 | 欧美激情精品久久久六区热门| 欧美88av| 欧美午夜电影完整版| 国产精品一区二区三区四区| 国产欧美精品在线播放| 尹人成人综合网| 亚洲欧洲中文日韩久久av乱码| aa成人免费视频| 性感少妇一区| 你懂的亚洲视频| 99re8这里有精品热视频免费| 亚洲男人天堂2024| 久久综合狠狠| 欧美日韩www| 国产一区二区高清不卡| 91久久国产精品91久久性色| 国产精品99久久久久久人| 性高湖久久久久久久久| 欧美电影免费观看网站| 99精品福利视频| 欧美在线一级va免费观看| 蜜臀va亚洲va欧美va天堂| 欧美日韩在线高清| 国产一区二区三区直播精品电影| 亚洲狠狠丁香婷婷综合久久久| 亚洲午夜av在线| 老司机成人网| 宅男噜噜噜66国产日韩在线观看| 欧美在线高清视频| 欧美日韩喷水| 一区二区三区在线高清| 亚洲欧美999| 亚洲国产老妈| 欧美一级淫片aaaaaaa视频| 欧美国产一区二区| 国产一区二区三区电影在线观看| 亚洲精品少妇| 久久久综合激的五月天| 99视频热这里只有精品免费| 久久亚洲私人国产精品va媚药 | 国产精品资源| 亚洲精品日韩激情在线电影| 久久精精品视频| 999在线观看精品免费不卡网站| 久久精品国产久精国产思思| 欧美性生交xxxxx久久久| 最新国产の精品合集bt伙计| 久久久久久久久久久一区| 99国产精品久久久久久久| 美女主播一区| 精品999网站| 久久本道综合色狠狠五月| 亚洲精选中文字幕| 老司机成人网| 狠狠色综合网| 久久www成人_看片免费不卡| 99国产精品国产精品久久| 免费视频一区| 亚洲电影下载| 久久综合给合| 久久精品国产欧美亚洲人人爽| 国产精品免费久久久久久| 亚洲五月六月| 妖精视频成人观看www| 欧美大片在线观看一区| 亚洲国产精品小视频| 美日韩精品免费| 久久久久久亚洲精品杨幂换脸 | 香蕉乱码成人久久天堂爱免费| 欧美三级视频| 亚洲午夜在线视频| 一区二区三区.www| 国产精品成人观看视频免费| 亚洲一区二区三区久久| 一区二区三区国产| 欧美午夜精品久久久| 正在播放亚洲一区| 亚洲最新视频在线| 国产精品jvid在线观看蜜臀| 亚洲视频图片小说| 亚洲午夜av电影| 国产精品影视天天线| 欧美中文字幕在线| 亚欧成人精品| 激情丁香综合| 亚洲国产精品一区二区久| 欧美精品尤物在线| 亚洲午夜性刺激影院| 亚洲一二三区精品| 国产一区二区三区视频在线观看| 久久av在线看| 久久露脸国产精品| 亚洲乱码日产精品bd| 日韩视频在线永久播放| 国产精品白丝黑袜喷水久久久| 亚洲专区一二三| 欧美一区二区三区四区夜夜大片| 激情av一区| 亚洲精品中文字| 国产精品中文在线| 老鸭窝亚洲一区二区三区| 欧美成人精品在线| 亚洲丝袜av一区| 久久电影一区| 亚洲免费播放| 亚洲欧美日韩在线不卡| 亚洲第一精品夜夜躁人人爽 | 欧美二区在线看| 欧美精品免费在线| 欧美专区一区二区三区| 久久视频一区| 亚洲午夜精品久久久久久浪潮 | 久久久久久久久久久一区 | 亚洲欧美成人一区二区三区| 狠狠色丁香久久婷婷综合丁香 | 亚洲午夜久久久久久久久电影网| 国产在线观看精品一区二区三区 | 亚洲狼人精品一区二区三区|