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 »

If you ask COMate to retrieve a string property when in fact it encounters an integer property then it simply converts it to a string etc.
Last edited by srod on Fri Sep 26, 2008 10:35 pm, edited 1 time in total.
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 »

Ahhh...OK, I did not realize that. Thanks srod.
jpd
Enthusiast
Enthusiast
Posts: 167
Joined: Fri May 21, 2004 3:31 pm

Post by jpd »

Hi Srod,
I have spend some time at this methode, and found Out that
for set the value with the methode enablestatic is needed to generate a "new Instance of the class" using spawninstance_()

I have give a look on the example and try some possibility with comate
but this was not really succesfully.

here a vbs script that demostrate this other way:

Code: Select all


strComputer = "." 
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") 
' Obtain an instance of the the class 
' using a key property value.
Set objShare = objWMIService.Get("Win32_NetworkAdapterConfiguration.Index='4'")

' Obtain an InParameters object specific
' to the method.
Set objInParam = objShare.Methods_("EnableStatic"). _ 
 inParameters.SpawnInstance_()


' Add the input parameters.
objInParam.Properties_.Item("IPAddress") =  "192.168.110.1"
objInParam.Properties_.Item("SubnetMask") =  "255.255.255.0"

' Execute the method and obtain the return status.
' The OutParameters object in objOutParams
' is created by the provider.
Set objOutParams = objWMIService.ExecMethod("Win32_NetworkAdapterConfiguration.Index='4'", "EnableStatic", objInParam)

' List OutParams
Wscript.Echo "Out Parameters: "
Wscript.echo "ReturnValue: " & objOutParams.ReturnValue

Hope that you can point me on the right direction.

Thanks
jpd
PB 5.10 Windows 7 x64 SP1
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Sorry, I have no experience with such stuff and lack the time right 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 »

jpd;

maybe something like this using the Invoke of Comate ? I haven't tried it yet but maybe, I just threw it together real quick at 4:30 this morning when I read your post. I don't think that you need the array thing for this method actually:

Code: Select all

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

Define.COMateObject objWMIService, Adapter
colAdapters.COMateEnumObject

strComputer.s = "."
;Interface_Index.s = "the route table Interface Index" ; works for Win Vista only 
Index.s = "the LUID index from the registry" ; works with winXP and Win Vista

IP_Address_to_Set_and_subnet_mask.s = "Your Static IP address here , sub_net_mask " 

objWMIService = COMate_GetObject("winmgmts:\\" + strComputer + "\root\cimv2", "") 
If objWMIService 
  ;colAdapters = objWMIService\CreateEnumeration("ExecQuery('Select * from Win32_NetworkAdapterConfiguration Where InterfaceIndex= $0027" + Interface_Index +"$0027')") ; win Vista only
  colAdapters = objWMIService\CreateEnumeration("ExecQuery('Select * from Win32_NetworkAdapterConfiguration Where Index= $0027" + Index.s +"$0027')") ; winXP and Win Vista
  If colAdapters 
    Adapter= colAdapters\GetNextObject() 
    While Adapter
      Adapter\Invoke("EnableStatic, $0027" + IP_Address_to_Set_and_subnet_mask + " $0027)
      
      Adapter\Release() 
      Adapter = colAdapters\GetNextObject()
    Wend
    colAdapters\Release() 
  EndIf
  objWMIService\Release()  
EndIf
There are actually two different index's that do the same thing. The interface index and the LUID index from the registry. They are different from each other. The inteface index is from the IPv4 routing table and might be 8 while the LUID index is from the registry and might be for example 4. Both ID the particular adapter.

The wmi script from the MSDN looks like this:

Code: Select all

strComputer = "."
Set objWMIService = GetObject( _
    "winmgmts:\\" & strComputer & "\root\cimv2")
Set colNetAdapters = objWMIService.ExecQuery _
    ("Select * from Win32_NetworkAdapterConfiguration " _
        & "where IPEnabled=TRUE")
strIPAddress = Array("192.168.1.141")
strSubnetMask = Array("255.255.255.0")
strGateway = Array("192.168.1.100")
strGatewayMetric = Array(1)
 
For Each objNetAdapter in colNetAdapters
    errEnable = objNetAdapter.EnableStatic( _
        strIPAddress, strSubnetMask)
    errGateways = objNetAdapter.SetGateways(_
        strGateway, strGatewaymetric)
Next
if you set a static IP you also need to set the gateway and the gateway metric. Windows usually takes care of the the gateway metric for you if you set the static thru the GUI, but I think if you set it thru WMI you also need to set the metric.
jpd
Enthusiast
Enthusiast
Posts: 167
Joined: Fri May 21, 2004 3:31 pm

Post by jpd »

Srod wrote: Sorry, I have no experience with such stuff and lack the time right now.
Ok, Thanks.


SFSxOI wrote: maybe something like this using the Invoke of Comate ?
Yes I use it on all possible combination, as simple string with arrays,
savearray ...

but in all cases this error appair:


-2147024809
One or more arguments are invalid. Possibly a numerical overflow or too many nested objects, -if so, try splitting your method call into two or more subcalls.


on this error I read alway try splitting and with the wmi code creator tool
on execute methode, was generated the code posted yesterday...

the problem is that I cannot see a possibility with COMate to
try the following line:

Code: Select all

Set objInParam = objShare.Methods_("EnableStatic"). _ 
 inParameters.SpawnInstance_() 

I'm continue to search and understand better this methode.

Thanks
jpd
PB 5.10 Windows 7 x64 SP1
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Code: Select all

objInParam.COMateObject = objShare\GetObjectProperty("Methods_('EnableStatic')\inParameters\SpawnInstance_()")
Will that do it?

As for your safe-arrays you must remember to embed them within variants before passing them etc.
I may look like a mule, but I'm not a complete ass.
KIKI
Enthusiast
Enthusiast
Posts: 145
Joined: Thu Dec 28, 2006 11:49 am
Location: FRANCE

Problem with variant array

Post by KIKI »

here is my code which works with pure displayhelper

Code: Select all

;EnableExplicit

 ;'Les objets de base pour travailler avec OOo
Declare MakePropertyValue(cName.s, uValue.b) 
Declare MakePropertyValue1(cName.s, uValue.s)
Declare.s ConvertToUrl(strFile.s) 




IncludePath ".\"
XIncludeFile "COMate.pbi"
XIncludeFile "VariantHelper_Include.pb" 

Define  mafeuille,lesfeuille
Define.COMateObject oSM,oDesk,oDoc 

Define.safearray *openpar 
Define.variant openarray 


*openpar = saCreateSafeArray(#VT_DISPATCH, 0, 3) 
SA_DISPATCH(*openpar, 0) = MakePropertyValue("ReadOnly", #True) 
SA_DISPATCH(*openpar, 1) = MakePropertyValue1("Password", "ISABELLE") 
SA_DISPATCH(*openpar, 2) = MakePropertyValue("Hidden", #False) 
V_ARRAY_DISP(openarray) = *openpar 
    ;'Instancie OOo : ces deux lignes sont obligatoires avec VB dans tout code !
oSM = COMate_CreateObject("com.sun.star.ServiceManager")
FILE.S= ConvertToUrl("travail.ots")
oDesk = oSM\GetObjectProperty("createInstance('com.sun.star.frame.Destop')")

;  oDoc = oDesk\GetObjectProperty("loadComponentFromURL('"+file+"', '_blank', 0," + Str(openarray) + " as variant)" 
 oDoc = oDesk\GetObjectProperty("loadComponentFromURL('"+file+"' , '_blank', 0, " + Str(openarray) + " as variant)")      
  
     If oDoc 
        oDoc\Release() 
      EndIf 
      oDesk\Release()    
 
  oSM\Release() 





End    


Procedure MakePropertyValue(cName.s, uValue.b) 
Define.COMateObject oServiceManager,oStruct
 oServiceManager = COMate_CreateObject("com.sun.star.ServiceManager")
If oServiceManager 
 oStruct = oServiceManager\GetObjectProperty("Bridge_GetStruct('com.sun.star.beans.PropertyValue')")
 oStruct\SetProperty("Name = " + cName)
 oStruct\SetProperty("Value = " + Str(uValue))
ProcedureReturn oStruct
EndIf 
EndProcedure 


Procedure MakePropertyValue1(cName.s, uValue.s) 
Define.COMateObject oServiceManager,oStruct
 oServiceManager = COMate_CreateObject("com.sun.star.ServiceManager")
If oServiceManager 
 oStruct = oServiceManager\GetObjectProperty("Bridge_GetStruct('com.sun.star.beans.PropertyValue')")
 oStruct\SetProperty("Name = " + cName)
 oStruct\SetProperty(Chr(34)+"Value = " + uValue )
ProcedureReturn oStruct
EndIf 
EndProcedure 

Procedure.s ConvertToUrl(strFile.s) 
    strFile = ReplaceString(strFile, "\", "/")
    strFile = ReplaceString(strFile, ":", "|")
    strFile= ReplaceString(strFile, " ", "%20")
    strFile = "file:///" + strfile
    ProcedureReturn strFile
EndProcedure
This instruction cause an error
oDoc = oDesk\GetObjectProperty("loadComponentFromURL('"+file+"' , '_blank', 0, " + Str(openarray) + " as variant)")

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

Post by srod »

What error are you getting?

**EDIT : wait I see what the problem is - or at least one problem. Checking... yes it is the safe array of iDispatch objects... COMate is not set up to return such objects from the GetObjectProperty() method (this returns a COMate object which itself contains an iDispatch object!)

Thinking...

***EDIT : okay the following untested workaround allows the MakePropertyValue() function to return an iDispatch pointer directly. It works by retrieving the Bridge_GetStruct('com.sun.star.beans.PropertyValue') property object as a variant from which the iDispatch object can easily be retrieved without any hacks! :) We then temporarily wrap this object within a COMate automation object so that you can set the two properties. Note that this COMate object is released so that the reference counting is kept in order. Note also that the iDisp object which we return has had it's reference count increased and so you will eventually need to destroy the safe array in order to set this right.

You will need to make similar adjustments to the MakePropertyValue1() function.

Code: Select all

Procedure MakePropertyValue(cName.s, uValue.b) 
Define.COMateObject oServiceManager,oStruct 
Define *ret.VARIANT, iDisp.IDISPATCH
oServiceManager = COMate_CreateObject("com.sun.star.ServiceManager") 
If oServiceManager 
  *ret = oServiceManager\GetVariantProperty("Bridge_GetStruct('com.sun.star.beans.PropertyValue')")
  If *ret
    If *ret\vt <> #VT_DISPATCH
      VariantChangeType_(*ret, *ret, 0, #VT_DISPATCH)
    EndIf
    If *ret\vt = #VT_DISPATCH
      iDisp = *ret\pdispVal
      oStruct = COMate_WrapCOMObject(iDisp)
      If oStruct
        oStruct\SetProperty("Name = " + cName) 
        oStruct\SetProperty("Value = " + Str(uValue)) 
        oStruct\Release() ;Required to keep the reference count in check.
      EndIf
    Else
      VariantClear_(*ret)
    EndIf
    FreeMemory(*ret) ;We do not use VariantClear_() as we do not wish to release the iDisp object which we are returning from this function.
  EndIf
EndIf 
ProcedureReturn iDisp
EndProcedure 
I may look like a mule, but I'm not a complete ass.
Tipperton
Addict
Addict
Posts: 1286
Joined: Thu Jun 19, 2003 7:55 pm

Post by Tipperton »

This thread is LOOONG! ;)

Is ActiveX supported/working?

I have project that being able to use name and address cleanup libraries in would be very helpful. Unfortunately these libraries are available as ActiveX libraries.

Thanks!

/edit=never mind, web site says it does, so I'll take it for a spin and see what "develops"... :mrgreen:
jpd
Enthusiast
Enthusiast
Posts: 167
Joined: Fri May 21, 2004 3:31 pm

Post by jpd »

Hi Srod,
srod wrote:

Code: Select all

objInParam.COMateObject = objShare\GetObjectProperty("Methods_('EnableStatic')\inParameters\SpawnInstance_()")
this line help the returncode is =0 and the error descripion is ok!

thanks for that! :-)

next problem directly at next line :oops:

how is this to understand?

Code: Select all

objInParam.Properties_.Item("IPAddress") =  "192.168.110.1" 
Best
jpd
PB 5.10 Windows 7 x64 SP1
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

objInParam\SetProperty("Properties_\Item('IPAddress') = '192.168.110.1'")
I may look like a mule, but I'm not a complete ass.
jpd
Enthusiast
Enthusiast
Posts: 167
Joined: Fri May 21, 2004 3:31 pm

Post by jpd »

if try the suggested line this error appair:

-2147352571
Type mismatch in the method parameters.


thanks
jpd
PB 5.10 Windows 7 x64 SP1
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Well I don't know about that. Have you a link to the VBS script?
I may look like a mule, but I'm not a complete ass.
jpd
Enthusiast
Enthusiast
Posts: 167
Joined: Fri May 21, 2004 3:31 pm

Post by jpd »

some post before, on this site!

http://www.purebasic.fr/english/viewtop ... 066#262066

jpd
PB 5.10 Windows 7 x64 SP1
Post Reply