COMate - control COM objects via automation - OBSOLETE!

Developed or developing a new product in PureBasic? Tell the world about it.
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Post by SFSxOI »

hmmm...well Duh! yes its '"One or more arguments are invalid' - but looking at the original function it would seem the arguments are correct, so it must be something i'm doing with the formatting or something.

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

Post by srod »

Can you post your code? I'd rather some code which will not alter my Win firewall settings though! :)
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 »

LoL :) nope, the below is only supposed to return information for globally open ports and doesn't change anything, just collects info:

Code: Select all

#NET_FW_IP_PROTOCOL_TCP = 6 ; for TCP ports

Protected objFirewall.COMateObject 
Protected objPolicy.COMateObject 
Protected colPorts.COMateObject
Protected objPort.ComateObject 

objFirewall = COMate_CreateObject("HNetCfg.FwMgr")
objPolicy = objFirewall\GetObjectProperty("LocalPolicy()\CurrentProfile()")
colPorts = objPolicy\GetObjectProperty("GloballyOpenPorts()")
count = colPorts\GetIntegerProperty("Count()")
;seems to be working to here OK - count returns 38 ports which is correct per manual count

for I = 0 to count
objPort = colPorts\GetObjectProperty("Item(" + Str(I) + ", " + Str(#NET_FW_IP_PROTOCOL_TCP) + ")")

; the rest goes here

Next

objFirewall\Release()

and this is the original code:

Code: Select all

Set objFirewall = CreateObject("HNetCfg.FwMgr")
Set objPolicy = objFirewall.LocalPolicy.CurrentProfile

Set colPorts = objPolicy.GloballyOpenPorts

For Each objPort in colPorts
    Wscript.Echo "Port name: " & objPort.Name
    Wscript.Echo "Port number: " & objPort.Port
    Wscript.Echo "Port IP version: " & objPort.IPVersion
    Wscript.Echo "Port protocol: " & objPort.Protocol
    Wscript.Echo "Port scope: " & objPort.Scope
    Wscript.Echo "Port remote addresses: " & objPort.RemoteAddresses
    Wscript.Echo "Port enabled: " & objPort.Enabled
    Wscript.Echo "Port built-in: " & objPort.Builtin
Next
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

I get a count of zero open global ports here!!! How do I manually add a port?

However, you do want For I = 0 to Count -1 do you not?
I may look like a mule, but I'm not a complete ass.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Does the GloballyOpenPorts() property not return an entire collection? If so you will need to use COMate's enumeration functions.
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 »

srod wrote:I get a count of zero open global ports here!!! How do I manually add a port?

However, you do want For I = 0 to Count -1 do you not?
To manually add a port you have to do it in the firewall interface its self. I do have a script that will progmatically add it if thats what you want? But were not adding ports here, just enumerating whats already there and getting info.


I don't think I = 0 to Count -1 is correct in this case, it doesn't work that way with the existing vbs script and throws an error. besides the count would start at -1 instead of 0 then.

it returns 38 ports here, which is correct for this setup because there are actually 38 global open ports. So I tend to think this part at least is correct because the manual count says its 38 and it returns the same with the .vbs as well which I already know works properly.
Last edited by SFSxOI on Tue Mar 31, 2009 2:18 pm, edited 2 times in total.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Well if count is 0 then I would have thought that you would not want the loop to run; which is what For I = 0 to count-1 will ensure.

With no ports added then there is no way I can test the code; although I still think that you may need to create an enumeration etc.
I may look like a mule, but I'm not a complete ass.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Okay, have added a port and it is being picked up.... let's have a look at this code then...

I am getting a general exception error with the objPort = colPorts\GetObjectProperty("Item(" + Str(I) + ", " + Str(#NET_FW_IP_PROTOCOL_TCP) + ")") 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 »

Actually you do want the loop to run in this case I think because port 0 always exists even though it doesn't do anything, but for a firewall there is always a port 0. But I just tried the -1 and indeed the loop doesn't run which makes sense, but when the loop does run there is still the problem with objPort. So I know the ports exist and are in the collection already in GloballyOpenPorts() which is the collection, and I know they are there because the count tells me so. You think the COMate enum functions are needed here?
Last edited by SFSxOI on Tue Mar 31, 2009 2:31 pm, edited 1 time in total.
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Post by SFSxOI »

srod wrote:Okay, have added a port and it is being picked up.... let's have a look at this code then...

I am getting a general exception error with the objPort = colPorts\GetObjectProperty("Item(" + Str(I) + ", " + Str(#NET_FW_IP_PROTOCOL_TCP) + ")") line.
Thats odd, I still get the 'One or more arguments are invalid' thing
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

The following works here :

Code: Select all

IncludePath "..\"
XIncludeFile "COMate.pbi"

Global objExcel.COMateObject

#NET_FW_IP_PROTOCOL_TCP = 6 ; for TCP ports 

Define objFirewall.COMateObject 
Define objPolicy.COMateObject 
Define colPorts.COMateEnumObject 
Define objPort.ComateObject 

objFirewall = COMate_CreateObject("HNetCfg.FwMgr") 
If objFirewall
  objPolicy = objFirewall\GetObjectProperty("LocalPolicy()\CurrentProfile()") 
  If objPolicy
    colPorts = objPolicy\CreateEnumeration("GloballyOpenPorts()") 
    If colPorts
      objPort = colPorts\GetNextObject()
      While objPort
        Debug objPort\GetStringProperty("Name")
        objPort\Release()
        objPort = colPorts\GetNextObject()
      Wend
      colPorts\Release()
    EndIf
    objPolicy\Release()
  EndIf
  objFirewall\Release() 
EndIf
**EDIT : sorry, if I had looked at the VB Script which you posted then I would have been absolutely definite from the beginning that you needed to use COMate's enumeration functions etc!
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 »

srod wrote:The following works here :

Code: Select all

IncludePath ".."
XIncludeFile "COMate.pbi"

Global objExcel.COMateObject

#NET_FW_IP_PROTOCOL_TCP = 6 ; for TCP ports 

Define objFirewall.COMateObject 
Define objPolicy.COMateObject 
Define colPorts.COMateEnumObject 
Define objPort.ComateObject 

objFirewall = COMate_CreateObject("HNetCfg.FwMgr") 
If objFirewall
  objPolicy = objFirewall\GetObjectProperty("LocalPolicy()\CurrentProfile()") 
  If objPolicy
    colPorts = objPolicy\CreateEnumeration("GloballyOpenPorts()") 
    If colPorts
      objPort = colPorts\GetNextObject()
      While objPort
        Debug objPort\GetStringProperty("Name")
        objPort\Release()
        objPort = colPorts\GetNextObject()
      Wend
      colPorts\Release()
    EndIf
    objPolicy\Release()
  EndIf
  objFirewall\Release() 
EndIf
hmmmm...strange...doesn't work here, nothing returned in the Debug objPort\GetStringProperty("Name")

You using Vista or XP?
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Post by SFSxOI »

Whoops! never mind, it works now. I screwed up. OKi Dokee then it works.

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

Post by srod »

Let me guess : you didn't use "Define colPorts.COMateEnumObject" ?

:)

Glad it is all working now.
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, you guessed it :)
Post Reply