Question How to combine the C++ code with C# GUI?

hack3rcon

Member
Joined
Jun 6, 2023
Messages
7
Programming Experience
Beginner
Hello,
How can I combine the C++ code with C# GUI? For example, I want to have a C# window with a button on it and when I click on the button, then a message be shown with a C++ code.
Any idea welcomed.


Thank you.
 
Do you want to write ANSI C++? Or are you willing to use the Microsoft variant of C++ for managed code called C++/CLI? (CLI stands for Common Language Interface, not what most *nix folks assume to stand for Command Line Interface.)

Alternatively, you have your C++ code expose a COM object, and the C# code can interact with the COM object.

And yet another alternative is to compile your C++ code as DLL and expose entry points into the DLL which can be called by P/Invoke (Platform Invoke).
 
Do you want to write ANSI C++? Or are you willing to use the Microsoft variant of C++ for managed code called C++/CLI? (CLI stands for Common Language Interface, not what most *nix folks assume to stand for Command Line Interface.)

Alternatively, you have your C++ code expose a COM object, and the C# code can interact with the COM object.

And yet another alternative is to compile your C++ code as DLL and expose entry points into the DLL which can be called by P/Invoke (Platform Invoke).

Hello,
Thank you so much for your reply.
I want to write ANSI C++. Can you write a simple tutorial here or show me a good link? I watched some videos on the Youtube.com, but I couldn't find anything useful!
 
The way you wrote your post, it sounds like you want to display c++ source code in a window when a button is clicked. Is that what you mean?
 
The way you wrote your post, it sounds like you want to display c++ source code in a window when a button is clicked. Is that what you mean?

Hello,
Thank you so much for your reply.
Not really. When I click on the button, then the C++ program executes. For example, my C++ program shows "Hello World!" and when I click on the button, "Hello World!" to be displayed.
 
Stop and think about the UI impedance mismatch of your planned C# form, and your hypothetical console C++ code. Do you intend the button to open up a console window and have the "Hello, World" show up in the console window? If some, then just compile the C++ "Hello World" console program as a .EXE as you normally would, and then have your C# form button handler use Process.Start() to run your HelloWorld.exe.
 
Stop and think about the UI impedance mismatch of your planned C# form, and your hypothetical console C++ code. Do you intend the button to open up a console window and have the "Hello, World" show up in the console window? If some, then just compile the C++ "Hello World" console program as a .EXE as you normally would, and then have your C# form button handler use Process.Start() to run your HelloWorld.exe.

Hello,
Thank you again for your reply.
I don't like to call an exe file. I want to learn how can I combine the C++ code with C#.
 
Hmm. So, you want to make a reference to the C++ exe and call its functions directly, like invoking unmanaged Windows functions (Platform Invoke (P/Invoke)) from managed (.net) code (which SD already covered)..

I noted you said this:

I want to write ANSI C++

Why do you want this hybrid approach? You’re writing some C++ code so you can call it from C#, which you’re also writing? What is the purpose of splitting it up? Why not write all in one or all in the other language?
 
In general, you can write ANSI C++ code, and compile it into an assembly taking advantage of Microsoft's IJW (It Just Works). My understanding is that Microsoft does this itself in various of is products where they don't want to completely rewrite larger swathes of its code base. You will still have deal with the places where the C# and the C++ code will talk to each other, but say least that narrows down the amount of specialized code you would need to write.
 
Hmm. So, you want to make a reference to the C++ exe and call its functions directly, like invoking unmanaged Windows functions (Platform Invoke (P/Invoke)) from managed (.net) code (which SD already covered)..

I noted you said this:



Why do you want this hybrid approach? You’re writing some C++ code so you can call it from C#, which you’re also writing? What is the purpose of splitting it up? Why not write all in one or all in the other language?

Hello,
Thank you again.
I just want to learn it for another program. My main program is a form with some items like "Name", "Family" and etc. that user enters the information and click on a button, then the C++ code gets items inputs and generate a code.
I know that I can do all of these stuffs with C# easily, but I want to integrate the C++ code with C# GUI.
 
In general, you can write ANSI C++ code, and compile it into an assembly taking advantage of Microsoft's IJW (It Just Works). My understanding is that Microsoft does this itself in various of is products where they don't want to completely rewrite larger swathes of its code base. You will still have deal with the places where the C# and the C++ code will talk to each other, but say least that narrows down the amount of specialized code you would need to write.

Hello,
Thank you again for your reply.
Excuse me, can you teach me this with a simple "Hello World!"? If possible, an article, a video or...
 
Hello,
Thank you again for your reply.
Excuse me, can you teach me this with a simple "Hello World!"? If possible, an article, a video or...

Google is your friend... Anyway try this:
 
Excuse me, can you teach me this with a simple "Hello World!"? If possible, an article, a video or...

This is a C# forum. It's not for us to teach you how to write C++ code. If you have working C++ code, we can help you invoke it from C# code. To actually learn how to write C++, there are places dedicated to that on the web.
 
This is a C# forum. It's not for us to teach you how to write C++ code. If you have working C++ code, we can help you invoke it from C# code. To actually learn how to write C++, there are places dedicated to that on the web.

Hello,
Thank you so much for your reply.
I know how to write C++ code and I just want to know how can I link C++ with C#?
 

Latest posts

Back
Top Bottom