Running C++ Program on C# Tool

Hi,
There's a tool (with GUI) which was built in C#.

Additionally, I have a C++ script.

Is it possible to have the C# tool running the C++ Script.

For example, when the user press a Start Button in the C# Tool, the Tool will run the C++ Script.

Thank you.

What kind of tool?
If the 'C++ script' is a compiled executable, then you can use .NET's Process class to create a new process and run it. http://stackoverflow.com/questions/181719/how-to-start-a-process-from-c

If it is just source code or code compiled into a library, you'll have to use some C++/CLI ugliness to build a mixed-mode .DLL that the C# code can reference (or P/Invoke... I don't have a lot of experience with that, though http://msdn.microsoft.com/en-us/library/aa288468%28v=vs.71%29.aspx ).
Last edited on
Sure there is, you of course will need access to the source code of the tool built in C#. Other then that it is as easy as mapping the Start button to creating a Process and launching it.

Which that can be done with System.Diagnostics.Process. http://msdn.microsoft.com/en-us/library/system.diagnostics.process(v=vs.110).aspx

For example here is a simple C# program that runs a hypothetical C++ executable named MyProgram.exe.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
using System;
using System.Diagnostics;

namespace ProcessExample
{
    class MyExample
    {
        public static void Main()
        {
            Process myProcess = new Process();

            try
            {
                myProcess.StartInfo.FileName = "C:\\MyProgram.exe"; // Note the absolute path
                
                // Some example settings that are availible.
                // If the process doesn't terminate by itself I would recommend
                // setting CreateNoWindow to false since you won't be able to 
                // terminate the process on the desktop (Though you can programmatically)
                myProcess.StartInfo.CreateNoWindow = true;
                myProcess.StartInfo.UseShellExecute = false;
                
                // This will actually run the process.
                myProcess.Start();
                
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
    }
}


It using System.Diagnostics.Process is actually quite simple, so all you need to do is alter the C# source to create a process and launch it when the Start button is hit.
Topic archived. No new replies allowed.