COMate - control COM objects via automation - OBSOLETE!

Developed or developing a new product in PureBasic? Tell the world about it.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

SFSxOI wrote:yep, you guessed it :)
Only because I made the same mistake! :wink:
I may look like a mule, but I'm not a complete ass.
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Post by SFSxOI »

What is error code :

Unknown error. (Code : Hex FFFFFFFF80020003). Please report this error code to the author at 'enquiries@nxsoftware.com'

?

generated from this code:

Code: Select all


#NET_FW_IP_PROTOCOL_TCP = 6
#NET_FW_ACTION_ALLOW = 1

Procedure AdvFireWall_LanRuleAdd()

Define fwPolicy2.COMateObject 
Define RulesObject.COMateObject 
Define NewRule.COMateObject 

Name$ = "Per_InterfaceType_Rule"
Description$ = "Allow incoming network traffic over port 2400 coming from LAN interfcace type"
LocalPorts$ = Str(2300)
Interfacetypes$ = "All"
Grouping$ = "@firewallapi.dll,-23255"

fwPolicy2 = COMate_CreateObject("HNetCfg.FwPolicy2")

RulesObject = fwPolicy2\GetObjectProperty("Rules()") ; INetFwRules object

CurrentProfiles = fwPolicy2\GetIntegerProperty("CurrentProfilesTypes()")

  NewRule = COMate_CreateObject("HNetCfg.FWRule") ; INetFwRule object
  NewRule\SetProperty("Name(" + Name$ + ")")
  NewRule\SetProperty("Description(" + Description$ + ")") 
  NewRule\SetProperty("Protocol(" + Str(#NET_FW_IP_PROTOCOL_TCP) + ")")
  NewRule\SetProperty("LocalPorts(" + LocalPorts$ + ")")
  NewRule\SetProperty("Interfacetypes(" + Interfacetypes$ + ")")
  NewRule\SetProperty("Enabled(" + Str(#True) + ")")
  NewRule\SetProperty("Grouping(" + Grouping$ + ")")
  NewRule\SetProperty("Profiles(" + Str(CurrentProfiles) + ")")
  NewRule\SetProperty("Action(" + Str(#NET_FW_ACTION_ALLOW) + ")")
    
RulesObject\SetProperty("Add(" + Str(NewRule) + ")")
Debug COMate_GetLastErrorDescription()

NewRule\Release()
RulesObject\Release()
fwPolicy2\Release()

EndProcedure

generated when this line is used:

RulesObject\SetProperty("Add(" + Str(NewRule) + ")")
(everything else above that reports as OK)
The original function is:

Code: Select all

Add(
  [in]  INetFwRule *rule
);
located at: http://msdn.microsoft.com/en-us/library ... S.85).aspx

I also tried SetPropertyRef and Invoke.

Invoke goves "Type mismatch in the method parameters."

SetPropertyRef give the unknown error

I also tried various type modifiers also.

[/code]
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

That error code is #DISP_E_MEMBERNOTFOUND and should be recognised by COMate! That is strange. Have you altered the COMate source by any chance because this is the second time now that you are reporting errors which differ from my version - errors which do not make any sense!

However, the line you refer to looks very wrong. Did you try the " as COMateObject" modifier?

Code: Select all

RulesObject\Invoke("Add(" + Str(NewRule) + " as COMateobject)") 
Note that it really does look to be 'Invoke' in this case.
I may look like a mule, but I'm not a complete ass.
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Post by SFSxOI »

Yep, I tried the Invoke;

Code: Select all

RulesObject\Invoke("Add(" + Str(NewRule) + " as COMateobject)") 
That was actually the very first thing I tried because it did indeed look like an invoke to be used here, the Invoke gives a different error of "Type mismatch in the method parameters." but there isn't a mismatch . I also tried the SetProperty, SetPropertyRef, and variations on GetIntergerProperty and stuff like that (even though this doesn't return anything). I also went thru most of the type modifiers as well.

hmmmm...nope, haven't changed the COMate source in anyway. In fact everytime I start something new I use a fresh unzipped COMate source and the latest I have is right from your download at the site.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

SFSxOI, this code should run fine... except that you have made some baaaaaaaaaaaad ass mistakes! :)

All of your \SetProperty() calls are invalid.

Try the following which I haven't tested and you will need to check for typos etc.

Code: Select all

#NET_FW_IP_PROTOCOL_TCP = 6 
#NET_FW_ACTION_ALLOW = 1 

Procedure AdvFireWall_LanRuleAdd() 

Define fwPolicy2.COMateObject 
Define RulesObject.COMateObject 
Define NewRule.COMateObject 

Name$ = "Per_InterfaceType_Rule" 
Description$ = "Allow incoming network traffic over port 2400 coming from LAN interfcace type" 
LocalPorts$ = Str(2300) 
Interfacetypes$ = "All" 
Grouping$ = "@firewallapi.dll,-23255" 

fwPolicy2 = COMate_CreateObject("HNetCfg.FwPolicy2") 

RulesObject = fwPolicy2\GetObjectProperty("Rules()") ; INetFwRules object 

CurrentProfiles = fwPolicy2\GetIntegerProperty("CurrentProfilesTypes()") 

  NewRule = COMate_CreateObject("HNetCfg.FWRule") ; INetFwRule object 
  NewRule\SetProperty("Name = '" + Name$ + "'") 
  NewRule\SetProperty("Description = '" + Description$ + "'") 
  NewRule\SetProperty("Protocol = " + Str(#NET_FW_IP_PROTOCOL_TCP)) 
  NewRule\SetProperty("LocalPorts = '" + LocalPorts$ + "'") 
  NewRule\SetProperty("Interfacetypes = '" + Interfacetypes$ + "'") 
  NewRule\SetProperty("Enabled = #True") 
  NewRule\SetProperty("Grouping = '" + Grouping$ + "'") 
  NewRule\SetProperty("Profiles = " + Str(CurrentProfiles)) 
  NewRule\SetProperty("Action = " + Str(#NET_FW_ACTION_ALLOW)) 
    
RulesObject\Invoke("Add(" + Str(NewRule) + " as comateobject)") 
Debug COMate_GetLastErrorDescription() 

NewRule\Release() 
RulesObject\Release() 
fwPolicy2\Release() 

EndProcedure 
I may look like a mule, but I'm not a complete ass.
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Post by SFSxOI »

With your code above I now get a "An exception occurred during the execution of this method/property." error at the last line at:

Code: Select all

RulesObject\Invoke("Add(" + Str(NewRule) + " as comateobject)")
If i comment out that line and do a "Debug COMate_GetLastErrorDescription()" it returns OK.

I don't understand it, this should be an Invoke and working.

I have another question for you also. I happen to look at this line again:

Code: Select all

CurrentProfiles = fwPolicy2\GetIntegerProperty("CurrentProfilesTypes()")
After reading the MSDN again, I don't think thats correct. The MSDN says that:

"profileTypesBitmask [out]
Pointer to a buffer that receives the bitmask of the currently active profile from NET_FW_PROFILE_TYPE2."

and if it suceeds it "If the method succeeds, the return value is S_OK." So I happen to look at the value of CurrentProfiles and it is indeed 0 which is #S_OK. So what its returning in CurrentProfiles is really just telling me that it suceeded. So the question is this; How do you return a pointer to a buffer from this using COMate? Like this:

Code: Select all

CurrentProfiles = fwPolicy2\GetIntegerProperty("CurrentProfilesTypes(@profileTypesBitmask)")

??????

srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Try

Code: Select all

CurrentProfiles = fwPolicy2\Invoke("CurrentProfilesTypes(" + str(@profileTypesBitmask) + " BYREF)") 
As for your exception... no idea.


**EDIT : correction; you will probably need to use :

Code: Select all

fwPolicy2\Invoke("CurrentProfilesTypes(" + str(@profileTypesBitmask) + " BYREF)")
and

Code: Select all

NewRule\SetProperty("Profiles = " + Str(profileTypesBitmask)) 

in place of your original line.
I may look like a mule, but I'm not a complete ass.
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Post by SFSxOI »

Finally!

This works!

Code: Select all

RulesObject\GetIntegerProperty("Add(" + Str(NewRule) + " as COMateobject)")
Heres the code...and thank you very much for your help :)

Note: Vista Only

Code: Select all

Procedure AdvFireWall_LanRuleAdd() 

Define fwPolicy2.COMateObject 
Define RulesObject.COMateObject 
Define NewRule.COMateObject

#NET_FW_IP_PROTOCOL_TCP = 6
#NET_FW_ACTION_ALLOW = 1 

Name$ = "Per_InterfaceType_Rule" 
Description$ = "Allow incoming network traffic over port 2400 coming from LAN interface type" 
LocalPorts$ = Str(2400) 
Interfacetypes$ = "LAN" 
Grouping$ = "@firewallapi.dll,-23255"
IP_PROTOCOL_TCP$ = Str(#NET_FW_IP_PROTOCOL_TCP)
ACTION_ALLOW$ = Str(#NET_FW_ACTION_ALLOW) 

fwPolicy2 = COMate_CreateObject("HNetCfg.FwPolicy2") 

RulesObject = fwPolicy2\GetObjectProperty("Rules()") ; INetFwRules object 
fwPolicy2\Invoke("CurrentProfilesTypes(" + Str(@CurrentProfiles) + ")") 

  NewRule = COMate_CreateObject("HNetCfg.FWRule") ; INetFwRule object
  
  NewRule\SetProperty("Name = '" + Name$ + "'") 
  NewRule\SetProperty("Description = '" + Description$ + "'") 
  NewRule\SetProperty("Protocol = '" + IP_PROTOCOL_TCP$ + "'") 
  NewRule\SetProperty("LocalPorts = '" + LocalPorts$ + "'") 
  NewRule\SetProperty("Interfacetypes = '" + Interfacetypes$ + "'") 
  NewRule\SetProperty("Enabled = #True") 
  NewRule\SetProperty("Grouping = '" + Grouping$ + "'") 
  NewRule\SetProperty("Profiles = " + Str(@CurrentProfiles) + ")")  
  NewRule\SetProperty("Action = '" + ACTION_ALLOW$ + "'")
  
RulesObject\GetIntegerProperty("Add(" + Str(NewRule) + " as COMateobject)")

NewRule\Release() 
RulesObject\Release() 
fwPolicy2\Release() 

EndProcedure
and not even any need to go any further with building on this because the rest with adding apps and stuff to the advanced firewall in Vista looks exactly like this except for changing out a property or two, so this makes a good template for this purpose.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

You sure about the @ in the line

Code: Select all

NewRule\SetProperty("Profiles = " + Str(@CurrentProfiles) + ")")
?

That doesn't make sense from where I'm standing -or slobbing on the sofa! :)
I may look like a mule, but I'm not a complete ass.
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Post by SFSxOI »

DoH! I was stupid, This line doesn't really work (and besides it is not really needed it turns out) > "fwPolicy2\Invoke("CurrentProfilesTypes(" + str(@profileTypesBitmask) + " BYREF)")" < it doesn't return a pointer like thought. It creates the rule but doesn't put it in the correct profile, so everything was correct for the rule except the profile and because it created the rule properly and I didn't notice the profile was wrong it means that the above line wasn't returning a pointer like thought. And you were right > NewRule\SetProperty("Profiles = " + Str(@CurrentProfiles) + ")") was not right either.

but it turns out this does work:

Code: Select all

Procedure AdvFireWall_LanRuleAdd() 

Define fwPolicy2.COMateObject 
Define RulesObject.COMateObject 
Define NewRule.COMateObject

Name$ = "Per_InterfaceType_Rule" 
Description$ = "Allow incoming network traffic over port 2400 coming from LAN interface type" 
LocalPorts$ = Str(2400) 
Interfacetypes$ = "LAN" 
Grouping$ = "@firewallapi.dll,-23255"

fwPolicy2 = COMate_CreateObject("HNetCfg.FwPolicy2") 

RulesObject = fwPolicy2\GetObjectProperty("Rules()") ; INetFwRules object

CurrentProfile = fwPolicy2\GetIntegerProperty("CurrentProfileTypes()")

  NewRule = COMate_CreateObject("HNetCfg.FWRule") ; INetFwRule object 
  
  NewRule\SetProperty("Name = '" + Name$ + "'") 
  NewRule\SetProperty("Description = '" + Description$ + "'") 
  NewRule\SetProperty("Protocol = '" + Str(#NET_FW_IP_PROTOCOL_TCP) + "'") 
  NewRule\SetProperty("LocalPorts = '" + LocalPorts$ + "'") 
  NewRule\SetProperty("Interfacetypes = '" + Interfacetypes$ + "'") 
  NewRule\SetProperty("Enabled = #True") 
  NewRule\SetProperty("Grouping = '" + Grouping$ + "'") 
  NewRule\SetProperty("Profiles = '" + Str(CurrentProfile) + "'")  
  NewRule\SetProperty("Action = '" + Str(#NET_FW_ACTION_ALLOW) + "'") 
  
RulesObject\GetIntegerProperty("Add(" + Str(NewRule) + " as COMateobject)") 

NewRule\Release() 
RulesObject\Release() 
fwPolicy2\Release() 

EndProcedure
So I was reading into the msdn too deeply and kept thinking pointer to a buffer when it wasn't realy needed. Now everything with the rule is correct including the profile. Thank you very much for your help. :)
Last edited by SFSxOI on Sun Apr 05, 2009 8:13 pm, edited 1 time in total.
User avatar
Falko
Enthusiast
Enthusiast
Posts: 271
Joined: Sat Oct 04, 2003 12:57 pm
Location: Germany
Contact:

any Idee with Selection.Insert Shift:=xlToRight

Post by Falko »

How can i use this

Code: Select all

Selection.Insert Shift:=xlToRight
in Comate?
www.falko-pure.de
Win11 Pro 64-Bit, PB_6.11b1
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

I have no idea - what is it?
I may look like a mule, but I'm not a complete ass.
User avatar
Falko
Enthusiast
Enthusiast
Posts: 271
Joined: Sat Oct 04, 2003 12:57 pm
Location: Germany
Contact:

Post by Falko »

Sorry, the follow is to this Function, i write from Disphelper to
COMate.

Code: Select all

Procedure InsertCell(ExcelObject.COMateObject,Range.s,Wert.b)        ;Insert Cells  and shift right or down with Wert 0 or 1
  ExcelObject\Invoke("Range('"+Range+"')\Select")
  Debug "InsertCell()1: " +COMate_GetLastErrorDescription() 
  ;dhCallMethod(*obj, "Range(%T).Select",@Range)
  If Wert=0
    ExcelObject\SetProperty("Selection\Shift := "+Str(#xlToRight))
    ExcelObject\Invoke("Selection\Insert")
   ;dhCallMethod(*obj,"Selection.Insert Shift:= %d",#xlToRight)
    Debug "InsertCell()2: " +COMate_GetLastErrorDescription() 
   ElseIf Wert=1
     ExcelObject\SetProperty("Selection\Shift := "+Str(#xlDown))
     ExcelObject\Invoke("Selection\Insert")
    ;dhCallMethod(*obj,"Selection.Insert Shift:= %d",#xlDown)
    Debug "InsertCell()3: "+COMate_GetLastErrorDescription() 
  EndIf
EndProcedure
The full source, you can see here
http://www.purebasic.fr/german/viewtopi ... 466#239466

regards from germany (Sorry for my little english :roll: )
www.falko-pure.de
Win11 Pro 64-Bit, PB_6.11b1
User avatar
Falko
Enthusiast
Enthusiast
Posts: 271
Joined: Sat Oct 04, 2003 12:57 pm
Location: Germany
Contact:

Post by Falko »

Now its ok. Thank You :idea: ,
but i have found a Solution

Code: Select all

Procedure InsertCell(ExcelObject.COMateObject,Range.s,Wert.b)        ;Insert Cells  and shift right or down with Wert 0 or 1
  ExcelObject\Invoke("Range('"+Range+"')\Select")
  If Wert=0
   ExcelObject\Invoke("Selection\Insert("+Str(#xlToRight)+")")
  ElseIf Wert=1
      ExcelObject\Invoke("Selection\Insert("+Str(#xlDown)+")")
  EndIf
EndProcedure

Procedure DeleteCell(ExcelObject.COMateObject,Range.s,Wert.b)        ;Delete Cells and shift left Or up with Wert 0 or 1
  ExcelObject\Invoke("Range('"+Range+"')\Select")
  If Wert=0
     ExcelObject\Invoke("Selection\Delete("+Str(#xlToLeft)+")")
  ElseIf Wert=1
    ExcelObject\Invoke("Selection\Delete("+Str(#xlUp)+")")
  EndIf
EndProcedure
www.falko-pure.de
Win11 Pro 64-Bit, PB_6.11b1
Bernard
User
User
Posts: 23
Joined: Sun Apr 27, 2003 4:49 pm
Location: France

Post by Bernard »

Does anyone know how to set the font size and others attributes, with comate in the CenterHeader page Excel ?

Thanks
Bernard
Post Reply