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 »

The agent is a funny beast! I think the hide does exactly that; hiding the agent without interrupting the program! The program completes as normal. There is probably a property to determine if the Agent has been hidden, but I haven't time right now to search for it! :) Have a look at the disp-interfaces behind the agent with some OLE viewer.
I may look like a mule, but I'm not a complete ass.
User avatar
Kiffi
Addict
Addict
Posts: 1484
Joined: Tue Mar 02, 2004 1:20 pm
Location: Amphibios 9

Post by Kiffi »

srod wrote:I am guessing, but I don't think even DispHelper would work with this code of yours?
yes, indeed. PureDispHelper is not able to pass ByRef-Values (yet).
srod wrote:As you can see I have added a 'BYREF' modifier for parameters.
cool, looks good! :D

i'am looking forward to your next release.

Thank you & Greetings ... Kiffi
Hygge
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Just working on updating COMate right now. I will try and speed the command tokeniser/parser up as well as that could slow things down a bit! :)
I may look like a mule, but I'm not a complete ass.
User avatar
Michael Vogel
Addict
Addict
Posts: 2797
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Post by Michael Vogel »

I prefer solutions which do not need libraries, so COMate is perfect for me (because it can be tested on the fly even with my PureBasic-Environment on an USB stick)...

I've downloaded the OLEviewer and scrolled around - found MS Agent Control 1.5 and 2.0 and dozens of entries. It's an try and error game for me now, because there is no entry which has a name like 'AgentViewStatus' :wink:

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

Post by srod »

@SFSxOI : right, can you please try the following code. Bear in mind that I am unable to test it and so I have no idea if this will work!

It needs mk-soft's and ts-soft's "VariantHelper_Include.pb" file which I've temporarily uploaded here because there seemed to be a slight bug with SafeArrays : http://www.purecoder.net/VariantHelper_Include.pb

Code: Select all

XIncludeFile "COMate.pbi"
XIncludeFile "VariantHelper_Include.pb"

Define.COMateObject objWMIService, IPConfig
IPConfigSet.COMateEnumObject
Define *var.VARIANT, *varIP.VARIANT
*sa.SafeArray

strComputer.s = "."

objWMIService = COMate_GetObject("winmgmts:\\" + strComputer + "\root\cimv2", "")
If objWMIService
  IPConfigSet = objWMIService\CreateEnumeration("ExecQuery('Select IPAddress from Win32_NetworkAdapterConfiguration')")
  If IPConfigSet
    IPConfig = IPConfigSet\GetNextObject()
    While IPConfig
      If IPConfig
        *var = IPConfig\GetVariantProperty("IPAddress")
        If *var\vt <> #VT_NULL
          *sa = *var\parray
          For i = saLBound(*sa) To saUBound(*sa)
            *varIP = SA_VARIANT(*sa, i)
            If *varIP\vt <> #VT_BSTR
              VariantChangeType_(*varIP, *varIP, 0, #VT_BSTR)
            EndIf
            Debug PeekS(*varIP\bstrVal, -1, #PB_Unicode)
            VariantClear_(*varIP)
          Next
          saFreeSafeArray(*sa)
        EndIf
        VariantClear_(*var)
        IPConfig\Release()
      EndIf
      IPConfig = IPConfigSet\GetNextObject()
    Wend
  EndIf  
EndIf 
I honestly have no idea if the above will work; although I do have various WMI examples running fine now. I reckon that one possible reason for the above not working correctly (if that proves to be the case) would be because I am perhaps trying to force a variant into the form of a BSTR and these particular variants may not be able to be coerced in this way!

We shall see! :)
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 code you just posted... well, I'm sorry, but it's nowhere near a valid translation to Purebasic with COMate! :) You're attempting to execute COM methods on internal COMate objects! You have also not created an enumeration of IPAddress objects etc. Someone hasn't read the COMate manual! :wink: You need to look at the code of mine which you ran as a test, that is how we must proceed. What your test has confirmed is that we simply need to complete the code.

The 8204 was what I was after! :wink: The 1's represent null returns, presumably from adapters with no ip address. You should modify the query to filter these values out. Should be easy enough.

The 8204 tells us that one adaptor does indeed have an ip address and the enumeration has returned a safearray of variants! All we need to do is figure out how to get our hands on the array and then the variants within the array! :wink: Difficult for me because I can't run the code in order to make tests as I proceed etc; I have no network adapters yielding any results and I do not have enough experience with SafeArrays to figure out the code whilst running blind!

Still, I'll certainly have a look tomorrow. :)
well, yeah its not right, COMate is new to me. I'll learn over time. :) Its just so different I guess...gotta get the logic and methods down. Anyway, back to the code you posted, the 8204. The 1's are from adapters that windows installs by default but aren't used in all cases, the 8204 represents an actual physical NIC in the computer which does have an ipv4 address assigned to it, where as the others are microsoft implemetations of things like the 6to4 adaptor or the loopback adaptor and such that would have a native IP of the computer of 127.0.0.1 if not being used but not a connection IP (like the NIC has).

But, your code, the 8204, i gather from your post that we are dealing with a safearray of variants?

If you look at this class in the MSDN at: http://msdn.microsoft.com/en-us/library ... S.85).aspx it shows that IPAddress is Data type: string array - but is this a safearray?
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Did the code in my last post work?
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;

Yep, your last code works. :) returns the ipv4 address and the ipv6 address here on windows vista ultimate just like the msdn says its supposed to do. (it only returns the ipv4 on XP per the msdn)

so there is an issue SafeArrays then ?
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Yes, there was a slight bug with the SA_VARIANT macro in the variant helper file. I'll send mk-soft a pm when I get a spare moment! :)
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 »

OK, starting to get the hang of a little of it now. Heres a couple of examples using the Win32_NetworkAdapterConfiguration class and the ReleaseDHCPLease and RenewDHCPLease methods with the COmate Invoke.

First the ReleaseDHCPLease method:

Code: Select all

XIncludeFile "COMate.pbi" 

Define.COMateObject objWMIService, objNetCard 
colNetCards.COMateEnumObject 

strComputer.s = "." 

objWMIService = COMate_GetObject("winmgmts:\\" + strComputer + "\root\cimv2", "") 
If objWMIService 
  colNetCards = objWMIService\CreateEnumeration("ExecQuery('Select IPAddress from Win32_NetworkAdapterConfiguration')")  
  If colNetCards 
    objNetCard = colNetCards\GetNextObject() 
    While objNetCard 
      objNetCard\Invoke("ReleaseDHCPLease()") 
      objNetCard\Release() 
      objNetCard = colNetCards\GetNextObject() 
    Wend
    colNetCards\Release() 
  EndIf
  objWMIService\Release()  
EndIf
and now the RenewDHCPLease method.

Code: Select all

XIncludeFile "COMate.pbi" 

Define.COMateObject objWMIService, objNetCard 
colNetCards.COMateEnumObject 

strComputer.s = "." 

objWMIService = COMate_GetObject("winmgmts:\\" + strComputer + "\root\cimv2", "") 
If objWMIService 
  colNetCards = objWMIService\CreateEnumeration("ExecQuery('Select IPAddress from Win32_NetworkAdapterConfiguration')")  
  If colNetCards 
    objNetCard = colNetCards\GetNextObject() 
    While objNetCard 
      Debug objNetCard\Invoke("RenewDHCPLease()") 
      objNetCard\Release() 
      objNetCard = colNetCards\GetNextObject() 
    Wend
    colNetCards\Release() 
  EndIf
  objWMIService\Release()  
EndIf
Last edited by SFSxOI on Thu Sep 04, 2008 5:27 pm, edited 1 time in total.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Aye, WMI is the business! :wink:
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 »

@SFSxOI : you are not releasing all the objects in your above two snippets. You must release the COMateEnumObjects and the original COMate object.
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 »

You mean like this?

Code: Select all

XIncludeFile "COMate.pbi" 

Define.COMateObject objWMIService, objNetCard 
colNetCards.COMateEnumObject 

strComputer.s = "." 

objWMIService = COMate_GetObject("winmgmts:\\" + strComputer + "\root\cimv2", "") 
If objWMIService 
  colNetCards = objWMIService\CreateEnumeration("ExecQuery('Select IPAddress from Win32_NetworkAdapterConfiguration')")  
  If colNetCards 
    objNetCard = colNetCards\GetNextObject() 
    While objNetCard 
      Debug objNetCard\Invoke("RenewDHCPLease()") 
      objNetCard\Release() 
      objNetCard = colNetCards\GetNextObject() 
    Wend
    colNetCards\Release() 
  EndIf
  objWMIService\Release()  
EndIf
Last edited by SFSxOI on Thu Sep 04, 2008 5:32 pm, edited 1 time in total.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Move

Code: Select all

colNetCards\Release() 
up one line to site before the EndIf. :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 »

OK, got ya. thanks. corrected code in previous post.

I was looking at the help, the use of variables wasn't real clear to me.

for example, if I package the below into a procedure like this:

Code: Select all

XIncludeFile "COMate.pbi" 

Procedure Disable_Nic(nic_index.l)

Define.COMateObject objWMIService, Adapter
colAdapters.COMateEnumObject 

strComputer.s = "." 
index_nic$ = Str(nic_index)

objWMIService = COMate_GetObject("winmgmts:\\" + strComputer + "\root\cimv2", "") 
If objWMIService 
  colAdapters = objWMIService\CreateEnumeration("ExecQuery('Select * from Win32_NetworkAdapter Where Index= ' index_nic$)")  
  If colAdapters 
    Adapter= colAdapters\GetNextObject() 
    While Adapter
    Debug Adapter\Invoke("Disable()")  
      Adapter\Release() 
      Adapter = colAdapters\GetNextObject() 
    Wend
    colAdapters\Release() 
  EndIf
  objWMIService\Release()  
EndIf
EndProcedure

Disable_Nic(8)
how do you include a passed value - nic_index.l - in the line

colAdapters = objWMIService\CreateEnumeration("ExecQuery('Select * from Win32_NetworkAdapter Where Index= ' index_nic$)")

?
Last edited by SFSxOI on Thu Sep 04, 2008 5:28 pm, edited 2 times in total.
Post Reply