Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a C# DLL and using it from unmanaged C++

I have a native (unmanaged) C++ application (using wxWidgets for what it's worth). I'm considering a separate tool application to be written in C# which would contain winform-based dialogs. putting some of those dialogs in a separate DLL would be useful as I'd hope to be able to use them from my C++ app.

But I have no idea how much messing about is needed to accomplish this, is it particularly easy?

EDIT:

I don't need to call dialogs functions directly. I just need a way for my C++ app to call an API in the C# DLL in order to pass data, and a way for the C# DLL to call methods on some kind of observer/back object in the C++ app.

e.g from C++:

CSharpManager *pCSM = SomehowGetPointerToCSharpObject();
CSharpObserver pCSO = new CSharpObserver;

pCSM->RegisterCPlusPlusObserver(pCSO);
pCSM->LaunchDialog();

As the user does stuff in the C# dialog, pCSO methods are called to pass data back into C++

So it's pretty much a raw C++/C# communication question, I think. But though I know C++ and C# I don't know how .net itself works. I know COM but would really rather avoid it, I doubt any other developers I work with know it.

like image 696
Mr. Boy Avatar asked Mar 20 '10 12:03

Mr. Boy


People also ask

What is C used to create?

C is a powerful general-purpose programming language. It can be used to develop software like operating systems, databases, compilers, and so on. C programming is an excellent language to learn to program for beginners.

Can I create software with C?

We inferred that C is used in all spheres of hardware and software development, making it useful for upcoming software developers and software professionals, of course, who have great command over C to design complex interfaces.

Why C is created?

The C language was actually created to move the UNIX kernel code from assembly to a higher level language, which would do the same tasks with fewer lines of code. Oracle database development started in 1977, and its code was rewritten from assembly to C in 1983. It became one of the most popular databases in the world.


1 Answers

The lingua franca of interop in unmanaged code is COM. That's very easy to get going on the C# side, just use the [ComVisible] attribute. You'll need to write COM code in your C++ program to use it, not so easy to get going if you've never done that. Start with the #import directive if you use the MSVC compiler.

Your next option is to host the CLR in your unmanaged code yourself rather than relying on the COM interop layer to take care of it. It allows you to create managed types directly. This requires COM as well, but only to get the CLR loaded and initialized. This project shows the approach.

like image 179
Hans Passant Avatar answered Oct 13 '22 23:10

Hans Passant