+/* Run a cmd.exe command */
+int run_cmd(char *cmd, HWND hwnd)
+{
+ int result;
+ int cmdlen;
+ char *comspec;
+ STARTUPINFO si;
+ PROCESS_INFORMATION pi;
+ DWORD exit_code;
+
+ /* Get cmd location from environment */
+ comspec = getenv("COMSPEC");
+ if (comspec == NULL || strncmp(comspec, "", strlen(comspec) == 0))
+ {
+ MessageBox(hwnd, "Could not determine the location of "
+ "cmd.exe using the COMSPEC environment variable.",
+ "Error -- Failure Locating cmd.exe",MB_OK);
+ return(0);
+ }
+
+ /* Build command */
+ cmdlen = strlen(comspec) + 5 + strlen(cmd);
+ char finalcmd[cmdlen];
+ snprintf(finalcmd, cmdlen, "%s /c %s", comspec, cmd);
+
+ /* Log command being run */
+ log2file("%s: INFO: Running the following command (%s)", ARGV0, finalcmd);
+
+ ZeroMemory(&si, sizeof(si));
+ si.cb = sizeof(si);
+ ZeroMemory(&pi, sizeof(pi));
+
+ if(!CreateProcess(NULL, finalcmd, NULL, NULL, FALSE, CREATE_NO_WINDOW, NULL, NULL,
+ &si, &pi))
+ {
+ MessageBox(hwnd, "Unable to run command.",
+ "Error -- Failure Running Command",MB_OK);
+ return(0);
+ }
+
+ /* Wait until process exits */
+ WaitForSingleObject(pi.hProcess, INFINITE);
+
+ /* Get exit code from command */
+ result = GetExitCodeProcess(pi.hProcess, &exit_code);
+
+ /* Close process and thread */
+ CloseHandle(pi.hProcess);
+ CloseHandle(pi.hThread);
+
+ if (!result)
+ {
+ MessageBox(hwnd, "Could not determine exit code from command.",
+ "Error -- Failure Running Command",MB_OK);
+
+ return(0);
+ }
+
+ return(exit_code);
+}
+
+