Page 1 of 1

COMAte to get network card info

Posted: Wed Dec 15, 2021 10:44 pm
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()

Re: COMAte to get network card info

Posted: Thu Dec 16, 2021 1:06 am
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.

Re: COMAte to get network card info

Posted: Thu Dec 16, 2021 3:13 pm
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 

Re: COMAte to get network card info

Posted: Thu Dec 16, 2021 7:11 pm
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

Re: COMAte to get network card info

Posted: Thu Dec 16, 2021 9:55 pm
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?