Steve
Steve

Reputation: 652

Run with elevated rights fails on WIN7

I am developing using Delphi 6 on a WinXP system.

I have been using the following function to run a program with elevated rights.

function LB_RunAsAdminWait(hWnd:       HWND;
                           filename:   string;
                           Parameters: string): Boolean;
   var   sei: TShellExecuteInfo;                // shell execute info
begin

   Result := False;                             // default to failure

   FillChar(sei, SizeOf(sei), 0);
   sei.cbSize := SizeOf(sei);
   sei.Wnd    := hWnd;
   sei.fMask  := SEE_MASK_FLAG_NO_UI or SEE_MASK_NOCLOSEPROCESS;
   sei.lpVerb := 'runas';
   sei.lpFile := PChar(filename);
   sei.lpParameters := PChar(Parameters);
   sei.nShow  := SW_SHOWNORMAL;

   if ShellExecuteEx(@sei) then                 // if success
      Result := True;                           //    return sucess

   if sei.hProcess <> 0 then begin              // wait for process to finish
      while WaitForSingleObject(sei.hProcess, 50) = WAIT_TIMEOUT do
         Application.ProcessMessages;
      CloseHandle(sei.hProcess);
   end;

end;  // of function LB_RunAsAdminWait

How I call it:

if (LB_RunAsAdminWait(FPGM.Handle,'RegSvr32',FName+' /s') = False) then
   begin
     ShowMessage('WARNING: unable to register OCX');
     exit;
   end;

where FPGM.handle is the handle to my application and Fname is the OCX i want to register.

When I run it on a WIN7 machine it returns true(successful) but the OCX is not registered.

Any help would be appreciated.

Upvotes: 1

Views: 236

Answers (1)

David Heffernan
David Heffernan

Reputation: 612964

Most likely explanation is that this is a 32 bit vs 64 bit issue. The DLL is 64 bit, and you are running the 32 bit regsvr32. Or vice versa. Or the file system redirector is confounding you. You put the DLL in system32, but the redirector turns that into SysWow64.

The obvious way to debug it is to remove the silent switch and let regsvr32 tell you what went wrong.

As an aside, as you have discovered, you cannot use the return value of ShellExecuteEx to determine whether or not the server registration succeeded. The return value of ShellExecuteEx merely tells you whether or not the process started.

Upvotes: 4

Related Questions