【发布时间】:2020-05-26 13:41:52
【问题描述】:
我正在使用 Delphi 附带的tregsvr.exe 程序来注册 OCX。
使用这个程序而不是regsrv32.exe 的原因是可以传递-c 参数,这将只允许当前用户注册。
我通过ExecAndWait()函数执行程序,复制如下。它使用ShellExecuteEx()(而不是CreateProcess()),正是因为(只要我不设法为当前用户传递参数)我必须要求提升,这是通过传递布尔值Adm来完成的,它修复了'runas'(我读到了关于应用清单的另一种方法,但没有设法让它工作,也许这是另一个问题)。
function ExecAndWait(const ExecuteFile, ParamString : string; Adm: boolean): 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);
If Adm then lpVerb:='runas';
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;
在此使用示例中,变量 Path 是 tregsvr.exe 的完整路径,Server 是 OCX 的完整路径。它运行良好(它完成了注册工作):
ExecandWait(Path,Chr(9)+Server+Chr(9), True);
但是当我尝试传递 -c 或其他参数时,我的问题就出现了,例如在任何这些尝试中,函数都不成功:
ExecandWait(Path,'-c '+Chr(9)+Server+Chr(9), True);
ExecandWait(Path,'\c '+Chr(9)+Server+Chr(9), True);
【问题讨论】:
-
你为什么要这样做?通常在安装时,您只需在部署应用程序时编写注册表项。您不希望向另一个进程发起攻击,并且也必须部署该进程。
-
@DavidHeffernan 主要是因为我想在没有管理权限的机器上进行部署。我认为仅将这个参数 -c 用于“当前用户”可能会奏效。 (我在这里使用 'runas' 仅用于测试目的。)而且 regsrv32 仅在 .ocx 在程序文件中时才有效,不要问我为什么...
-
ComObj.RegisterComserver可用于此目的,与RegOverridePredefkey一起用于注册表重定向。 -
您如何部署应用程序的其余部分?有安装包吗?
-
@DavidHeffernan 安装包就可以做到这一点,我在问什么。
标签: delphi server ocx shellexecuteex