1 |
joko |
1.3 |
/* $Id: min_shell.c,v 1.2 2006/06/15 12:41:05 joko Exp $ */ |
2 |
joko |
1.1 |
|
3 |
|
|
#include <stdlib.h> |
4 |
joko |
1.3 |
#include <stdio.h> |
5 |
joko |
1.1 |
#include <errno.h> |
6 |
joko |
1.2 |
#include <windows.h> |
7 |
|
|
|
8 |
|
|
#define BOOL int |
9 |
|
|
#define TRUE 1 |
10 |
|
|
#define FALSE 0 |
11 |
joko |
1.1 |
|
12 |
|
|
int main(int argc, char * argv[]) { |
13 |
|
|
if (argc == 1) { |
14 |
joko |
1.2 |
//printf("No arguments given."); |
15 |
|
|
//exit(EXIT_FAILURE); |
16 |
|
|
char filename[255] = "abc\0"; |
17 |
|
|
BOOL background = 0; |
18 |
|
|
start_process(filename, background); |
19 |
joko |
1.1 |
} |
20 |
|
|
} |
21 |
joko |
1.2 |
|
22 |
|
|
BOOL start_process(char *filename[], BOOL background) { |
23 |
|
|
|
24 |
|
|
STARTUPINFO si; |
25 |
|
|
PROCESS_INFORMATION pi; |
26 |
|
|
|
27 |
|
|
ZeroMemory(&si,sizeof(si)); |
28 |
|
|
si.cb=sizeof(STARTUPINFO); |
29 |
|
|
|
30 |
|
|
ZeroMemory(&pi,sizeof(pi)); |
31 |
|
|
//pi.cb=sizeof(PROCESS_INFORMATION); |
32 |
|
|
|
33 |
|
|
CreateProcess( |
34 |
|
|
NULL, "notepad.exe", |
35 |
|
|
NULL, NULL, FALSE, 0, |
36 |
|
|
NULL, NULL, |
37 |
|
|
&si, |
38 |
|
|
&pi |
39 |
|
|
); |
40 |
joko |
1.3 |
|
41 |
|
|
if (!background) { |
42 |
|
|
long status; |
43 |
|
|
status = WaitForSingleObject(pi.hProcess, INFINITE); |
44 |
|
|
if (status == WAIT_FAILED) { |
45 |
|
|
check_alert_error("WaitForSingleObject"); |
46 |
|
|
} |
47 |
|
|
} |
48 |
|
|
|
49 |
|
|
} |
50 |
|
|
|
51 |
|
|
check_alert_error(char *error_source[]) { |
52 |
|
|
DWORD dwError = GetLastError(); |
53 |
joko |
1.2 |
|
54 |
joko |
1.3 |
HLOCAL hlocal = NULL; |
55 |
|
|
|
56 |
|
|
BOOL fOk = FormatMessage( |
57 |
|
|
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER, |
58 |
|
|
NULL, |
59 |
|
|
dwError, |
60 |
|
|
MAKELANGID(LANG_NEUTRAL, SUBLANG_SYS_DEFAULT), |
61 |
|
|
(PTSTR)&hlocal, |
62 |
|
|
0, |
63 |
|
|
NULL |
64 |
|
|
); |
65 |
|
|
|
66 |
|
|
if (hlocal != NULL) { |
67 |
|
|
fprintf(stderr, "Error with '%s': %s", error_source, hlocal); |
68 |
|
|
LocalFree(hlocal); |
69 |
|
|
} |
70 |
|
|
|
71 |
joko |
1.2 |
} |