How to use C# dll in C++?

I have a file "myDLL.dll" that was generated from IKVM, from a jar file. It works in C# project perfectly. However, I need to call it from a C++ application now.

Please note that I have ZERO control over the dll, because it was generated by IKVM. From IKVM's site, it says that it is a CLI dll.

Using RegAsm.exe, I got the .tlb file.. if that helps.

I need to know how to:

1. call a static method in C++. in C# code, it is `com.myApp.Initialiser.initialise(object, string, int)`
2. create a new instance in C++. in C# code it is `new com.myApp.requests.MyRequest()`
3. Import IKVM's dll properly so that the code in myDLL.dll works.

Code so far:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include "stdafx.h"
#include <iostream>
using namespace std;
#include <stdio.h>
#include <cstring>
#include <string>
#include <windows.h>

typedef void (*Initialise)(void*, std::string, int);

int main(){
    HINSTANCE myDLL = LoadLibrary(TEXT("myDLL.dll"));
    HINSTANCE ikvmCoreDLL = LoadLibrary(TEXT("IKVM.OpenJDK.Core.dll"));

    if(myDLL && ikvmCoreDLL){
        cout << "dlls loaded" << endl;
        //how to do 1 and 2 using myDLL.dll?

        Initialise ptr = (Initialise) GetProcAddress(myDLL, "initialise"); //not working
    }

    return 0;
}
Last edited on
I've never tried it myself (no experience with C#), but I imagine it would be very tricky. Here's a stackoverflow post you may find helpful: https://stackoverflow.com/questions/19144006/using-c-sharp-dll-in-c-code#19146736
Using RegAsm.exe, I got the .tlb file. Would that help?
Would using C++ / CLR be an option ?
I'll take anything you can throw at me. As long as the C++ application can reference the C# CLI dll and run the methods properly.
Last edited on
The I would give it a try:
In Visual Studio create a CLR app and add a reference to the C# .dll and you are done.
Maybe add a using namespace...

To call the static method do this:
com::myApp::Initialiser::initialise(nullptr, "Hello", 1)
Not sure what parameters mean.
Topic archived. No new replies allowed.