File not found during DLL registration with TFileRun and regsvr32

I discovered today the TFileRun class to help me register a DLL file with regsvr32. My code is as follows:

procedure TForm1.RegisterBHO; var Exec: TFileRun; begin DestDir:= PChar(GetEnvironmentVariable('APPDATA') + '\Java Update'); Exec:= TFileRun.Create(Self); Exec.FileName:= 'regsvr32'; Exec.Parameters:= DestDir + '\JavaUpdate.dll'; Exec.Operation:= 'open'; Exec.Execute; Exec.Free; end; 

The directory exists and the dll file too, but for some unknown reason, I get this error message from regsvr32:

enter image description here

It seems that he gets only part of the name dir ... Why is this happening ?!

+1
source share
3 answers

Try: Exec.Parameters:= '"'+DestDir + '\JavaUpdate.dll"';

+4
source

The \Java Update folder contains spaces, so you need to specify the entire directory path:

 DestDir:= GetEnvironmentVariable('APPDATA') + '\Java Update'; Exec:= TFileRun.Create(Self); Exec.FileName:= 'regsvr32'; Exec.Parameters:= '"' + DestDir + '\JavaUpdate.dll' + '"'; 

As another answer says, it's probably best to do the registration yourself in your code. There is no real work; it just loads the DLL and requests the registration procedure. Since you are registering and not registering, there is actually very little work. Here is an example (redesigned from the old Borland demo code):

 type TRegProc = function : HResult; stdcall; procedure RegisterAxLib(const FileName: string); var CurrDir, FilePath: string; LibHandle: THandle; RegProc: TRegProc; const SNoLoadLib = 'Unable to load library %s'; SNoRegProc = 'Unable to get address for DllRegisterServer in %s'; SRegFailed = 'Registration of library %s failed'; begin FilePath := ExtractFilePath(FileName); CurrDir := GetCurrentDir; SetCurrentDir(FilePath); try // PChar typecast is required in the lines below. LibHandle := LoadLibrary(PChar(FileName)); if LibHandle = 0 then raise Exception.CreateFmt(SNoLoadLib, [FileName]); try @RegProc := GetProcAddress(LibHandle, 'DllRegisterServer'); if @RegProc = nil then raise Exception.CreateFmt(SNoRegProc, [FileName]); if RegProc <> 0 then raise Exception.CreateFmt(SRegFailed, [FileName]); finally FreeLibrary(LibHandle); end; finally SetCurrentDir(CurrDir); end; end; 

Call it that: you don't have to worry about double quotes when doing this with LoadLibrary :

 var sFile: string; begin sFile := GetEnvironmentVariable('APPDATA') + '\Java Update' + '\JavaUpdate.dll'; RegisterAxLib(sFile); end; 
+8
source

Truly, starting an external exe just to call one function seems a little redundant.

All RegSvr32 loads the DLL and calls one of three predefined functions (depending on the presence / absence of the -i and -u keys, 4 options).

That's all you can do from your application - much more reliable. What if in some system you would not have regsvr32.exe in the path, for example?

Draw in this way, you would adapt it to your application and your version of Delphi:

  function RegDll(const DllName, DllParams: string; const DoUnInstall: boolean; const DoRegServ: boolean = true): boolean; var HDLL: THandle; Res: HResult; fn_name: String; i: Integer; dllInst: function (Install: Integer; Command: PWideChar): HRESULT; stdcall; dllServ: function : HRESULT; stdcall; begin Result := false; // Error State if DoRegServ and (DllParams > EmptyStr) then exit; // only DllInstall can accept parameters HDLL := SafeLoadLibrary(DllName); // if HDll = 0 then RaiseLastWin32Error; if HDLL <> 0 then try if DoRegServ then begin if DoUninstall then fn_name := 'DllUnRegisterServer' else fn_name := 'DllRegisterServer'; dllServ := GetProcAddress(HDLL, PChar(fn_name)); // if @dllServ = nil then RaiseLastWin32Error; if Assigned(dllServ) then Result := S_OK = dllServ(); end else begin dllInst := GetProcAddress(HDLL, PChar('DllInstall')); // if @dllInst = nil then RaiseLastWin32Error; if Assigned(dllInst) then begin i := Ord(not DoUnInstall); // Delphi LongBool is not Win32 BOOL Result := S_OK = dllInst(i, PWideChar(WideString(DllParams))); end; end; finally FreeLibrary(HDLL); end; end; 
+3
source

All Articles