[SOLARIS2]: Define SUNOS_5.
[emacs.git] / nt / runemacs.c
blob720c6ca7241e93d02dc489b8e121eac3aec32dbf
1 /*
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. */
8 #define WIN32
10 #include <windows.h>
11 #include <string.h>
12 #include <malloc.h>
14 int WINAPI
15 WinMain (HINSTANCE hSelf, HINSTANCE hPrev, LPSTR cmdline, int nShow)
17 STARTUPINFO start;
18 SECURITY_ATTRIBUTES sec_attrs;
19 SECURITY_DESCRIPTOR sec_desc;
20 PROCESS_INFORMATION child;
21 int wait_for_child = FALSE;
22 DWORD ret_code = 0;
23 char * new_cmdline;
24 char * p;
25 char modname[MAX_PATH];
27 if (!GetModuleFileName (NULL, modname, MAX_PATH))
28 goto error;
29 if ((p = strrchr (modname, '\\')) == NULL)
30 goto error;
31 *p = 0;
33 new_cmdline = alloca (MAX_PATH + strlen (cmdline) + 1);
34 strcpy (new_cmdline, modname);
35 strcat (new_cmdline, "\\emacs.exe ");
37 /* append original arguments if any; first look for -wait as first
38 argument, and apply that ourselves */
39 if (strncmp (cmdline, "-wait", 5) == 0)
41 wait_for_child = TRUE;
42 cmdline += 5;
44 strcat (new_cmdline, cmdline);
46 /* set emacs_dir variable if runemacs was in "%emacs_dir%\bin" */
47 if ((p = strrchr (modname, '\\')) && stricmp (p, "\\bin") == 0)
49 *p = 0;
50 SetEnvironmentVariable ("emacs_dir", modname);
53 memset (&start, 0, sizeof (start));
54 start.cb = sizeof (start);
55 start.dwFlags = STARTF_USESHOWWINDOW;
56 start.wShowWindow = SW_HIDE;
58 sec_attrs.nLength = sizeof (sec_attrs);
59 sec_attrs.lpSecurityDescriptor = NULL;
60 sec_attrs.bInheritHandle = FALSE;
62 if (CreateProcess (NULL, new_cmdline, &sec_attrs, NULL, TRUE, 0,
63 NULL, NULL, &start, &child))
65 if (wait_for_child)
67 WaitForSingleObject (child.hProcess, INFINITE);
68 GetExitCodeProcess (child.hProcess, &ret_code);
70 CloseHandle (child.hThread);
71 CloseHandle (child.hProcess);
73 else
74 goto error;
75 return (int) ret_code;
77 error:
78 MessageBox (NULL, "Could not start Emacs.", "Error", MB_ICONSTOP);
79 return 1;