I’m getting error when i call AddExceptionToFirewall in my Console Application
Exception EOleSysError In Module at CoInitialize has not been called
how to fix it why I’m getting this error even using ComObj ,ActiveX in uses clause?
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;
You need to call OleInitialize (or OleInitializeEx) in your application before you try and Create a COM object.
I do this in my main unit:
This function is in the ActiveX unit (via an External) but is not automatically called by that unit. It could be that other units will call this. It depends on when your function runs.
Here is a Stackoverflow question asking about calling OleInitialize twice.
Also be aware that this is thread specific. If are starting a different thread where this function is running you need to call OleInitialize on that thread.
Here is another good page that goes into a lot of detail: Inside the COM Client
A relevant quote from that page: