未初始化Coinitialize错误消息 [英] Coinitialize has not been called error message

查看:187
本文介绍了未初始化Coinitialize错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 程序ChangeFW; 

{$ APPTYPE控制台}

{$ R * .res}

使用
System.SysUtils,
ComObj ;

var
ExecName:字符串;

过程AddExceptionToFirewall(常量标题,可执行文件:字符串);
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;
个人资料:整数;
NewRule:OleVariant;
开始
配置文件:= NET_FW_PROFILE2_PRIVATE或NET_FW_PROFILE2_PUBLIC;
fwPolicy2:= CreateOleObject(’HNetCfg.FwPolicy2’);
RulesObject:= fwPolicy2.Rules;
NewRule:= CreateOleObject(’HNetCfg.FWRule’);
NewRule.Name:=标题;
NewRule.Description:=标题;
NewRule.Applicationname:=可执行的;
NewRule.Protocol:= NET_FW_IP_PROTOCOL_TCP;
NewRule.Enabled:= TRUE;
NewRule.Profiles:=个人资料;
NewRule.Action:= NET_FW_ACTION_ALLOW;
RulesObject.Add(NewRule);
结尾;

开始
尝试
{TODO -oUser -cConsole Main:在此处插入代码}
ExecName:= GetCurrentDir + \ + Client.exe;
AddExceptionToFirewall( SIP广告资源,ExecName);
,但E上的
除外:执行
Writeln(E.ClassName,’:``,E.Message);
结尾;
结尾。

执行应用程序时,出现以下错误消息:
EOIeSysError:尚未调用Coinitialize,ProgID: HNetCfg.FwPolicy2
知道我在做什么错吗?您能指出我正确的方向吗?

解决方案

如果要使用COM-对象,则必须使用相应的CoUninitialize调用CoInitialize。 / p>

在通常的应用程序中,此操作已经完成。

就您的程序而言,它是一个控制台程序,您必须自己调用它。

  ..... 
CoInitialize(nil);
尝试
尝试
{TODO -oUser -cConsole Main:在此处插入代码}
ExecName:= GetCurrentDir + \ + Client.exe;
AddExceptionToFirewall( SIP广告资源,ExecName);
,但E上的
除外:执行
Writeln(E.ClassName,’:``,E.Message);
结尾;
最终
CoUninitialize;
结尾;
.....


I am in the process of coding a console application that will create a firewall exception for my main app called Client.exe which uploads a few documents to our servers via FTP. I borrowed RRUZ code from Delphi 7 Windows Vista/7 Firewall Exception Network Locations my code looks like this:

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.

When I execute the application I get the following error message: EOIeSysError: Coinitialize has not been called, ProgID: "HNetCfg.FwPolicy2" Any idea what I am doing wrong? Could you please point me in the right direction? Thank you so very much.

解决方案

If you want to use COM - objects you will have to call CoInitialize with corresponding CoUninitialize.

In a usual application this will be already done.
As far as your program is a console program you will have to call it on your own.

.....
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错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆