Bug 575870 - Enable the firefox button on xp themed, classic, and aero basic. r=dao...
[mozilla-central.git] / toolkit / xre / nsWindowsWMain.cpp
blob7f81c22580df1f07aed3959ab6636df85eedee69
1 // This file is a .cpp file meant to be included in nsBrowserApp.cpp and other
2 // similar bootstrap code. It converts wide-character windows wmain into UTF-8
3 // narrow-character strings.
5 #ifndef XP_WIN
6 #error This file only makes sense on Windows.
7 #endif
9 #include "nsUTF8Utils.h"
11 #if defined(_MSC_VER) && defined(_M_IX86) && defined(XRE_WANT_DLL_BLOCKLIST)
12 #include "nsWindowsDllBlocklist.cpp"
13 #else
14 #undef XRE_WANT_DLL_BLOCKLIST
15 #endif
18 #ifdef __MINGW32__
20 /* MingW currently does not implement a wide version of the
21 startup routines. Workaround is to implement something like
22 it ourselves. See bug 411826 */
24 #include <shellapi.h>
26 int wmain(int argc, WCHAR **argv);
28 int main(int argc, char **argv)
30 LPWSTR commandLine = GetCommandLineW();
31 int argcw = 0;
32 LPWSTR *argvw = CommandLineToArgvW(commandLine, &argcw);
33 if (!argvw)
34 return 127;
36 int result = wmain(argcw, argvw);
37 LocalFree(argvw);
38 return result;
40 #endif /* __MINGW32__ */
42 #define main NS_internal_main
44 int main(int argc, char **argv);
46 static char*
47 AllocConvertUTF16toUTF8(const WCHAR *arg)
49 // be generous... UTF16 units can expand up to 3 UTF8 units
50 int len = wcslen(arg);
51 char *s = new char[len * 3 + 1];
52 if (!s)
53 return NULL;
55 ConvertUTF16toUTF8 convert(s);
56 convert.write(arg, len);
57 convert.write_terminator();
58 return s;
61 static void
62 FreeAllocStrings(int argc, char **argv)
64 while (argc) {
65 --argc;
66 delete [] argv[argc];
69 delete [] argv;
72 #ifdef WINCE
73 /** argc/argv are in/out parameters */
74 void ExtractEnvironmentFromCL(int &argc, char **&argv)
76 for (int x = argc - 1; x >= 0; x--) {
77 if (!strncmp(argv[x], "--environ:", 10)) {
78 char* key_val = strdup(argv[x]+10);
79 putenv(key_val);
80 free(key_val);
81 argc -= 1;
82 char *delete_argv = argv[x];
83 if (x < argc) /* if the current argument is not at the tail, shift following arguments. */
84 memcpy(&argv[x], &argv[x+1], (argc - x) * sizeof(char*));
85 delete [] delete_argv;
89 #endif
91 int wmain(int argc, WCHAR **argv)
93 #ifdef XRE_WANT_DLL_BLOCKLIST
94 SetupDllBlocklist();
95 #endif
97 char **argvConverted = new char*[argc + 1];
98 if (!argvConverted)
99 return 127;
101 for (int i = 0; i < argc; ++i) {
102 argvConverted[i] = AllocConvertUTF16toUTF8(argv[i]);
103 if (!argvConverted[i]) {
104 return 127;
107 #ifdef WINCE
108 ExtractEnvironmentFromCL(argc, argvConverted);
109 #endif
110 argvConverted[argc] = NULL;
112 // need to save argvConverted copy for later deletion.
113 char **deleteUs = new char*[argc+1];
114 if (!deleteUs) {
115 FreeAllocStrings(argc, argvConverted);
116 return 127;
118 for (int i = 0; i < argc; i++)
119 deleteUs[i] = argvConverted[i];
120 int result = main(argc, argvConverted);
122 delete[] argvConverted;
123 FreeAllocStrings(argc, deleteUs);
125 return result;