static void RunScript(HWND hwnd)
{
TCHAR szProgDir[MAX_PATH], szPhpExe[MAX_PATH], szUtils[MAX_PATH], szScriptFile[MAX_PATH], szBuffer[BUFFER], szCommandline[BUFFER];
STARTUPINFO si;
PROCESS_INFORMATION pi;
GetModuleFileName(ghInstance, szProgDir, DIMOF(szProgDir));
PathRemoveFileSpec(szProgDir);
lstrcpy(szPhpExe, szProgDir);
PathAppend(szPhpExe, _T("\\php\\php.exe"));
lstrcpy(szUtils, szProgDir);
PathAppend(szUtils, _T("\\utils"));
SetEnvironmentVariable(_T("PATH"), szUtils);
lstrcpy(szScriptFile, szProgDir);
wsprintf(szScriptFile, _T("%s\\scripts\\%s.php"), szProgDir, gCfg.szLastUsedScript);
iLastUsed = 0;
ZeroMemory(szBuffer, DIMOF(szBuffer));
if (EnumChildWindows(GetParent(hwnd), BuildCommandline, (LPARAM) szBuffer) == 0)
return;
ZeroMemory(szCommandline, DIMOF(szCommandline));
wsprintf(szCommandline, _T(" -n -f \"%s\" -- %s"), szScriptFile, szBuffer);
INITSTRUCT(si, TRUE);
INITSTRUCT(pi, FALSE);
EnableWindow(GetDlgItem(GetParent(hwnd), IDOK), FALSE);
SetCursor(LoadCursor(ghInstance, IDC_WAIT));
if (CreateProcess(szPhpExe, szCommandline, NULL, NULL, TRUE, (gCfg.fConsole) ? NORMAL_PRIORITY_CLASS|CREATE_NO_WINDOW : NORMAL_PRIORITY_CLASS, NULL, NULL, &si, &pi))
{
WaitForSingleObject(pi.hProcess, INFINITE);
EnumChildWindows(GetParent(hwnd), ReloadClipboard, 0);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
MessageBox(hwnd, _T("Script execution done."), _T("Info"), MB_OK|MB_ICONINFORMATION);
}
else
MessageBox(hwnd, _T("Script execution failed."), _T("Error"), MB_OK|MB_ICONERROR);
SetCursor(LoadCursor(ghInstance, IDC_ARROW));
EnableWindow(GetDlgItem(GetParent(hwnd), IDOK), TRUE);
}
Edit: Last two lines moved out of if block, or the cursor and window might not get restored, if it fails.