+/* Run a cmd.exe command */
+int run_cmd(char *cmd, HWND hwnd)
+{
+ int result;
+ int cmdlen;
+ STARTUPINFO si;
+ PROCESS_INFORMATION pi;
+ DWORD exit_code;
+
+ /* 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);
+}