@@ -47,31 +47,70 @@ LanguageTable kLanguages[18] = {
4747#ifdef _WIN32
4848namespace
4949{
50+ //
51+ // @bug: this routine fails if the executable is called from a directory
52+ // wuth spaces in it. This routine quores the command with spaces
53+ // but then _execv fails to run.
54+ //
5055 int win32_execv ()
5156 {
5257 // Get the full command line string
5358 LPWSTR lpCmdLine = GetCommandLineW ();
5459
55- // Enclose the command line string in quotes
56- size_t len = wcslen (lpCmdLine) + 3 ; // 2 for quotes, 1 for null terminator
57- LPWSTR cmd = (LPWSTR) malloc (len * sizeof (wchar_t ));
58- if (cmd == NULL ) {
59- wprintf (L" Failed to allocate memory for command line\n " );
60+ // Parse the command line string into an array of arguments
61+ int argc;
62+ LPWSTR* argv = CommandLineToArgvW (lpCmdLine, &argc);
63+
64+ if (argv == NULL ) {
65+ wprintf (L" Failed to parse command line\n " );
6066 return EXIT_FAILURE;
6167 }
62- swprintf_s (cmd, len, L" \" %s\" " , lpCmdLine);
6368
64- // Call _wsystem with the quoted command string
65- int result = _wsystem (cmd);
69+ // Construct a new array of arguments
70+ LPWSTR* new_argv = (LPWSTR*) malloc ((argc + 1 ) * sizeof (LPWSTR));
71+ if (new_argv == NULL ) {
72+ wprintf (L" Failed to allocate memory for command line arguments\n " );
73+ return EXIT_FAILURE;
74+ }
75+ new_argv[0 ] = argv[0 ];
76+ for (int i = 1 ; i < argc; i++) {
77+ new_argv[i] = argv[i];
78+ }
79+ new_argv[argc] = NULL ;
80+
81+ // Enclose argv[0] in double quotes if it contains spaces
82+ LPWSTR cmd = argv[0 ];
83+ if (wcschr (cmd, L' ' ) != NULL ) {
84+ size_t len = wcslen (cmd) + 3 ; // 2 for quotes, 1 for null terminator
85+ LPWSTR quoted_cmd = (LPWSTR) malloc (len * sizeof (wchar_t ));
86+ if (quoted_cmd == NULL ) {
87+ wprintf (L" Failed to allocate memory for command line\n " );
88+ return EXIT_FAILURE;
89+ }
90+ swprintf_s (quoted_cmd, len, L" \" %s\" " , cmd);
91+ cmd = quoted_cmd;
92+ }
93+
94+ // Call _wexecv with the command string and arguments in separate parameters
95+ int result = _wexecv (cmd, new_argv);
6696
6797 if (result == -1 ) {
68- perror (" _wsystem " );
98+ perror (" _wexecv " );
6999 return EXIT_FAILURE;
70100 }
71101
72- // Free the memory used by the command string
73- free (cmd );
102+ // Free the memory used by the new array of arguments
103+ free (new_argv );
74104
105+ // Free the memory used by the quoted command line, if necessary
106+ if (cmd != argv[0 ]) {
107+ free (cmd);
108+ }
109+
110+ // Free the array of arguments
111+ LocalFree (argv);
112+
113+
75114 exit (EXIT_SUCCESS);
76115 }
77116}
@@ -105,7 +144,7 @@ void check_language(PreferencesUI* uiPrefs, int& language_index)
105144 base.flush ();
106145
107146 // deleete ViewerUI
108- mrv::Preferences::ui->uiMain ->hide ();
147+ // mrv::Preferences::ui->uiMain->hide();
109148
110149#ifdef _WIN32
111150 win32_execv ();
0 commit comments