使用TFileRun和regsvr32注册DLL时找不到文件

时间:2012-08-25 15:28:34

标签: delphi regsvr32

我今天发现了类TFileRun,帮助我用regsvr32注册一个DLL文件。我的代码是这样的:

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;

目录存在且DLL文件也存在,但由于某些未知原因,我从regsvr32收到此错误消息:

enter image description here

看起来它只是dir名称的一部分...为什么会发生这种情况?!

3 个答案:

答案 0 :(得分:9)

\Java Update文件夹包含空格,因此您必须引用整个目录路径:

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

正如另一个回答所提到的,尽管如此,最好在代码中自己进行注册。它没有真正的工作;它只是加载DLL并要​​求注册程序。由于你只是注册而不是注册,所以工作真的很少。这是一个例子(从旧的Borland演示代码重新编写):

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;

这样称呼 - 使用LoadLibrary执行此操作时,您无需担心双引号:

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

  RegisterAxLib(sFile);
end;

答案 1 :(得分:4)

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

答案 2 :(得分:3)

真的,启动外部exe只是为了调用一个函数似乎有点矫枉过正。

所有RegSvr32都会加载DLL并调用3个预定义函数之一(取决于是否存在-i和-u键,4个变体)。

这一切都可以通过您的应用程序完成 - 以更可靠的方式。如果在某些系统上你没有例如路径中的regsvr32.exe怎么办?

关于这样的草图,你将它适应你的应用程序和你的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;