I have 2 applications, program.exe and updater.exe written in Delphi5. The program works without administrator rights (and without a manifest), updater has a manifest with "requireAdministrator", because it must be able to write in Program-Folder to update program.exe.
The problem is to run the update program and let it wait until the program is closed. I found different ways on the Internet, but no one works (in most cases the first application launches the second application and waits for the second application to finish, in my case the 2nd application should wait for the 1st application to finish).
Updater must wait, thats easy
updater.exe
{$R manifest.res} label.caption:='Wait for program.exe closing'; repeat sleep(1000); until File is not open ProgramHandle := Read Handle from File WaitForSingleObject(ProgramHandle,INFINITE); label.caption:='program.exe CLOSED'; Do updates
Path 1
Launching the updater using CreateProcess:
program.exe
FillChar(siInfo, SizeOf(siInfo), 0); siInfo.cb := SizeOf(siInfo); saProcessAttributes.nLength := SizeOf(saProcessAttributes); saProcessAttributes.lpSecurityDescriptor := nil; saProcessAttributes.bInheritHandle := TRUE; saThreadAttributes.nLength := SizeOf(saThreadAttributes); saThreadAttributes.lpSecurityDescriptor := nil; saThreadAttributes.bInheritHandle := True; if CreateProcess(nil, PChar('updater.exe'), @saProcessAttributes, @saThreadAttributes, TRUE, NORMAL_PRIORITY_CLASS, nil, PChar(ExtractFilePath(Application.ExeName)), siInfo, piInfo) then begin DuplicateHandle(GetCurrentProcess, GetCurrentProcess, piInfo.hProcess, @MyHandle, PROCESS_QUERY_INFORMATION, TRUE, DUPLICATE_SAME_ACCESS) then Write MyHandle in a File end; Close program
It does nothing, it works only when the updater does not have a manifest with requireAdministrator. If I run the program with administrator privileges explizit, it also works.
Path 2 Run the updater using ShellExecuteEx:
program.exe
FillChar(Info, SizeOf(Info), Chr(0)); Info.cbSize := SizeOf(Info); Info.fMask := SEE_MASK_NOCLOSEPROCESS; Info.lpVerb := PChar('runas'); Info.lpFile := PChar('update.exe'); Info.lpDirectory := nil; Info.nShow := SW_RESTORE; ShellExecuteEx(@Info); MyHandle:=OpenProcess(PROCESS_ALL_ACCESS, False, GetCurrentProcessId()))); Write MyHandle in a File Close program
It doesnβt work, MyHandle has a different meaning every time I run this procedure (without restarting the program), so the updater cannot work with it.
So, I do not know how to run updater.exe and write the program.exe descriptor in the file.
I am not very familiar with these pieces of programming ... does anyone have an idea for my product?