Coinitialize has not been called 错误信息

Posted

技术标签:

【中文标题】Coinitialize has not been called 错误信息【英文标题】:Coinitialize has not been called error message 【发布时间】:2013-05-04 08:47:16 【问题描述】:

我正在编写一个控制台应用程序,该应用程序将为我的名为 Client.exe 的主应用程序创建一个防火墙例外,该应用程序通过 FTP 将一些文档上传到我们的服务器。我从Delphi 7 Windows Vista/7 Firewall Exception Network Locations借用了RRUZ代码,我的代码如下:

program ChangeFW;

$APPTYPE CONSOLE

$R *.res

uses
  System.SysUtils,
  ComObj;

var
  ExecName: string;

procedure AddExceptionToFirewall(Const Caption, Executable: String);
const
NET_FW_PROFILE2_DOMAIN  = 1;
NET_FW_PROFILE2_PRIVATE = 2;
NET_FW_PROFILE2_PUBLIC  = 4;

NET_FW_IP_PROTOCOL_TCP = 6;
NET_FW_ACTION_ALLOW    = 1;
var
  fwPolicy2      : OleVariant;
  RulesObject    : OleVariant;
  Profile        : Integer;
  NewRule        : OleVariant;
begin
  Profile             := NET_FW_PROFILE2_PRIVATE OR NET_FW_PROFILE2_PUBLIC;
  fwPolicy2           := CreateOleObject('HNetCfg.FwPolicy2');
  RulesObject         := fwPolicy2.Rules;
  NewRule             := CreateOleObject('HNetCfg.FWRule');
  NewRule.Name        := Caption;
  NewRule.Description := Caption;
  NewRule.Applicationname := Executable;
  NewRule.Protocol := NET_FW_IP_PROTOCOL_TCP;
  NewRule.Enabled := TRUE;
  NewRule.Profiles := Profile;
  NewRule.Action := NET_FW_ACTION_ALLOW;
  RulesObject.Add(NewRule);
end;

begin
  try
     TODO -oUser -cConsole Main : Insert code here 
    ExecName := GetCurrentDir + '\' + 'Client.exe';
    AddExceptionToFirewall('SIP Inventory',ExecName);
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

当我执行应用程序时,我收到以下错误消息: EOIeSysError:尚未调用 Coinitialize,ProgID:“HNetCfg.FwPolicy2” 知道我做错了什么吗?你能指出我正确的方向吗?非常感谢。

【问题讨论】:

【参考方案1】:

如果您想使用 COM - 对象,您必须使用相应的 CoUninitialize 调用 CoInitialize。

在通常的应用程序中,这已经完成了。 只要您的程序是控制台程序,您就必须自己调用它。

.....
CoInitialize(nil);
try
  try
     TODO -oUser -cConsole Main : Insert code here 
    ExecName := GetCurrentDir + '\' + 'Client.exe';
    AddExceptionToFirewall('SIP Inventory',ExecName);
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
finally
  CoUninitialize;
end;
.....

【讨论】:

以上是关于Coinitialize has not been called 错误信息的主要内容,如果未能解决你的问题,请参考以下文章

lumen----------A facade root has not been set.

Hangfire JobStorage.Current property value has not been initialized

React NativeInvariant Violation: Application AwesomeProject has not been registered

System.ServiceProcess.TimeoutException: Time out has expired and the operation has not been complete

微信小程序 uniapp has not been registered yet.报错 解决方案

SecureCRT A public key file has not been specified