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

HwT*_*rap 1 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;
Run Code Online (Sandbox Code Playgroud)

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

在此输入图像描述

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

Ken*_*ite 9

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

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

正如另一个回答所提到的那样,尽管如此,最好自己在代码中进行注册.它没有真正的工作; 它只是加载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;
Run Code Online (Sandbox Code Playgroud)

这样称呼 - 使用时不需要担心双引号LoadLibrary:

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

  RegisterAxLib(sFile);
end;
Run Code Online (Sandbox Code Playgroud)

  • @HwTrap接受最佳答案而不是第一个答案更重要.+1给肯.肯花时间用文字解释.这在我看来非常有价值. (2认同)