2 Simple program to start Emacs with its console window hidden.
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. */
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
21 /* #define CHOOSE_NEWEST_EXE */
29 #if defined(__CYGWIN32__)
30 #include <sys/types.h>
35 WinMain (HINSTANCE hSelf, HINSTANCE hPrev, LPSTR cmdline, int nShow)
38 SECURITY_ATTRIBUTES sec_attrs;
39 SECURITY_DESCRIPTOR sec_desc;
40 PROCESS_INFORMATION child;
41 int wait_for_child = FALSE;
45 char modname[MAX_PATH];
47 if (!GetModuleFileName (NULL, modname, MAX_PATH))
49 if ((p = strrchr (modname, '\\')) == NULL)
53 new_cmdline = alloca (MAX_PATH + strlen (cmdline) + 1);
54 strcpy (new_cmdline, modname);
56 #ifdef CHOOSE_NEWEST_EXE
58 /* Silly hack to allow new versions to be installed on
59 server even when current version is in use. */
61 char * best_name = alloca (MAX_PATH + 1);
62 FILETIME best_time = {0,0};
65 p = new_cmdline + strlen (new_cmdline);
66 strcpy (p, "\\xemacs*.exe ");
67 fh = FindFirstFile (new_cmdline, &wfd);
68 if (fh == INVALID_HANDLE_VALUE)
72 if (wfd.ftLastWriteTime.dwHighDateTime > best_time.dwHighDateTime
73 || (wfd.ftLastWriteTime.dwHighDateTime == best_time.dwHighDateTime
74 && wfd.ftLastWriteTime.dwLowDateTime > best_time.dwLowDateTime))
76 best_time = wfd.ftLastWriteTime;
77 strcpy (best_name, wfd.cFileName);
80 while (FindNextFile (fh, &wfd));
83 strcpy (p, best_name);
87 #if defined(__CYGWIN32__)
90 char sym_link_name[MAX_PATH+1], real_name[MAX_PATH+1];
92 strcpy(sym_link_name, new_cmdline);
93 strcat(sym_link_name, "\\xemacs");
94 if (lstat(sym_link_name, &stbuf) == 0)
96 if ((stbuf.st_mode & S_IFLNK) == S_IFLNK)
98 if (readlink(sym_link_name, real_name, sizeof(real_name)) == -1)
100 MessageBox (NULL, "Error reading symbolic link for xemacs",
101 "Error", MB_ICONSTOP);
106 strcat(new_cmdline, "\\");
107 strcat(new_cmdline, real_name);
108 strcat(new_cmdline, " ");
112 strcat(new_cmdline, "\\xemacs ");
116 MessageBox (NULL, "can't locate XEmacs executable",
117 "Error", MB_ICONSTOP);
122 strcat (new_cmdline, "\\xemacs.exe ");
126 /* Append original arguments if any; first look for -wait as first
127 argument, and apply that ourselves. */
128 if (strncmp (cmdline, "-wait", 5) == 0)
130 wait_for_child = TRUE;
133 strcat (new_cmdline, cmdline);
135 /* Set emacs_dir variable if runemacs was in "%emacs_dir%\bin". */
136 if ((p = strrchr (modname, '\\')) && stricmp (p, "\\bin") == 0)
139 for (p = modname; *p; p++)
140 if (*p == '\\') *p = '/';
141 SetEnvironmentVariable ("emacs_dir", modname);
144 memset (&start, 0, sizeof (start));
145 start.cb = sizeof (start);
146 start.dwFlags = STARTF_USESHOWWINDOW;
147 start.wShowWindow = SW_HIDE;
149 sec_attrs.nLength = sizeof (sec_attrs);
150 sec_attrs.lpSecurityDescriptor = NULL;
151 sec_attrs.bInheritHandle = FALSE;
153 if (CreateProcess (NULL, new_cmdline, &sec_attrs, NULL, TRUE, 0,
154 NULL, NULL, &start, &child))
158 WaitForSingleObject (child.hProcess, INFINITE);
159 GetExitCodeProcess (child.hProcess, &ret_code);
161 CloseHandle (child.hThread);
162 CloseHandle (child.hProcess);
166 return (int) ret_code;
169 MessageBox (NULL, "Could not start XEmacs.", "Error", MB_ICONSTOP);