【问题标题】:Coinitialize has not been called error messageCoinitialize has not been called 错误信息
【发布时间】: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” 知道我做错了什么吗?你能指出我正确的方向吗?非常感谢。

【问题讨论】:

    标签: delphi delphi-xe2 console-application windows-firewall


    【解决方案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;
    .....
    

    【讨论】:

      猜你喜欢
      • 2018-12-16
      • 2020-03-13
      • 1970-01-01
      • 2019-04-25
      • 2023-01-09
      • 1970-01-01
      • 2011-10-05
      • 1970-01-01
      • 2021-12-25
      相关资源
      最近更新 更多