0

I'm trying to run odbcad32.exe (Windows 10, 64 bit) from a program written in the Lazarus IDE. I tried building in 32 & 64 bit and always got an exception with this text:

Failed to execute : 740

Apparently, after some googling, it means that my program needs admin privileges, but I AM running it from an admin account. I can run odbcad32 from an icon on the desktop, from the console, and from a much older program written in Delphi IDE normally (no Windows questions about privileges), but not from a Lazarus code.

So what am I doing wrong? The program has to be crossplatform. I'll run something else in Unix/OSX.

Here is the source code ('smesse' is a procedure to show error messages):

uses SysUtils, Process;
...
function RunExe1(exefilename:string;var em:string):boolean;
var
   exe:TProcess;
   f:integer;
begin
     exe:=TProcess.Create(nil);
     exe.InheritHandles:=false;
     exe.Executable:=exefilename;
     for f:=1 to GetEnvironmentVariableCount do
       exe.Environment.Add(GetEnvironmentString(f));
     exe.Options:=exe.Options-[poWaitOnExit];
     try
        exe.Execute; // <--- Exception is raised here
        result:=true;
        em:='';
     except
       on e:exception do
       begin
         result:=false;
         em:=e.Message;
       end
     end;
     exe.Free;
end;
procedure Tselectdbw.odbcmanClick(Sender: TObject);
var
   em:string;
begin
     //if not RunExe1('C:\Windows\syswow64\odbcad32.exe',em) then smesse(em);
     //if not RunExe1('C:\Windows\system32\odbcad32.exe',em) then smesse(em);
     if not RunExe1('odbcad32.exe',em) then smesse(em);
end;

I ran it with a full path too. BTW, in Delphi I used WinAPI and it works fine even today:

procedure ShellOpenFile(hWnd:HWND;filename:string);
begin
     shellexecute(hWnd,'open',
      pansichar(filename),nil,nil,SW_SHOW)
end;
Alex Xmas
  • 1
  • 2
  • 1
    Under UAC, admin users run with restricted token. You need to explicitly ask to run the process with full admin token. – David Heffernan Jul 18 '17 at 09:01
  • ^^ in other words, you must add a manifest to your application with requestedExecutionLevel set to `requireAdministrator`, for more info look [here](https://stackoverflow.com/questions/6226976/how-to-add-manifest-requestedprivileges-info-into-delphi-project) – whosrdaddy Jul 18 '17 at 13:25
  • Or, call `ShellExecute` with the `runas` verb instead of `open`. – Remy Lebeau Jul 18 '17 at 13:40
  • Indeed, being logged in under an administrator account is not the same thing as running a process "as administrator". – Jerry Dodge Jul 18 '17 at 14:34
  • Thank you, everyone. I completely forgot about the UAC. Tried calling CreateProcess directly and got the same result. I guess, I'll have to use conditional directives for different OSes and ShellExecute in Windows. My app doesn't need admin privileges, I just want it to run odbcad32 if the user is allowed to run it. – Alex Xmas Jul 20 '17 at 06:08

0 Answers0