aless239
aless239

Reputation: 51

How can I add a C# GUI to C++ code?

I wrote C++ code in which are included some libraries and defined some functions plus the main. I am trying to add a GUI to this code in C#.

This code sends data (double[]) to a server. What I would make is create a graphic user interface using C#, that lets me to start sending data, clicking a button.

How could I make this?

I've tried to run the file .exe of the c++ project solution, but that doesn't work.

Upvotes: 2

Views: 1416

Answers (1)

tenfour
tenfour

Reputation: 36896

You have a lot of ways to run C++ code from C#:

COM

This is the canonical, good-bear way to do things, but I have a feeling this will be daunting at your level. Basically, you will expose your C++ classes as COM objects from your C++ project (dealing with converting between COM datatypes like _bstr_t for strings, et al). Then add this COM object as a reference in your C# project. This imposes the least pain on your C# code.

I suspect the biggest hurdle here is that you will need to understand how COM works, which is going to be pretty daunting if you don't already have experience in it.

C-like DLL

This is probably much easier, if you don't need to call many functions. It's the same as how you call native winapi functions from C#. You export C-style functions from your C++ DLL, and import them in C#. Converting between datatypes is done via the marshalling system in C#. So in general this places the burden more on the C# code, less on the C++ code.

The catch here is that you can't export much C++ stuff. So for example you cannot export a function like:

void myFunction(std::string& s);

Because std::string is a template, and will cause havoc regarding memory management and will probably just lead to heap corruption.

Rewrite in C#

If all you're doing is sending some data over a socket, then just write it in C#. For many things, C# is way faster than C++ for development, so it's probably worth looking into how much effort this would be.

Upvotes: 1

Related Questions