The following is the C# program to execute a program and get the return code. (This example launches NOTEPAD in "C:\Windows" directory.)
I believe you can convert the program into your desired one.
using System;
using System.Text;
using System.Runtime.InteropServices;
class Program
{
[StructLayout(LayoutKind.Sequential)]
internal struct PROCESS_INFORMATION
{
public IntPtr hProcess;
public IntPtr hThread;
public UInt32 dwProcessId;
public UInt32 dwThreadId;
}
const UInt32 WAIT_OBJECT_0 = 0x00000000;
const UInt32 INFINITE = 0xFFFFFFFF;
[StructLayout(LayoutKind.Sequential)]
internal struct SECURITY_ATTRIBUTES
{
public int nLength;
public IntPtr lpSecurityDescriptor;
public bool bInheritHandle;
}
[StructLayout(LayoutKind.Sequential)]
internal struct STARTUPINFO
{
public int cb;
public IntPtr lpReserved;
public IntPtr lpDesktop;
public IntPtr lpTitle;
public int dwX;
public int dwY;
public int dwXSize;
public int dwYSize;
public int dwXCountChars;
public int dwYCountChars;
public int dwFillAttribute;
public int dwFlags;
public short wShowWindow;
public short cbReserved2;
public IntPtr lpReserved2;
public IntPtr hStdInput;
public IntPtr hStdOutput;
public IntPtr hStdError;
}
[DllImport("kernel32.dll", EntryPoint = "CreateProcess", CallingConvention = CallingConvention.Winapi, CharSet = CharSet.Auto, SetLastError = true)]
static extern bool CreateProcess(string appName, StringBuilder cmdLine, IntPtr processAttr, IntPtr threadAttr, bool inheritHandles, int creationFlag, IntPtr environment, IntPtr curDir, ref STARTUPINFO startupInfo, ref PROCESS_INFORMATION processInfo);
[DllImport("kernel32.dll", EntryPoint = "WaitForSingleObject", CallingConvention = CallingConvention.Winapi, SetLastError = true)]
static extern UInt32 WaitForSingleObject(IntPtr hHandle, UInt32 timeOut);
[DllImport("kernel32.dll", EntryPoint = "GetExitCodeProcess", CallingConvention = CallingConvention.Winapi, SetLastError = true)]
static extern bool GetExitCodeProcess(IntPtr hHandle, out Int32 timeOut);
[DllImport("kernel32.dll", EntryPoint = "CloseHandle", CallingConvention = CallingConvention.Winapi, SetLastError = true)]
static extern bool CloseHandle(IntPtr hHandle);
unsafe static void Main(string[] args)
{
PROCESS_INFORMATION pi = new PROCESS_INFORMATION();
STARTUPINFO si = new STARTUPINFO();
si.cb = Marshal.SizeOf(si);
if (CreateProcess(@"C:\WINDOWS\notepad.exe", null , IntPtr.Zero, IntPtr.Zero, false, 0, IntPtr.Zero, IntPtr.Zero, ref si, ref pi))
{
uint result = WaitForSingleObject(pi.hProcess, INFINITE);
if (result == WAIT_OBJECT_0)
{
Int32 returnCode;
if (GetExitCodeProcess(pi.hProcess, out returnCode))
Console.Out.WriteLine(returnCode);
}
else
{
}
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}
}
}
click Accept Answer button if solves your problem.. it motivates :)