In alternativa al metodo start della classe process (il cf 1.1 non ne dispone) per avviare i vari processi, si può ricorrere alla dichiarazione di api (CreateProcess) per avviare un determinato programma.
Di seguito si riporta un esempio di codice.
Dichiarazione di Api e struttura
VB.Net
Declare Function CreateProcess Lib "coredll.dll" (ByVal imageName As String, ByVal cmdLine As String, ByVal lpProcessAttributes As IntPtr, ByVal lpThreadAttributes As IntPtr, ByVal boolInheritHandles As Int32, ByVal dwCreationFlags As Int32, ByVal lpEnvironment As IntPtr, ByVal lpszCurrentDir As IntPtr, ByVal si As Byte(), ByVal pi As ProcessInfo) As Integer
Public hProcess As IntPtr
Public hThread As IntPtr
Public ProcessId As Int32
Public ThreadId As Int32
End Class
[DllImport("coredll.dll", SetLastError = true)]
static extern bool CreateProcess(String imageName,
String cmdLine,
IntPtr lpProcessAttributes,
IntPtr lpThreadAttributes,
bool boolInheritHandles,
Int32 dwCreationFlags,
IntPtr lpEnvironment,
IntPtr lpszCurrentDir,
byte[] si,
ProcessInfo pi);
{
public IntPtr hProcess;
public IntPtr hThread;
public Int32 ProcessId;
public Int32 ThreadId;
}
Codice da inserire in un evento click di un pulsante
VB.Net
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim pi As New ProcessInfo
Dim si(128) As Byte
Dim resultato As Int32
resultato = CreateProcess("calc.exe", "", IntPtr.Zero, IntPtr.Zero, 0, 0, IntPtr.Zero, IntPtr.Zero, si, pi)
End Sub
C#
private void button1_Click(object sender, EventArgs e)
{
ProcessInfo pi = new ProcessInfo();
byte[] si = new byte[128];
bool resultato = CreateProcess("calc.exe", " ", IntPtr.Zero, IntPtr.Zero, false, 0, IntPtr.Zero, IntPtr.Zero, si, pi);
}
Tramite la parola download è possibile scaricare il file di esempio.
Download esempio
1 commento:
Really entertaining article.
Scandinavian optimization consultant providing affordable all-encompassing service (sokoptimering).
[url=http://www.smotop.se]SMOTop[/url]
http://www.smotop.se
Posta un commento