Yep, it is possible to start and close a C++ application from C#—even if you want it minimized or hidden, just like a background service. Based on your structure:
C# ---- C++ ----- PLC
your C# app acts like a controller or manager. It launches the C++ process (which talks to the PLC), possibly passing in arguments, monitors its health, and stops it when needed.
1. Starting a C++ application via C# (hidden or minimized)
You can use the System.Diagnostics.Process
class in C# to start the C++ executable.
Example (minimized):
startInfo.WindowStyle = ProcessWindowStyle.Minimized;
Example (hidden):
using System.Diagnostics;
Process cppProcess = Process_Start(startInfo);
2. Closing the C++ application from C#
If you keep a reference to the Process
object (e.g., cppProcess
), you can close it directly:
Close by process object:
cppProcess.Kill(); // Forcefully kills the process
cppProcess.WaitForExit(); // Optional: Waits until it fully exits
You can also find the target app by process name:
foreach (var process in Process.GetProcessesByName("YourCppApp")) {
process.Kill();
}
Btw. make sure the name does not include .exe
in GetProcessesByName
—just "YourCppApp"
.*
If the above response helps answer your question, remember to "Accept Answer" so that others in the community facing similar issues can easily find the solution. Your contribution is highly appreciated.
hth
Marcin