The .bat file works correctly when launched from a Delphi application with administrative rights, but without rights it simply does not find the file. The application is installed in the Program Files folder. In other non-system folders, without rights is performed correctly. How to bypass this restriction?
procedure RunBatchFile; var List: TStringList; ShExecInfo: SHELLEXECUTEINFO; begin if not FileExists(ExtractFilePath(Application.ExeName) + 'config.ini') then try try if (FileExists(ExtractFilePath(Application.ExeName) + 'get_ip.bat')) or (FileExists(ExtractFilePath(Application.ExeName) + 'ip.txt')) then begin DeleteFile(ExtractFilePath(Application.ExeName) + 'get_ip.bat'); DeleteFile(ExtractFilePath(Application.ExeName) + 'ip.txt'); end; List := TStringList.Create; List.Add('@echo off'); List.Add('cls'); List.Add('for /f "usebackq tokens=4" %%A in (`route print ^| findstr "\<0.0.0.0"`) do echo %%A > ip.txt'); List.SaveToFile(ExtractFilePath(Application.ExeName) + 'get_ip.bat'); ZeroMemory(@ShExecInfo, SizeOf(SHELLEXECUTEINFO)); with ShExecInfo do begin cbSize := SizeOf(SHELLEXECUTEINFO); fMask := SEE_MASK_NOCLOSEPROCESS; lpFile := PChar(ExtractFilePath(Application.ExeName) + 'get_ip.bat'); nShow := SW_HIDE; end; ShellExecuteEx(@ShExecInfo); WaitForSingleObject(ShExecInfo.hProcess, INFINITE); List.LoadFromFile(ExtractFilePath(Application.ExeName) + 'ip.txt'); edtIP.Text := Trim(List.Text); edtPort.Text := '5432'; edtUser.Text := 'postgres'; edtPassword.Text := 'passwd'; Exit; except on E: Exception do ShowMessage(SysErrorMessage(GetLastError)); end; finally List.Free; end;
GetLastErrorreturn? - Anton ShchyrovShellExecuteExrunsget_ip.bat, but the code itself does not save data to the Program Files \ MyApp folder. When running from the administrator, the code is executed correctly, without permissions - "Access denied" - user214690