Deploying a C # DLL COM File in an Unmanaged C ++ Program

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

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

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

Here is my COM file

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 simple example of using this with unmanaged code. Please include in your answers:

  • Do I need to include a project or only COM source files
  • Do I need to add a link
  • The simplest example of passing a string and printing it with cout.

Thank you for your help!

+1
source share
1

, , COM- .NET, (++ ). :

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

.NET COM, RegAsm. (.TLB), - ( , X86, X64):

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

. codebase, .

.TLB . , ComVisible. , Dispatch Dual, COM Automation (VB, VBA), (VBScript, JScript), IUnknown, C/++, IDispatch.

++ ++ Microsoft: #import Directive, Add References .NET., , COM-:

#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 String Automation BSTR , IUnknown) , .

, IMHO .

+5

All Articles