15139
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
1 /*
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
2 Simple program to start Emacs with its console window hidden.
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
3
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
4 This program is provided purely for convenience, since most users will
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
5 use Emacs in windowing (GUI) mode, and will not want to have an extra
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
6 console window lying around. */
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
7
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
8 #define WIN32
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
9
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
10 #include <windows.h>
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
11 #include <string.h>
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
12 #include <malloc.h>
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
13
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
14 int WINAPI
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
15 WinMain (HINSTANCE hSelf, HINSTANCE hPrev, LPSTR cmdline, int nShow)
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
16 {
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
17 STARTUPINFO start;
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
18 SECURITY_ATTRIBUTES sec_attrs;
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
19 SECURITY_DESCRIPTOR sec_desc;
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
20 PROCESS_INFORMATION child;
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
21 int wait_for_child = FALSE;
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
22 DWORD ret_code = 0;
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
23 char * new_cmdline;
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
24 char * p;
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
25 char modname[MAX_PATH];
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
26
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
27 if (!GetModuleFileName (NULL, modname, MAX_PATH))
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
28 goto error;
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
29 if ((p = strrchr (modname, '\\')) == NULL)
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
30 goto error;
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
31 *p = 0;
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
32
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
33 new_cmdline = alloca (MAX_PATH + strlen (cmdline) + 1);
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
34 strcpy (new_cmdline, modname);
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
35 strcat (new_cmdline, "\\emacs.exe ");
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
36
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
37 /* append original arguments if any; first look for -wait as first
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
38 argument, and apply that ourselves */
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
39 if (strncmp (cmdline, "-wait", 5) == 0)
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
40 {
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
41 wait_for_child = TRUE;
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
42 cmdline += 5;
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
43 }
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
44 strcat (new_cmdline, cmdline);
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
45
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
46 /* set emacs_dir variable if runemacs was in "%emacs_dir%\bin" */
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
47 if ((p = strrchr (modname, '\\')) && stricmp (p, "\\bin") == 0)
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
48 {
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
49 *p = 0;
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
50 SetEnvironmentVariable ("emacs_dir", modname);
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
51 }
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
52
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
53 memset (&start, 0, sizeof (start));
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
54 start.cb = sizeof (start);
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
55 start.dwFlags = STARTF_USESHOWWINDOW;
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
56 start.wShowWindow = SW_HIDE;
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
57
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
58 sec_attrs.nLength = sizeof (sec_attrs);
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
59 sec_attrs.lpSecurityDescriptor = NULL;
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
60 sec_attrs.bInheritHandle = FALSE;
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
61
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
62 if (CreateProcess (NULL, new_cmdline, &sec_attrs, NULL, TRUE, 0,
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
63 NULL, NULL, &start, &child))
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
64 {
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
65 if (wait_for_child)
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
66 {
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
67 WaitForSingleObject (child.hProcess, INFINITE);
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
68 GetExitCodeProcess (child.hProcess, &ret_code);
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
69 }
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
70 CloseHandle (child.hThread);
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
71 CloseHandle (child.hProcess);
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
72 }
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
73 else
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
74 goto error;
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
75 return (int) ret_code;
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
76
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
77 error:
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
78 MessageBox (NULL, "Could not start Emacs.", "Error", MB_ICONSTOP);
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
79 return 1;
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
80 }
|