Can I import a C# dll into a C++ program?

DarkForceRising

Senior member
Apr 16, 2005
407
0
71
I've been trying to figure this out. I have a dll compiled from C# that I'd like to use in a C++ program. It'd really help to be able to use one of the classes in the dll as opposed to rewriting it in C++. But every single tutorial I've been able to find is going the opposite direction, from C++ to C#.

Is it possible to import a class from a C# dll to a C++ program, and if so, how?
 

Crusty

Lifer
Sep 30, 2001
12,684
2
81
I believe the only way is by exposing your C# class using Interop, I've only needed to do it for some VSTO solutions I'm working on so not sure if there is a better way for C++ or not.
 

DarkForceRising

Senior member
Apr 16, 2005
407
0
71
Anything else? The closest thing I can find is this piece of code:

#include <iostream>
#using <mscorlib.dll>

#using <dlltest.dll>

using namespace System;
using namespace System::Collections;
using namespace std;


using namespace dlltest;

void main(void)
{
Adder a;
cout << a.add(3,5) << endl;
while(1);
}

But that's Managed C++. The #using command doesn't exist in C++ that I know of. Anyone know of a way around this?
 

imported_Dhaval00

Senior member
Jul 23, 2004
573
0
0
Is this managed (.NET) C++? I don't develop in C++, but a couple of my team members
reference C# libraries from within managed C++.

Edit: Nevermind... just saw your last sentence there. All that Friday funk.
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
13
81
www.markbetz.net
I'm pretty sure Crusty is correct. If you expose the interface to the managed assembly as an interop COM interface, then you can connect to it and use it from C++. I haven't had to do it, though, so I don't know the details.
 

DarkForceRising

Senior member
Apr 16, 2005
407
0
71
Originally posted by: Markbnj
I'm pretty sure Crusty is correct. If you expose the interface to the managed assembly as an interop COM interface, then you can connect to it and use it from C++. I haven't had to do it, though, so I don't know the details.

Unfortunately, I have no idea what this means. And google is driving me bonkers by ignoring # symbols.

 

oog

Golden Member
Feb 14, 2002
1,721
0
0
you create a com-callable wrapper to the .net library, then you access it as a com object, which you can do with c++.
 

sao123

Lifer
May 27, 2002
12,648
201
106
Originally posted by: DarkForceRising
Originally posted by: Markbnj
I'm pretty sure Crusty is correct. If you expose the interface to the managed assembly as an interop COM interface, then you can connect to it and use it from C++. I haven't had to do it, though, so I don't know the details.

Unfortunately, I have no idea what this means. And google is driving me bonkers by ignoring # symbols.

if you do not understand COM objects in C++, then your answer is no.
If you were using managed C++ with C#, then the process is somewhat easier.
 

StormRider

Diamond Member
Mar 12, 2000
8,324
2
0
You can do it in C++/CLI which is what MS is calling Managed C++ now. I forget the exact details but you need to compile the C++ code with the /CLR switch so that the compiler (in VS 2005 and up) produces managed code. C++/CLI is the only .NET language that can mix and match native and managed code.

You need to put a reference to the C# assembly (DLL) that you want to use in your C++/CLI program. I forget the exact syntax but you also need to do that "import" and "using" namespaces for your C# classes.

When you instantiate your C# class, you need to use a new syntax to do it. You use gcnew instead of new to instantiate a .NET class like below. Also, you use "^" to declare a "reference" variable to a .NET object (similar to how you use "*" to declare a pointer variable to a regular native C++ object). The syntax can be confusing. So the below code declares arr to be a reference to a .NET array of .NET strings.

int main()
{
array<String^>^ arr = gcnew array<String^>(10);
int i = 0;

for each(String^% s in arr)
s = i++.ToString();

return 0;
}
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |