Windows
. Windows - . : , , , .. Windows 2000 NT , .
, , . . :
, , .
(Thread Local Storage - TLS) , .
, , , ,
, , .
CreateProcess - Win32. . , CreateProcess .
CreateProcess . CreateProcess , CloseHandle . CreateProcess :
BOOL CreateProcess (
LPCTSTR lpszImageName,
LPTSTR lpszCommandLine,
LPSECURITY_ATTRIBOTES lpsaProcess,
LPSECURITY_ATTRIBUTES lpsaThread,
BOOL fInheritHandles,
DWORD fdwCreate,
LPVOID lpvEnvironment,
LPCTSTR lpszCurDir,
LPSTARTUPINFO lpsiStartInfo,
LPPROCESS_INFORMATION lppiProcInfo);
: TRUE , .
lpszImageName, lpszCommandLine , , lpsaProcess lpsaThread . NULL, , fInheritHandles , (, ..) , . , ;
fdwCreate :
CREATE_SUSPENDED ResumeThread.
DETACHED_PROCESS CREATE_NEW_CONSOLE - , .
, . , .
|
|
CREATE_NEW_PROCESS_GROUP . , (<Ctrl+C> <Ctrl+Break>).
lpvEnvironment .
NULL, . , .
lpszCurDir . NULL, .
lpsiStartInfo . , GetStartupInfo. CreateProcess STARTUPINFO , , (hStdln, hStdOut hStdErr) STARTUPINFO. , STARTUPINFO - dwFlags STARTF_USESTDHANDLES, , .
, CreateProcess fInheritProcess.
lppiProcinfo , .
PROCESS_INFORMATION :
typedef struct PROCESS_INFORMATION (
HANDLE hProcess;
HANDLE hThread,
DWORD dwProcessId;
DWORD dwThreadld;
) PROCESS_INFORMATION;
lpszImageName lpszCommandLine , .
lpszImageName NULL, . , .
- lpszCommandLine.
lpszCommandLine, lpszImageName NULL. lpszImageName.
lpszImageName NULL, . .
, . (.,. ..).
lpszImageName NULL, lpszCommandLine, . , :
1. .
2. .
3. Windows,
GetSystemDirectory.
4. Windows,
GetWindowsDirectory.
5. , PATH.
, argv, GetCommandLine .
|
|
, . , argv . , .
: . ,
:
, F1 FN, , . grep pattern FK (pattern ). , dot .
, , hstdOut .
WaitForMultipleObjects, .
, ( ). cat.
WaitForMultipleObjects MAXIMUM_WAIT_OBJECTS, .
, , GetExitCodeProcess.
#include "stdafx.h"
//#include "EvryThng.h"
#define BUF_SIZE 256
static VOID cat(LPTSTR, LPTSTR);
int _tmain (DWORD argc, LPTSTR argv [])
/* . . */
{
HANDLE hTempFile;
BOOL prov;
TCHAR outFile[MAX_PATH + 100];
SECURITY_ATTRIBUTES StdOutSA =
/* . */
{sizeof
(SECURITY_ATTRIBUTES), NULL, TRUE};
TCHAR CommandLine [MAX_PATH + 100];
STARTUPINFO StartUpSearch, StartUp;
PROCESS_INFORMATION ProcessInfo;
DWORD iProc, ExCode;
HANDLE *hProc;
/* . */
typedef struct
{TCHAR TempFile [MAX_PATH];}
PROCFILE;
PROCFILE *ProcFile;
/* . */
/* , . */
GetStartupInfo (&StartUpSearch);
GetStartupInfo (&StartUp);
/* ,
. */
ProcFile = (PROCFILE *)malloc ((argc - 2) *sizeof (PROCFILE));
hProc = (HANDLE *)malloc ((argc-2) *sizeof (HANDLE));
/* "grep"
*/
for (iProc = 0; iProc < argc - 2; iProc++)
{
/* grep argv [1] argv [iProc + 2] */
_stprintf (CommandLine, _T ("%s%s%s"),
_T ("grep "), argv [1], argv [iProc + 2]);
_tprintf(_T("%s\n"),CommandLine);
if (GetTempFileName (_T ("."), _T ("gtm"), 0,
ProcFile [iProc].TempFile) == 0)
/* . */
hTempFile = /* */
CreateFile (ProcFile [iProc].TempFile, GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE, &StdOutSA,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL, NULL);
StartUpSearch.dwFlags = STARTF_USESTDHANDLES;
StartUpSearch.hStdOutput = hTempFile;
StartUpSearch.hStdError = hTempFile;
StartUpSearch.hStdInput = GetStdHandle (STD_INPUT_HANDLE);
/* . */
CreateProcess (NULL, CommandLine, NULL, NULL,
TRUE, 0, NULL, NULL, &StartUpSearch, &ProcessInfo);
|
|
/* */
CloseHandle (hTempFile);
CloseHandle (ProcessInfo.hThread);
/* . */
hProc [iProc] = ProcessInfo.hProcess;
}
/* , . */
for (iProc = 0; iProc < argc-2; iProc += MAXIMUM_WAIT_OBJECTS)
WaitForMultipleObjects (min (MAXIMUM_WAIT_OBJECTS, argc - 2
-iProc),
&hProc[iProc], TRUE, INFINITE);
/*
"cat". */
for (iProc = 0; iProc < argc - 2; iProc++)
{
printf("Proc= %d\n", iProc);
prov=GetExitCodeProcess (hProc [iProc], &ExCode);
if(ExCode!= 0) DeleteFile (ProcFile [iProc].TempFile);
if (GetExitCodeProcess (hProc [iProc], &ExCode) && ExCode == 0)
{
/* - . */
if (argc > 3) _tprintf (_T ("%s: \n"), argv [iProc+2]);
fflush (stdout);
/*
*/
_stprintf (outFile, _T("%s"),ProcFile[iProc].TempFile);
cat(argv[iProc+2], (LPTSTR) outFile);
_stprintf (CommandLine, _T("%s%s"),
_T ("cat "), ProcFile[iProc].TempFile);
_tprintf(_T("%s\n"),CommandLine);
CreateProcess (NULL, CommandLine, NULL, NULL,
TRUE, 0, NULL, NULL, &StartUp, &ProcessInfo);
WaitForSingleObject (ProcessInfo.hProcess, INFINITE);
CloseHandle (ProcessInfo.hProcess);
CloseHandle (ProcessInfo.hThread);
}
CloseHandle (hProc [iProc]);
/*DeleteFile (ProcFile [iProc].TempFile); */
}
free (ProcFile);
free (hProc);
return 0;
}
static VOID cat(LPTSTR hInFile, LPTSTR hOutFile)
{
CopyFile(hInFile, hOutFile, FALSE);
return;
}
1. : CreateProcess, OpenProcess, ExitProcess .
2. WaitForSingleObject, WaitForMultipleObjects .
3. Visual Studio C++.NET 2003.
4. , . .
5. .
6. .
7. .
4. Visual Studio C++.NET 2003
1. Microsoft Visual Studio C++.NET 2003.
2. lab_proc.
3. lab_proc.cpp .
4. , .
5. ,
, .
:
1. .
2. .
3. .
4. .
5. .
1. CreateProcess?
2. OpenProcess?
3. ExitProcess?
4. WaitForSingleObject?
5. ?
6. .