How to use Windows Management Instrumentation (WMI) in StarBasic macros?

I was almost giving up trying to figure out how to enable use of WMI in the world of Star Basic macros…

If you want to share a solution, please do not post it in the question. Instead, create a question, worded as someone would want to ask; and then add a solution, which you may also mark as such, to make the question shown as solved.

Or, if I misunderstood your intention, what is the question here?

The big question (to me) was how to get it working :slightly_smiling_face:

Do you mean, that your code doesn’t work, and you ask others to look at it and suggest how to fix it?

Nope, the code works perfectly now.

Then PLEASE do follow what I asked you to do. The way you posted is simply confusing on this Ask site.

I suppose you as a moderator could delete the whole thread if you wanted to. I don’t see any point in first asking a question and then answering only yourself…

I am not a moderator. But if you don’t see a point to make this site a better tool for others to find useful answered questions (when they search, and in the list of potential matches, see it’s simply unanswered, and have no “solution” checkbox), I can’t help.

I finally found a way to make it work. Here is a simple code snippet on how to get information about installed network adapters in a Windows environment.

REM  *****  BASIC  *****
Option VBASupport 1
Option Compatible

Sub Main

End Sub

Sub GetNetworkAdpters

	Dim strObjectName As String
	Dim NetworkAdapters As Variant
	strObjectName = "WbemScripting.SWbemLocator"
	Set objLocator = CreateObject(strObjectName)
	Dim strServer   As String
	Dim strNamespace As String
 	strServer = "."
	strNamespace = "root\cimv2"
	Set objService = objLocator.ConnectServer(strServer, strNamespace)
	Dim strQuery As String
	strQuery = "Select * from Win32_NetworkAdapter where AdapterTypeId < 10 And NetConnectionID IS NOT NULL"
	Set  NetworkAdapters = objService.ExecQuery(strQuery)
	Dim strCols As String : strCols = "" 
	Dim adapter As Variant

	If IsEmpty(NetworkAdapters) Then
		MsgBox "No network adapters found."
		Exit Sub
	End If
    
	Set adaptersDict = CreateObject("Scripting.Dictionary")
	For Each adapter In NetworkAdapters
		adaptersDict.Add i, adapter
		i = i + 1
	Next adapter
    
	For i = 0 To NetworkAdapters.Count - 1
		Set adapter = NetworkAdapters.ItemIndex(i) 
        	strCols = strCols & "Name: " & adapter.Properties_("Name") & Chr(10) 
        	strCols = strCols & "NetConnectionID: " & adapter.Properties_("NetConnectionID") & Chr(10)
        	strCols = strCols & "NetEnabled: " & adapter.Properties_("NetEnabled") & Chr(10)
        	strCols = strCols & "Speed: " & (adapter.Properties_("Speed") / 1000000) & " Mbit/s" & Chr(10)
        	strCols = strCols & "DeviceID: " & adapter.Properties_("DeviceID") & Chr(10)
        	strCols = strCols & "Index: " & adapter.Properties_("Index") & Chr(10)
        	strCols = strCols & "MACAddress: " & adapter.Properties_("MACAddress") & Chr(10)
        	strCols = strCols & "AdapterType: " & adapter.Properties_("AdapterType") & Chr(10)
        	strCols = strCols & "InterfaceIndex: " & adapter.Properties_("InterfaceIndex") & Chr(10)
        	strCols = strCols & "AdapterTypeID: " & adapter.Properties_("AdapterTypeID") & Chr(10)
        	strCols = strCols & Chr(13) & Chr(10)
    	Next i
    
	MsgBox strCols
    
	Set NetworkAdapters = Nothing
	Set objService = Nothing
	Set objLocator = Nothing
	
End Sub

@ mikekaganski
Better now?

1 Like

Here’s a demo
WMI_Test.odt (17.0 KB)