我正在尝试在 Delphi 程序中注册 Active X .ocx 库,我尝试了以下代码,但没有成功,没有错误,并且程序运行了所有代码,但是当它完成时,Active X 库还没有运行已注册。我究竟做错了什么 ?
procedure RegisterOCX;
type
TRegFunc = function : HResult; stdcall;
var
ARegFunc : TRegFunc;
aHandle : THandle;
ocxPath,AppPath : string;
begin
GetDir(0, AppPath);
try
ocxPath := AppPath + '\VOIP.ocx';
aHandle := LoadLibrary(PChar(ocxPath));
if aHandle <> 0 then
begin
ARegFunc := GetProcAddress(aHandle,'DllRegisterServer');
if Assigned(ARegFunc) then
begin
ExecAndWait('regsvr32','/s ' + ocxPath);
end;
FreeLibrary(aHandle);
end;
except
ShowMessage('Unable to register ');
end;
end;
function ExecAndWait(const ExecuteFile, ParamString : string): boolean;
var
SEInfo: TShellExecuteInfo;
ExitCode: DWORD;
begin
FillChar(SEInfo, SizeOf(SEInfo), 0);
SEInfo.cbSize := SizeOf(TShellExecuteInfo);
with SEInfo do begin
fMask := SEE_MASK_NOCLOSEPROCESS;
Wnd := Application.Handle;
lpFile := PChar(ExecuteFile);
lpParameters := PChar(ParamString);
nShow := SW_HIDE;
end;
if ShellExecuteEx(@SEInfo) then
begin
repeat
Application.ProcessMessages;
GetExitCodeProcess(SEInfo.hProcess, ExitCode);
until (ExitCode <> STILL_ACTIVE) or Application.Terminated;
Result:=True;
end
else Result:=False;
end;
最佳答案
使用 regsvr32 让您的生活变得艰难。你已经完成了 99% 的事,不再需要任何东西。无需调用 regsvr32,只需调用 DllRegisterServer
。毕竟,这就是 regsvr32 要做的全部事情!
您的代码变为:
if Assigned(ARegFunc) then
OleCheck(ARegFunc());
然后您可以完全删除 ExecAndWait
。这很好,因为它让我不必讨论繁忙的循环和泄漏的句柄!
将您称为 ARegFunc
的变量重命名为 DllRegisterServer
对我来说是有意义的。因此代码可能如下所示:
aHandle := LoadLibrary(PChar(ocxPath));
if aHandle = 0 then
RaiseLastWin32Error;
try
DllRegisterServer := GetProcAddress(aHandle,'DllRegisterServer');
if Assigned(DllRegisterServer) then
OleCheck(DllRegisterServer());
finally
FreeLibrary(aHandle);
end;
调用 DllRegisterServer
最可能的失败模式是无法运行提升的注册代码。
顺便说一句,LoadLibrary
返回 HMODULE
而不是 THandle
。
关于delphi - 如何在 Delphi XE4 中注册 Active X .ocx 库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19946814/