实现一个C#DLL COM文件在非托管C ++程序 [英] Implement a C# DLL COM File In Unmanaged C++ Program

查看:155
本文介绍了实现一个C#DLL COM文件在非托管C ++程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的,导致进入这个作为参考也有其他的问题:
来自联合国管理C ++

Here is my other question that led into this one as reference also: How to call managed C++ methods from Un-managed C++

我已经成功地创建了一个C#COM文件。现在我需要就如何落实它在非托管C ++一个简单的解释。

I have successfully created a C# COM File. Now I need a simple explanation on how to implement it in unmanaged C++.

我下面这个例子,但C ++的部分是薄弱。
http://www.codeproject.com/Articles / 7859 /建筑-COM对象,在-C

I am following this example but the c++ part is weak. http://www.codeproject.com/Articles/7859/Building-COM-Objects-in-C

下面是我的COM文件

using System;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace cSharpRiJHarn
{
    [Guid("ED1483A3-000A-41f5-B1BC-5235F5897872")]
    public interface DBCOM_Interface
    {
        [DispId(1)]
        String encrypt(string s);
        [DispId(2)]
        String decrpyt(string s);
    }

    [Guid("A6BCEC1D-B60C-4c97-B9AD-1FE72642A9F8"),
    InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
    public interface DBCOM_Events
    {
    }

    [Guid("7C13A8C6-4230-445f-8C77-0CA5EDECDCB5"),
    ClassInterface(ClassInterfaceType.None),
    ComSourceInterfaces(typeof(DBCOM_Events))]
    public class RijndaelLink : DBCOM_Interface
    {
        public String encrypt(String s)
        {
            return Rijndael.EncryptString(s); 
        }
        public String decrpyt(String s)
        {
            return Rijndael.DecryptString(s);
        }
    }
}



我只想要一个很基本的例如使用该与非托管代码。
请在您的回答:

I just want a VERY basic example on using this with unmanaged code. Please include in your answers:


  • 请我需要包括项目或只是COM的源文件

  • 请我需要添加一个引用

  • 出传递一个字符串,并打印出来与COUT的一个非常基本的例子。

感谢您的帮助!

推荐答案

您需要做的就是正确地定义在.NET中的COM对象的第一件事,由不可控制的世界中使用( C ++或其他)。这里是一个体面的定义:

The first thing you need to do is properly define the COM object in .NET, to be used by the unmanaged world (C++ or other). Here is a decent definition:

namespace cSharpRiJHarn
{
    [Guid("ED1483A3-000A-41f5-B1BC-5235F5897872")]
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    [ComVisible(true)]
    public interface IRijndaelLink
    {
        string encrypt(string s);
        string decrypt(string s);
    }

    [Guid("7C13A8C6-4230-445f-8C77-0CA5EDECDCB5")]
    [ComVisible(true)]
    public class RijndaelLink : IRijndaelLink
    {
        public string encrypt(string s)
        {
            return Rijndael.EncryptString(s); 
        }

        public string decrypt(string s)
        {
            return Rijndael.DecryptString(s); 
        }
    }
}



接下来,你需要注册使用 RegAsm 工具为COM此.NET程序集。我建议你​​也建立一个类型库(.tlb)与之中,这样的事情(我假设你建全工具和X86,X64没有):

Next, you need to register this .NET assembly for COM using the RegAsm tool. I suggest also you build a Type Library (.TLB) with it, something like this (I suppose you build the whole stuff for X86, not X64):

c:\Windows\Microsoft.NET\Framework\v4.0.30319\RegAsm.exe YourAssembly.dll /tlb:YourAssembly.tlb /codebase

请适应你的实际路径。同时检查代码库ARG作为你可能不需要这个投入生产。

Please adapt to your actual path. Also check the codebase arg as you may not need this in production.

这将建立一个.TLB与两个接口和内部类文件。它的工作原理,因为我们添加了标记有ComVisible特性属性。您还会注意到我没有定义调度或双接口,因为在此示例中,我不需要 COM自动化(VB,VBA),也没有任何脚本语言(VBScript中,JScript中)的支持,只有的IUnknown接口,这是很容易在普通的C / C ++比IDispatch接口使用。

This will build a .TLB file with both the interface and the class inside. It works because we added the ComVisible attribute. You will also note I have not defined a Dispatch or Dual interface because in this sample, I don't need COM Automation (VB, VBA) nor any scripting language (VBScript, JScript) support, only IUnknown interfaces which are much easier to use in plain C/C++ than IDispatch interfaces.

现在,有一个简单的方法来导入非托管C ++使用Microsoft特定的C ++世界扩展:的 #import指令,类似于.NET中添加引用。下面是使用COM对象的示例控制台应用程序:

Now, there is an easy way to import that in the unmanaged c++ world using a Microsoft specific C++ extension: #import Directive, similar to Add References in .NET. Here is a sample Console Application that uses the COM Object:

#include "stdafx.h"
#import  "c:\MyPathToTheTlb\YourAssembly.tlb" // import the COM TLB

using namespace YourAssembly;

int _tmain(int argc, _TCHAR* argv[])
{
    CoInitialize(NULL); // needed to enter COM space
    IRijndaelLinkPtr ptr(__uuidof(RijndaelLink)); // create the COM Object with the desired interface
    _bstr_t s = ptr->encrypt("hello"); // call the function
    printf("%S", (LPWSTR)s); // for example
    CoUninitialize();
    return 0;
}

您会注意到#import指令也创造了凉爽的包装(_bstr_t作为。 NET字符串将导出为自动化BSTR在这里,即使是的IUnknown接口),用于字符串处理,所以它是没有真正的大事。

You will notice the #import directive also create cool wrappers (_bstr_t, as .NET String will be exported as Automation BSTR here, even for IUnknown interfaces) for string handling, so it's no really big deal.

这是不是唯一的方法这一切都可以的工作,但是这是恕我直言一个最简单的。

This is not the only way all this can work, but that's IMHO one of the most simple.

这篇关于实现一个C#DLL COM文件在非托管C ++程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆