COMAte to get network card info

Just starting out? Need help? Post your questions and find answers here.
User avatar
OldSkoolGamer
Enthusiast
Enthusiast
Posts: 150
Joined: Mon Dec 15, 2008 11:15 pm
Location: Nashville, TN
Contact:

COMAte to get network card info

Post by OldSkoolGamer »

Got this so far, it will return the description and the MAC, but not the IP Address, any thoughts or suggestions? Pretty sure it's due to the IP's being in an array so it might have to be a variant type instead of string, but not sure how to query like that. The included COMAte demo_GetIPAddresses doesn't work either get a polink error.... :|

Code: Select all

XIncludeFile "Includes\COMatePLus.pbi"
#COMATE_NOINCLUDEATL = 1
#COMATE_NOERRORREPORTING = 1

Procedure.s GetNetworkInfo()
;wmic nicconfig where ipenabled=true get description,ipaddress,macaddress
  strComputer.s = ".";local computer
  Define.COMateObject objWMIService, objItem
  colNICinfo.COMateEnumObject
  objWMIService = COMate_GetObject("winmgmts:{impersonationLevel=impersonate}!\\" + strComputer + "\root\cimv2")
  If objWMIService
    colNICinfo = objWMIService\CreateEnumeration("ExecQuery('Select * from Win32_NetworkAdapterConfiguration where IPEnabled=True')")
    If colNICinfo
      objItem = colNICinfo\GetNextObject()
      While objItem
        mynicdescription.s=objItem\GetStringProperty("Description")
         myipaddress.s=objItem\GetStringProperty("IPAddress")
          mymacaddress.s=objItem\GetStringProperty("MACAddress")
           objItem = colNICinfo\GetNextObject()
nicinfo.s+mynicdescription+#CRLF$+"IP: "+myipaddress+#CRLF$+"MAC: "+mymacaddress+#CRLF$
      Wend
      colNICinfo\Release()
    EndIf
    objWMIService\Release()
  Else
    MessageRequester("Error", "Error retrieving network information."+#CRLF$+"Make sure you are running as an Administrator.",#MB_ICONERROR)
  EndIf
  ProcedureReturn nicinfo
EndProcedure

Debug GetNetworkInfo()
User avatar
chi
Addict
Addict
Posts: 1087
Joined: Sat May 05, 2007 5:31 pm
Location: Austria

Re: COMAte to get network card info

Post by chi »

To run Demo_GetIPAddresses.pb on PB x64 you have to edit VariantHelper_Include.pb and replace Import "oleaut32.lib" ... EndImport with:

Code: Select all

CompilerIf #PB_Compiler_Processor = #PB_Processor_x64
  Import "oleaut32.lib" 
    SafeArrayAllocDescriptorEx(a.l,b.l,c.l) As "SafeArrayAllocDescriptorEx" 
    SafeArrayGetVartype(a.l,b.l) As "SafeArrayGetVartype" 
  EndImport
CompilerElse
  Import "oleaut32.lib" 
    SafeArrayAllocDescriptorEx(a.l,b.l,c.l) As "_SafeArrayAllocDescriptorEx@12" 
    SafeArrayGetVartype(a.l,b.l) As "_SafeArrayGetVartype@8" 
  EndImport  
CompilerEndIf
On PB x86 it works fine, but on PB x64 it now crashes at VariantClear_(*var). Uncomment this call and it also works. Don't know what's going on here, sorry

Btw. if you want to run this Demo on the C backend, you also have to edit COMatePLUS.pbi as described here.
Et cetera is my worst enemy
User avatar
OldSkoolGamer
Enthusiast
Enthusiast
Posts: 150
Joined: Mon Dec 15, 2008 11:15 pm
Location: Nashville, TN
Contact:

Re: COMAte to get network card info

Post by OldSkoolGamer »

OK,

Did that and it works now, thanks for the tip, I think I can manage from here :wink:

EDIT:

Here's what I am using for any that want to know:

Code: Select all

Define.COMateObject objWMIService, IPConfig
IPConfigSet.COMateEnumObject
Define *var.VARIANT, *varIP.VARIANT
*sa.SafeArray
Dim IPArray.s(9)
x=0
strComputer.s = "."
objWMIService = COMate_GetObject("winmgmts:\\" + strComputer + "\root\cimv2", "")
If objWMIService
  IPConfigSet = objWMIService\CreateEnumeration("ExecQuery('Select * from Win32_NetworkAdapterConfiguration where IPEnabled=True')")
  If IPConfigSet
    IPConfig = IPConfigSet\GetNextObject()
    While IPConfig
     If IPConfig
      mynicdescription.s=IPConfig\GetStringProperty("Description")
       *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
          IPArray(X)=PeekS(*varIP\bstrVal, -1, #PB_Unicode)
           x=x+1
            VariantClear_(*varIP)
       Next
          saFreeSafeArray(*sa)
      EndIf
         mymacaddress.s=IPConfig\GetStringProperty("MACAddress")
          FreeMemory(*var)
           IPConfig\Release()
            x=0
     EndIf
nicinfo.s+mynicdescription+#CRLF$
For i=0 To ArraySize(IPArray())
 If IPArray(i)<>""
  If FindString(IPArray(i),":",1)
   nicinfo.s+"IPv6: "+IPArray(i)+#CRLF$
  Else
   nicinfo.s+"IPv4: "+IPArray(i)+#CRLF$
  EndIf
 EndIf
Next i
nicinfo.s+"MAC: "+mymacaddress+#CRLF$+#CRLF$
      IPConfig = IPConfigSet\GetNextObject()
    Wend
  EndIf
FreeArray(IPArray())
Else
  MessageRequester("Error", "Couldn't create the WMI scripting object!",#MB_ICONERROR)
EndIf 
Last edited by OldSkoolGamer on Wed Dec 22, 2021 12:59 pm, edited 1 time in total.
User avatar
mk-soft
Always Here
Always Here
Posts: 6251
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: COMAte to get network card info

Post by mk-soft »

The VariantHelper_Include is deprecated in the COMmate package.
Here you will find the current ...
Link: https://www.purebasic.fr/english/viewtopic.php?t=40150
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
chi
Addict
Addict
Posts: 1087
Joined: Sat May 05, 2007 5:31 pm
Location: Austria

Re: COMAte to get network card info

Post by chi »

OldSkoolGamer wrote: Thu Dec 16, 2021 3:13 pm Here's what I am using for any that want to know:
Seems to work, thanks
mk-soft wrote: Thu Dec 16, 2021 7:11 pm The VariantHelper_Include is deprecated in the COMmate package.
Here you will find the current ...
Link: https://www.purebasic.fr/english/viewtopic.php?t=40150
Thanks, mk-soft! I guess that explains it...

Edit: Demo_IPAddresses.pb still crashes on PB x64 with VariantClear_(*var) enabled. Any ideas why?
Et cetera is my worst enemy
Post Reply