M
MDB
Hello All, I am trying to figure out how to execute a third party program
and then stop it and can not seem to figure out how to stop it.
I am using this code to execute it and "Terminate" it, the execute works
fine but the TerminateProcess dosn't.
Here is how I start the app:
myApp.Classes.Globals.testID =
PMMobile.Classes.SHELLEXECUTEEX.ExecuteFile("\\program
files\\WebTechWireless\\wtwnav.exe","");
My Code:
public class SHELLEXECUTEEX
{
public UInt32 cbSize;
public UInt32 fMask;
public IntPtr hwnd;
public IntPtr lpVerb;
public IntPtr lpFile;
public IntPtr lpParameters;
public IntPtr lpDirectory;
public int nShow;
public IntPtr hInstApp;
// Optional members
public IntPtr lpIDList;
public IntPtr lpClass;
public IntPtr hkeyClass;
public UInt32 dwHotKey;
public IntPtr hIcon;
public IntPtr hProcess;
[DllImport("coredll")]
extern static IntPtr ShellExecuteEx( SHELLEXECUTEEX ex );
[DllImport("coredll", EntryPoint="TerminateProcess", SetLastError=true)]
extern static int TerminateProcess(IntPtr hProcess, int uExitCode);
[DllImport("coredll")]
extern static IntPtr LocalAlloc( int flags, int size );
[DllImport("coredll")]
extern static void LocalFree( IntPtr ptr );
public static void StopProcess()
{
int temp = TerminateProcess(myApp.Classes.Globals.testID,0);
}
public static IntPtr ExecuteFile(string strFileName, string strParameters)
{
int nSize = strFileName.Length * 2 + 2;
IntPtr pData = LocalAlloc(0x40, nSize);
Marshal.Copy(Encoding.Unicode.GetBytes(strFileName), 0, pData, nSize -
2);
SHELLEXECUTEEX see = new SHELLEXECUTEEX();
see.cbSize = 60;
see.dwHotKey = 0;
see.fMask = 0;
see.hIcon = IntPtr.Zero;
see.hInstApp = IntPtr.Zero;
see.hProcess = IntPtr.Zero;
see.lpClass = IntPtr.Zero;
see.lpDirectory = IntPtr.Zero;
see.lpIDList = IntPtr.Zero;
if(strParameters.Trim().Length == 0)
{
see.lpParameters = IntPtr.Zero;
}
else
{
int nSizePar = strParameters.Length * 2 + 2;
IntPtr pDataPar = LocalAlloc(0x40, nSizePar);
Marshal.Copy(Encoding.Unicode.GetBytes(strParameters), 0, pDataPar,
nSizePar - 2);
see.lpParameters = pDataPar;
}
see.lpVerb = IntPtr.Zero;
see.nShow = 0;
see.lpFile = pData;
LocalFree(pData);
return see.hProcess;
}
}
and then stop it and can not seem to figure out how to stop it.
I am using this code to execute it and "Terminate" it, the execute works
fine but the TerminateProcess dosn't.
Here is how I start the app:
myApp.Classes.Globals.testID =
PMMobile.Classes.SHELLEXECUTEEX.ExecuteFile("\\program
files\\WebTechWireless\\wtwnav.exe","");
My Code:
public class SHELLEXECUTEEX
{
public UInt32 cbSize;
public UInt32 fMask;
public IntPtr hwnd;
public IntPtr lpVerb;
public IntPtr lpFile;
public IntPtr lpParameters;
public IntPtr lpDirectory;
public int nShow;
public IntPtr hInstApp;
// Optional members
public IntPtr lpIDList;
public IntPtr lpClass;
public IntPtr hkeyClass;
public UInt32 dwHotKey;
public IntPtr hIcon;
public IntPtr hProcess;
[DllImport("coredll")]
extern static IntPtr ShellExecuteEx( SHELLEXECUTEEX ex );
[DllImport("coredll", EntryPoint="TerminateProcess", SetLastError=true)]
extern static int TerminateProcess(IntPtr hProcess, int uExitCode);
[DllImport("coredll")]
extern static IntPtr LocalAlloc( int flags, int size );
[DllImport("coredll")]
extern static void LocalFree( IntPtr ptr );
public static void StopProcess()
{
int temp = TerminateProcess(myApp.Classes.Globals.testID,0);
}
public static IntPtr ExecuteFile(string strFileName, string strParameters)
{
int nSize = strFileName.Length * 2 + 2;
IntPtr pData = LocalAlloc(0x40, nSize);
Marshal.Copy(Encoding.Unicode.GetBytes(strFileName), 0, pData, nSize -
2);
SHELLEXECUTEEX see = new SHELLEXECUTEEX();
see.cbSize = 60;
see.dwHotKey = 0;
see.fMask = 0;
see.hIcon = IntPtr.Zero;
see.hInstApp = IntPtr.Zero;
see.hProcess = IntPtr.Zero;
see.lpClass = IntPtr.Zero;
see.lpDirectory = IntPtr.Zero;
see.lpIDList = IntPtr.Zero;
if(strParameters.Trim().Length == 0)
{
see.lpParameters = IntPtr.Zero;
}
else
{
int nSizePar = strParameters.Length * 2 + 2;
IntPtr pDataPar = LocalAlloc(0x40, nSizePar);
Marshal.Copy(Encoding.Unicode.GetBytes(strParameters), 0, pDataPar,
nSizePar - 2);
see.lpParameters = pDataPar;
}
see.lpVerb = IntPtr.Zero;
see.nShow = 0;
see.lpFile = pData;
LocalFree(pData);
return see.hProcess;
}
}