我试图在安装过程中创建Windows防火墙规则,并且我很难理解我的命令是如何被错误地格式化的。ResultCode中返回的错误是“不正确的函数”。
procedure OpenFirewall;
var
WindowsVersion: Integer;
ResultCode: Integer;
W7Command: String;
WXPCommand: String;
begin
WXPCommand := 'netsh firewall add portopening TCP 12345 "MyApp"'
W7Command := 'netsh advfirewall add rule name="MyApp" dir=in action=allow protocol=TCP localport=12345';
WindowsVersion := GetWindowsVersion();
case WindowsVersion of
5:
begin
Exec(ExpandConstant('{cmd}'), '/C ' + WXPCommand, '', SW_HIDE, ewWaitUntilTerminated, ResultCode);
end;
6:
begin
Exec(ExpandConstant('{cmd}'), '/C ' + W7Command, '', SW_HIDE, ewWaitUntilTerminated, ResultCode);
Log(SysErrorMessage(ResultCode));
end;
end;
end;发布于 2015-08-13 06:43:36
首先,ResultCode包含一个系统错误,只有当Exec返回False (指示启动进程失败)时才会出现系统错误。如果Exec成功启动进程,则ResultCode包含流程的退出代码。你的案子可能是什么。不能将退出代码传递给SysErrorMessage。
引用文档
如果返回True并等待为ewWaitUntilTerminated,则ResultCode返回进程的退出代码。如果返回False,则ResultCode指定发生的错误。使用SysErrorMessage(ResultCode)获取错误描述。
实际问题是您的netsh语法。
在firewall之后缺少一个advfirewall关键字。
netsh advfirewall firewall add rule ...你有麻烦先从命令行测试命令吗?
边注:
Exec的返回值netsh.exe通过cmd.exe启动https://stackoverflow.com/questions/31970310
复制相似问题