La création d'un processus se fait par un appel à l'API Win32 CreateProcess. Cette fonction requiert plusieurs paramètres.
BOOL CreateProcess(
LPCTSTR lpApplicationName, // name of executable module
LPTSTR lpCommandLine, // command line string
LPSECURITY_ATTRIBUTES lpProcessAttributes, // SD
LPSECURITY_ATTRIBUTES lpThreadAttributes, // SD
BOOL bInheritHandles, // handle inheritance option
DWORD dwCreationFlags, // creation flags
LPVOID lpEnvironment, // new environment block
LPCTSTR lpCurrentDirectory, // current directory name
LPSTARTUPINFO lpStartupInfo, // startup information
LPPROCESS_INFORMATION lpProcessInformation // process information
);
Le paramètre qui nous intéresse est lpCommandLine et il va contenir le nom de l'application à lancer.
#include "stdafx.h"
#include "windows.h"
BOOL CmdRun(LPSTR lpszCmd);
int main(int argc, char* argv[])
{
if( argc != 2 )
{
printf("CreateP3 cmd\n");
return 0;
}
CmdRun(argv[1]);
return 0;
}
BOOL CmdRun(LPSTR lpszCmd)
{
PROCESS_INFORMATION pi;
STARTUPINFO si;
BOOL bCreated = FALSE;
memset(&pi, 0, sizeof(PROCESS_INFORMATION));
memset(&si, 0, sizeof(STARTUPINFO));
bCreated = CreateProcess(NULL, lpszCmd, NULL, NULL, TRUE, 0, NULL, NULL, &si, π);
if( bCreated==FALSE )
{
printf("CreateProcess failed\n");
return FALSE;
}
WaitForSingleObject(pi.hProcess, INFINITE);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
return TRUE;
}
© 2001 Christophe Pichaud. All rights reserved.