Is it possible to call a .dll I made in C#, and use it in my unmanaged C++ code? If so, can someone show me an example of how this is done, or a link to go about doing it. I wasn't able to find much on google, and most of the info wasn't relevant to me. I don't even know if it is truly possible.

Here is some code to help:

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
using System.Runtime.InteropServices;

namespace AdobeDSL
{

    interface DSLInterface
    {
        string GetID(string username);
        string Verify(string pIDMID);
    }


    public class DSL : DSLInterface
    {

        public DSL()
        {
        }

        public string GetID(string username)
        {
            //code
        }

        public string Verify(string pIDMID)
        {
            //code
       }
    }
}

This is the code to the C++ file. There should be a pointer to the interface in the member list but there is only one for the class. Therefore I can't see any of the methods I made.

#import "Path\AdobeDSL.tlb"
#include <iostream>
#include <string>

int main()
{
	CoInitialize(NULL);
	AdobeDSL:: //There should be a pointer to the interface,
                //but it is just a pointer to the class.
	return 0;
}

Edited 7 Years Ago by AceiferMaximus: n/a

Is it possible to call a .dll I made in C#, and use it in my unmanaged C++ code?

It's certainly possible, though you may want to go via some managed C++ to get there.
By compiling some of the modules in your C++ project with the /clr switch you will enable managed extensions for that code, although it will not be fully managed. It will, however, interact more easily with the C# code. So if you just have one or two modules you want to call C# from, try and compile those /clr. There may be a performance hit on those modules depending on the application. If you are calling C# from many modules and compiling all of them with /clr causes more trouble, try creating one wrapper module using /clr, then route all calls to C# via the wrapper.
Calling into the C# is pretty simple, but you need to think carefully about the data you are transferring. Because of the different ways that some data types are represented (strings, dynamic arrays), you will need to rearrange the data in the wrapper using managed data types. Stick to simple types (eg. int) and there's no problem.

Paul DiLascia has written a couple of excellent articles on this topic at
http://msdn.microsoft.com/en-us/magazine/cc300632.aspx
and
http://msdn.microsoft.com/en-us/magazine/cc163602.aspx
The second article is an update of the first, using improved features in VS2005.

Unfortunately, I can not include any CLR in the project. It would make things 100% easier, but this is an add-in, and the code has to be completely unmanged within the project.

Well, I finally figured it out. So now I have a fully functional COM wrapper I can use from unmanaged C++. It took some tweaking but here is how I did it.

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace AdobeDSL
{

    public interface DSLInterface
    {
        string GetID(string username);
        string Verify(string pID);
    }

    [ClassInterface(ClassInterfaceType.None)]
    public class DSL : DSLInterface
    {

        public DSL()
        {
        }

        public string GetID(string username)
        {
              //My Code
        }

        public string Verify(string pID)
        {
            //My Code
        }
    }
}

I had to add public to the interface, which may have been a major problem in the first place. You also have to make an assembly key, then you must register it for COM (checkbox in the Properties\Build), and in the assembly properties file, you must set the "Com Visible" Property to True. This should get anyone else up and running.

This question has already been answered. Start a new discussion instead.