Implement a C# DLL COM File In Unmanaged C++ Program

前端 未结 1 795
一向
一向 2021-01-24 17:20

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

I have successfully created a C# COM File. Now I

相关标签:
1条回答
  • 2021-01-24 17:59

    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); 
            }
        }
    }
    

    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
    

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

    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.

    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;
    }
    

    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.

    0 讨论(0)
提交回复
热议问题