Welcome! Log In Create A New Profile

Advanced

Re: OLE

Posted by sivakrith 
Re: OLE
February 19, 2021 01:07PM
Hello,

I want to connect to "WbemScripting.SWbemLocator", connect to server " ('.','root\cimv2') ", query it with " Select * from Win32_LogicalDisk' " and read all the properties of hard disk(s) returned.

I'm unaware of any OLE workings in Windev, would like to have directions or code snippets to connect and read the values is really appreciated.

I have the code in Powerbuilder to do the work, which is here:
*****************************************************************************************

// Public Function of_GetDisks() returns integer

// Obtains information from the Windows Management Instrumentation (WMI) service
// about the logical disks (disk partitions, network drives, CD/DVD drives, etc.)
// and physical disk drives (hard disks) that are currently available to Windows.

// The PB OLEObject object (and COM) is used to obtain the information from WMI,
// then the disk property name/value pairs for each disk are loaded into an instance
// array of "disk properties" structures.

// WMI provides this information as the properties of instances of the Win32_LogicalDisk
// and Win32_DiskDrive classes.

// Note: The Try/Catch logic, the trapping of OLE runtime errors and the display of any
// runtime errors is admittedly a little over the top - You probably don't need or want
// anything quite this obsessive... but this is what I coded to help me determine what
// object methods and properties were needed to accomplish the task. So, that being said,
// it should be easy to follow along and see what I did to obtain and examine the WMI info.

// Author: John Fauss

Integer li_rc, li_disk_number
Long ll_logicaldisk_count, ll_physicaldrive_count, ll_index, ll_drive_count
String ls_msg, ls_status, ls_objecttext, ls_token[], ls_name[], ls_value[], ls_empty_array[]
OLEObject lole_locator, lole_service, lole_logicaldisks, lole_physicaldrives
s_disk_properties lstr_empty_disk_array[]

This.ii_num_disks = 0
This.istr_disk = lstr_empty_disk_array

ls_status = ''

//---------------
// Establish an OLE connection to Windows Management Instrumentation (WMI) Scripting Locator.
//---------------
try
if not IsValid(lole_Locator) then
lole_locator = CREATE OLEObject
li_rc = lole_locator.ConnectToNewObject('WbemScripting.SWbemLocator')
end if
catch(OLERuntimeError lore_001)
ls_msg = 'An error has occurred while connecting to WMI Scripting Locator.~r~n~r~n' + &
'Error number: ' + string(lore_001.Number) + '~r~n' + &
'Description: ' + lore_001.Description + '~r~n' + &
'Source: ' + lore_001.Source + '~r~n' + &
'Message: ' + lore_001.Text
Post Function MessageBox('WMI Extractor',ls_msg,Exclamation!,OK!)
end try

if IsValid(lole_locator) then
ls_status = 'Connected via COM to the WMI Scripting Locator.~r~n'
else
ls_status = 'Unable to connect via COM to the WMI Scripting Locator.~r~n'
end if

//---------------
// Using the Scripting Locator, connect to the COM Server for this computer's WMI namespace.
//---------------
try
if IsValid(lole_locator) then
lole_service = lole_locator.ConnectServer('.','root\cimv2')
end if
catch(OLERuntimeError lore_002)
ls_msg = 'An error has occurred while connecting to the WMI namespace server.~r~n~r~n' + &
'Error number: ' + string(lore_002.Number) + '~r~n' + &
'Description: ' + lore_002.Description + '~r~n' + &
'Source: ' + lore_002.Source + '~r~n' + &
'Message: ' + lore_002.Text
Post Function MessageBox('WMI Extractor',ls_msg,Exclamation!,OK!)
end try

if IsValid(lole_service) then
ls_status += 'Connected to the COM Server for the WMI Namespace.~r~n'
else
ls_status += 'Unable to connect to the COM Server for the WMI Namespace.~r~n'
end if

//---------------
// Obtain information on all logical disks (partitions) on this computer.
//---------------
try
if IsValid(lole_service) then
lole_logicaldisks = lole_service.ExecQuery('Select * from Win32_LogicalDisk')
end if
catch(OLERuntimeError lore_003)
ls_msg = 'An error has occurred while obtaining logical disk info.~r~n~r~n' + &
'Error number: ' + string(lore_003.Number) + '~r~n' + &
'Description: ' + lore_003.Description + '~r~n' + &
'Source: ' + lore_003.Source + '~r~n' + &
'Message: ' + lore_003.Text
Post Function MessageBox('WMI Extractor',ls_msg,Exclamation!,OK!)
end try

if IsValid(lole_logicaldisks) then
// How many objects were returned by the query?
ll_logicaldisk_count = lole_logicaldisks.Count
ls_status += String(ll_logicaldisk_count) + ' logical disk(s) found.~r~n'
else
ll_logicaldisk_count = 0
ls_status += 'No logical disks found.~r~n'
end if

//---------------
// Obtain information on all physical disk drives on this computer.
//---------------
try
if IsValid(lole_Service) then
lole_physicaldrives = lole_service.ExecQuery('Select * from Win32_DiskDrive')
end if
catch(OLERuntimeError lore_004)
ls_msg = 'An error has occurred while obtaining physical disk drive info.~r~n~r~n' + &
'Error number: ' + string(lore_004.Number) + '~r~n' + &
'Description: ' + lore_004.Description + '~r~n' + &
'Source: ' + lore_004.Source + '~r~n' + &
'Message: ' + lore_004.Text
Post Function MessageBox('WMI Extractor',ls_msg,Exclamation!,OK!)
end try

if IsValid(lole_physicaldrives) then
// How many objects were returned by the query?
ll_physicaldrive_count = lole_physicaldrives.Count
ls_status += String(ll_physicaldrive_count) + ' physical drive(s) found.~r~n'
else
ll_physicaldrive_count = 0
ls_status += 'No physical drives found.~r~n'
end if

//---------------
// Obtain & parse a text represention of every logical disk class instance that was returned.
//---------------
if ll_logicaldisk_count > 0 then
for ll_index = 1 to ll_logicaldisk_count
// Collection indexes begins at zero, not one.
ls_objecttext = lole_logicaldisks.ItemIndex(ll_index - 1).GetObjectText_()
if ls_objecttext <> '' then
// Parse the properties of this logical disk.
li_disk_number = This.ii_num_disks + 1
li_rc = This.of_ParseDiskProperties('Logical',ls_objecttext,This.istr_disk[li_disk_number])
This.ii_num_disks = li_disk_number
// For debugging,,,
// Post Function MessageBox('Logical Disk '+String(ll_index - 1),ls_objecttext)
end if
next
end if

//---------------
// Obtain & parsey a text represention of every physical disk drive class instance that was returned.
//---------------
if ll_physicaldrive_count > 0 then
for ll_index = 1 to ll_physicaldrive_count
// Collection indexes begin at zero, not one.
ls_objecttext = lole_physicaldrives.ItemIndex(ll_index - 1).GetObjectText_()
if ls_objecttext <> '' then
li_disk_number = This.ii_num_disks + 1
li_rc = This.of_ParseDiskProperties('Physical',ls_objecttext,This.istr_disk[li_disk_number])
This.ii_num_disks = li_disk_number
// For debugging,,,
// Post Function MessageBox('Physical Disk Drive '+String(ll_index - 1),ls_objecttext)
end if
next
end if

//---------------
// Cleanup on aisle 3! Disconnect OLE objects.
//---------------
try
if IsValid(lole_physicaldrives) then
li_rc = lole_physicaldrives.DisconnectObject()
end if
catch(OLERuntimeError lore_996)
ls_msg = 'An error has occurred while disconnecting the physical disk drives OLE object.~r~n~r~n' + &
'Error number: ' + string(lore_996.Number) + '~r~n' + &
'Description: ' + lore_996.Description + '~r~n' + &
'Source: ' + lore_996.Source + '~r~n' + &
'Message: ' + lore_996.Text
Post Function MessageBox('WMI Extractor',ls_msg,Exclamation!,OK!)
end try

try
if IsValid(lole_logicaldisks) then
li_rc = lole_logicaldisks.DisconnectObject()
end if
catch(OLERuntimeError lore_997)
ls_msg = 'An error has occurred while disconnecting the logical disks OLE object.~r~n~r~n' + &
'Error number: ' + string(lore_997.Number) + '~r~n' + &
'Description: ' + lore_997.Description + '~r~n' + &
'Source: ' + lore_997.Source + '~r~n' + &
'Message: ' + lore_997.Text
Post Function MessageBox('WMI Extractor',ls_msg,Exclamation!,OK!)
end try

try
if IsValid(lole_service) then
li_rc = lole_service.DisconnectObject()
end if
catch(OLERuntimeError lore_998)
ls_msg = 'An error has occurred while disconnecting the WMI namespace OLE object.~r~n~r~n' + &
'Error number: ' + string(lore_998.Number) + '~r~n' + &
'Description: ' + lore_998.Description + '~r~n' + &
'Source: ' + lore_998.Source + '~r~n' + &
'Message: ' + lore_998.Text
Post Function MessageBox('WMI Extractor',ls_msg,Exclamation!,OK!)
end try

try
if IsValid(lole_locator) then
li_rc = lole_locator.DisconnectObject()
DESTROY lole_locator
end if
catch(OLERuntimeError lore_999)
ls_msg = 'An error has occurred while disconnecting the WMI Locator OLE object.~r~n~r~n' + &
'Error number: ' + string(lore_999.Number) + '~r~n' + &
'Description: ' + lore_999.Description + '~r~n' + &
'Source: ' + lore_999.Source + '~r~n' + &
'Message: ' + lore_999.Text
Post Function MessageBox('WMI Extractor',ls_msg,Exclamation!,OK!)
end try

//---------------
// All done.
//---------------

// For debugging,,,
//Post Function MessageBox('WMI Data Extraction Final Results',ls_status)

Return 0
************************************************************************************

Happiness Always
BKR Sivaprakash
pao
Re: OLE
February 19, 2021 04:59PM
Did you try using one Automation object or one OLE control
[doc.windev.com]
[doc.windev.com]

Regards

Paulo Oliveira
Argus
Re: OLE
February 20, 2021 01:35PM
Not a direct answer, but the link below points to a series of functions using WMI...

[depot.pcsoft.fr]
Author:

Subject:


Spam prevention:
Please, enter the code that you see below in the input field. This is for blocking bots that try to post this form automatically. If the code is hard to read, then just try to guess it right. If you enter the wrong code, a new image is created and you get another chance to enter it right.
Message: