TerminateProcess

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;
}
}
 
M

MDB

Thanks, I will see if I can figure how they do it since I do not want to use
the SDF.


Daniel Moth said:
Use the Process class to Start the 3rd party program and then Kill it:
http://vault.netcf.tv/VaultService/...tics/Process.cs&version=4&includedversions=20
(guest, guest)

Hint: If you don't want to use the SDF binary, or don't want to borrow the
code, then you can at least be "inspired" by it!

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


MDB said:
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;
}
}
 
P

Paul G. Tobey [eMVP]

Why not use SDF?

In any case, using TerminateProcess() is not a suitable way to cleanly stop
a process (it's a last resort method of shutting down a process which is
messed up). We've been over the list of other things that you might do with
respect to stopping a second application. You can search for them (probably
searching on TerminateProcess is the best way to find them), in the
archives:

http://groups-beta.google.com/group...tframework?hl=en&lr=lang_en&ie=UTF-8&oe=UTF-8

Paul T.

MDB said:
Thanks, I will see if I can figure how they do it since I do not want to
use the SDF.


Daniel Moth said:
Use the Process class to Start the 3rd party program and then Kill it:
http://vault.netcf.tv/VaultService/...tics/Process.cs&version=4&includedversions=20
(guest, guest)

Hint: If you don't want to use the SDF binary, or don't want to borrow
the code, then you can at least be "inspired" by it!

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


MDB said:
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;
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top