100
|
1 /*
|
|
2 Simple program to start Emacs with its console window hidden.
|
|
3
|
|
4 This program is provided purely for convenience, since most users will
|
|
5 use Emacs in windowing (GUI) mode, and will not want to have an extra
|
|
6 console window lying around. */
|
|
7
|
|
8 /*
|
|
9 You may want to define this if you want to be able to install updated
|
|
10 emacs binaries even when other users are using the current version.
|
|
11 The problem with some file servers (notably Novell) is that an open
|
|
12 file cannot be overwritten, deleted, or even renamed. So if someone
|
|
13 is running emacs.exe already, you cannot install a newer version.
|
|
14 By defining CHOOSE_NEWEST_EXE, you can name your new emacs.exe
|
|
15 something else which matches "emacs*.exe", and runemacs will
|
|
16 automatically select the newest emacs executeable in the bin directory.
|
|
17 (So you'll probably be able to delete the old version some hours/days
|
|
18 later).
|
|
19 */
|
|
20
|
|
21 /* #define CHOOSE_NEWEST_EXE */
|
|
22
|
|
23 #define WIN32
|
|
24
|
|
25 #include <windows.h>
|
|
26 #include <string.h>
|
|
27 #include <malloc.h>
|
|
28
|
|
29 int WINAPI
|
|
30 WinMain (HINSTANCE hSelf, HINSTANCE hPrev, LPSTR cmdline, int nShow)
|
|
31 {
|
|
32 STARTUPINFO start;
|
|
33 SECURITY_ATTRIBUTES sec_attrs;
|
|
34 SECURITY_DESCRIPTOR sec_desc;
|
|
35 PROCESS_INFORMATION child;
|
|
36 int wait_for_child = FALSE;
|
|
37 DWORD ret_code = 0;
|
|
38 char *new_cmdline;
|
|
39 char *p;
|
|
40 char modname[MAX_PATH];
|
|
41
|
|
42 if (!GetModuleFileName (NULL, modname, MAX_PATH))
|
|
43 goto error;
|
|
44 if ((p = strrchr (modname, '\\')) == NULL)
|
|
45 goto error;
|
|
46 *p = 0;
|
|
47
|
|
48 new_cmdline = alloca (MAX_PATH + strlen (cmdline) + 1);
|
|
49 strcpy (new_cmdline, modname);
|
|
50
|
|
51 #ifdef CHOOSE_NEWEST_EXE
|
|
52 {
|
|
53 /* Silly hack to allow new versions to be installed on
|
|
54 server even when current version is in use. */
|
|
55
|
|
56 char * best_name = alloca (MAX_PATH + 1);
|
|
57 FILETIME best_time = {0,0};
|
|
58 WIN32_FIND_DATA wfd;
|
|
59 HANDLE fh;
|
|
60 p = new_cmdline + strlen (new_cmdline);
|
|
61 strcpy (p, "\\emacs*.exe ");
|
|
62 fh = FindFirstFile (new_cmdline, &wfd);
|
|
63 if (fh == INVALID_HANDLE_VALUE)
|
|
64 goto error;
|
|
65 do
|
|
66 {
|
|
67 if (wfd.ftLastWriteTime.dwHighDateTime > best_time.dwHighDateTime
|
|
68 || (wfd.ftLastWriteTime.dwHighDateTime == best_time.dwHighDateTime
|
|
69 && wfd.ftLastWriteTime.dwLowDateTime > best_time.dwLowDateTime))
|
|
70 {
|
|
71 best_time = wfd.ftLastWriteTime;
|
|
72 strcpy (best_name, wfd.cFileName);
|
|
73 }
|
|
74 }
|
|
75 while (FindNextFile (fh, &wfd));
|
|
76 FindClose (fh);
|
|
77 *p++ = '\\';
|
|
78 strcpy (p, best_name);
|
|
79 strcat (p, " ");
|
|
80 }
|
|
81 #else
|
|
82 strcat (new_cmdline, "\\emacs.exe ");
|
|
83 #endif
|
|
84
|
|
85 /* Append original arguments if any; first look for -wait as first
|
|
86 argument, and apply that ourselves. */
|
|
87 if (strncmp (cmdline, "-wait", 5) == 0)
|
|
88 {
|
|
89 wait_for_child = TRUE;
|
|
90 cmdline += 5;
|
|
91 }
|
|
92 strcat (new_cmdline, cmdline);
|
|
93
|
|
94 /* Set emacs_dir variable if runemacs was in "%emacs_dir%\bin". */
|
|
95 if ((p = strrchr (modname, '\\')) && stricmp (p, "\\bin") == 0)
|
|
96 {
|
|
97 *p = 0;
|
|
98 for (p = modname; *p; p++)
|
|
99 if (*p == '\\') *p = '/';
|
|
100 SetEnvironmentVariable ("emacs_dir", modname);
|
|
101 }
|
|
102
|
|
103 memset (&start, 0, sizeof (start));
|
|
104 start.cb = sizeof (start);
|
|
105 start.dwFlags = STARTF_USESHOWWINDOW;
|
|
106 start.wShowWindow = SW_HIDE;
|
|
107
|
|
108 sec_attrs.nLength = sizeof (sec_attrs);
|
|
109 sec_attrs.lpSecurityDescriptor = NULL;
|
|
110 sec_attrs.bInheritHandle = FALSE;
|
|
111
|
|
112 if (CreateProcess (NULL, new_cmdline, &sec_attrs, NULL, TRUE, 0,
|
|
113 NULL, NULL, &start, &child))
|
|
114 {
|
|
115 if (wait_for_child)
|
|
116 {
|
|
117 WaitForSingleObject (child.hProcess, INFINITE);
|
|
118 GetExitCodeProcess (child.hProcess, &ret_code);
|
|
119 }
|
|
120 CloseHandle (child.hThread);
|
|
121 CloseHandle (child.hProcess);
|
|
122 }
|
|
123 else
|
|
124 goto error;
|
|
125 return (int) ret_code;
|
|
126
|
|
127 error:
|
|
128 MessageBox (NULL, "Could not start Emacs.", "Error", MB_ICONSTOP);
|
|
129 return 1;
|
|
130 }
|