Closing cmd window after opening it with shellexecute()
Good Day.
I have an Powerbuilder application that triggers a batch file to start up a database server.
Below the contents of the batch file:
"C:Program FilesSybaseSQL Anywhere 8win32dbsrv8.exe" -c 8m -n DEMO "C:loadcondb_demodemo.db"
This all works fine.
However I would like the command window to close automatically after the execution of the batch script. I have spend most of today reading this website and trying options that might work, like adding start, exit, /exit, /c but none work correct. With the start option in front it has a problem with the database switch -c. Repositioning the string quotation marks withing the batch file has undesirable effect on the database startup. However adding /exit at the end – first the the database promts a mssg ‘Cant read file /exit’ and then the cmd prompt closes – so, something is working but not 100%.
Anybody can enlighten me?
Thanks
Alex
Shellexecute a cmd prompt with a specific working directory
Microsoft added this as a security feature starting in Windows 8. Whenever cmd.exe detects it’s running elevated, it ignores its launch parameters and always starts in %SystemRoot%System32
. You cannot override this behavior.
You can, however, change directory as the first command in the prompt. To do this, set lpFile
to "cmd.exe"
as normal. Then set lpParameters
to "/k cd /d d:yourpath"
. CMD will change directories immediately on launch, and then stay open for further commands.
Как вывести сообщение после выполнения shellexecute?
Ответ – никак.
Функция ShellExecute
не позволяет получить информацию о времени жизни программы. Данная функция является устаревшей и не рекомендуется к использованию.
Вам нужно вызвать функцию ShelExecuteEx
или CreateProcess
(в данном конкретном случае лучше последнюю), получить хендл запущенного процесса и дождаться завершения этого процесса при помощи функции WaitForSingleObject
var
LCmd: string;
LInfo: TStartupInfo;
LPI: TProcessInformation;
begin
// Заполняем структуры
LCmd := 'cmd.exe ' SPar;
FillChar(LInfo, SizeOf(LInfo), 0);
LInfo.cb := SizeOf(LInfo);
LInfo.dwFlags := STARTF_USESHOWWINDOW;
LInfo.wShowWindow := SW_HIDE;
// Запускаем программу
Win32Check(CreateProcess(
'cmd.exe', // lpApplicationName,
PChar(LCmd), // lpCommandLine,
nil, // lpProcessAttributes,
nil, // lpThreadAttributes,
False, // bInheritHandles,
CREATE_NO_WINDOW, // dwCreationFlags,
nil, // lpEnvironment,
nil, // lpCurrentDirectory,
LInfo, // lpStartupInfo,
LPI // lpProcessInformation
));
try
// Закрываем хендл главного потока (он нам не нужен)
CloseHandle(LPI.hThread);
// Ждем завершения процесса
Win32Check(WaitForSingleObject(LPI.hProcess, INFINITE) <> WAIT_FAILED);
finally
// Закрываем хендл процесса
CloseHandle(LPI.hProcess);
end;
ShowMessage('Программа завершена');
end;