Jump to content

Problem determining the adapter GUID when using the Network List Manager COM interface


Go to solution Solved by Danyfirex,

Recommended Posts

I'm playing around a bit with the Network List Manager COM interface.
Now I have a simple example where I first create the COM object and then iterate over the network connections.
Determining and outputting the domain type of the individual connections is no problem, but when determining the GUID of the network adapter I get an error:

Quote

"…\example.au3" (19) : ==> The requested action with this object has failed.:
$oNC.GetAdapterId
$oNC^ ERROR

To reproduce my problem, just uncomment the corresponding line from the following example code:

Opt("MustDeclareVars", 1)

Main()

Func Main()
    ; Create Network List Manager object
    Local $oNLM = ObjCreate("{DCB00C01-570F-4A9B-8D69-199FDBA5723B}")

    ; Get the list of network connections
    Local $aoNC = $oNLM.GetNetworkConnections

    For $oNC In $aoNC

        ; Get domain type of network connection
        Local $iDT = $oNC.GetDomainType
        ConsoleWrite("DomainType = " & $iDT & @CRLF)

        ; Uncommenting following code line to get the adapter ID of network connection leads to error
        ; $oNC.GetAdapterId
    Next
EndFunc

Why does getting the adapter ID not work? 🤔

Link to comment
Share on other sites

For me it works:

Quote

+>Setting Hotkeys...--> Press Ctrl+Alt+Break to Restart or Ctrl+BREAK to Stop.
DomainType = 0
+>22:02:31 AutoIt3.exe ended.rc:0
+>22:02:31 AutoIt3Wrapper Finished.
>Exit code: 0    Time: 3.148

 

mfg (auto)Bert

Link to comment
Share on other sites

Maybe it has something to do with the fact that the parameters of these methods doesn't have native AutoIt data type. It looks like GetConnectionId method also crash with the same error message „The parameter is incorrect.”

When the words fail... music speaks.

Link to comment
Share on other sites

According to the documentation GetAdapterID returns a "Pointer to a GUID that specifies the adapter ID of the TCP/IP interface used by this network connection."
AutoIt comes with a $tagGUID structure. But I couldn't find an example how to use it :(

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Just tested also myself.  @water I don't think it is about a structure issue, if the method could return a structure, AutoIt would accept it.  I agree with @Andreik that it is probably a problem with AutoIt being unable to retrieve the specific value returned.  So I would suggest, that OP should use the interface approach, instead of the COM approach.

Link to comment
Share on other sites

Hello everyone,

first of all, thanks to everyone who has taken the trouble to deal with my problem! ☺️

I have now tried the approach with ObjCreateInterface.
For the interface INetworkListManager this works perfectly at first sight, the @error has the value 0 after the call of ObjCreateInterface.
Here is the corresponding code snippet:

#include <StructureConstants.au3>

Opt("MustDeclareVars", 1)

Main()

Func Main()
    Local Const $sCLSID_NetworkListManager = "{DCB00C01-570F-4A9B-8D69-199FDBA5723B}"
    Local Const $sIID_INetworkListManager = "{DCB00000-570F-4A9B-8D69-199FDBA5723B}"
    Local Const $sTag_INetworkListManager = _
            "GetNetworks hresult(int;ptr*);" & _
            "GetNetwork hresult(" & $tagGUID & ";ptr*);" & _
            "GetNetworkConnections hresult(ptr*);" & _
            "GetNetworkConnection hresult(" & $tagGUID & ";ptr*);" & _
            "IsConnectedToInternet hresult(short*);" & _
            "IsConnected hresult(short*);" & _
            "GetConnectivity hresult(int_ptr*);" & _
            "SetSimulatedProfileInfo hresult(struct*);" & _
            "ClearSimulatedProfileInfo hresult();"

    Local $oNLM_INetworkListManager = ObjCreateInterface($sCLSID_NetworkListManager, $sIID_INetworkListManager, $sTag_INetworkListManager)
    ConsoleWrite("ObjCreateInterface: @error = " & @error & @CRLF)
EndFunc

But it doesn't work with the INetworkConnection interface. Here @error has the value 1 after calling ObjCreateInterface.
Here is also the corresponding code snippet:

Opt("MustDeclareVars", 1)

Main()

Func Main()
    Local Const $sCLSID_NetworkListManager = "{DCB00C01-570F-4A9B-8D69-199FDBA5723B}"
    Local Const $sIID_INetworkConnection = "{DCB00005-570F-4A9B-8D69-199FDBA5723B}"
    Local Const $sTag_INetworkConnection = _
            "GetNetwork hresult(ptr*);" & _
            "IsConnectedToInternet hresult(short*);" & _
            "IsConnected hresult(short*);" & _
            "GetConnectivity hresult(int_ptr*);" & _
            "GetConnectionId hresult(struct*);" & _
            "GetAdapterId hresult(struct*);" & _
            "GetDomainType hresult(int_ptr*);"

    Local $oNLM_INetworkConnection = ObjCreateInterface($sCLSID_NetworkListManager, $sIID_INetworkConnection, $sTag_INetworkConnection)
    ConsoleWrite("ObjCreateInterface: @error = " & @error & @CRLF)
EndFunc

Here are the relevant lines from the file netlistmgr.idl:

[…]

// GUID Definitions
//
// DCB00000-570F-4A9B-8D69-199FDBA5723B  INetworkListManager
// DCB00001-570F-4A9B-8D69-199FDBA5723B  INetworkListManagerEvents
//
// DCB00002-570F-4A9B-8D69-199FDBA5723B  INetwork
// DCB00003-570F-4A9B-8D69-199FDBA5723B  IEnumNetworks
// DCB00004-570F-4A9B-8D69-199FDBA5723B  INetworkEvents
//
// DCB00005-570F-4A9B-8D69-199FDBA5723B  INetworkConnection
// DCB00006-570F-4A9B-8D69-199FDBA5723B  IEnumNetworkConnections
// DCB00007-570F-4A9B-8D69-199FDBA5723B  INetworkConnectionEvents
//
// DCB00008-570F-4A9B-8D69-199FDBA5723B  INetworkCostManager
// DCB00009-570F-4A9B-8D69-199FDBA5723B  INetworkCostManagerEvents
//
// DCB0000a-570F-4A9B-8D69-199FDBA5723B  INetworkConnectionCost
// DCB0000b-570F-4A9B-8D69-199FDBA5723B  INetworkConnectionCostEvents
//
// ... reserved
// DCB000FF-570F-4A9B-8D69-199FDBA5723B  Reserved
//
// DCB00D01-570F-4A9B-8D69-199FDBA5723B  Library: NetworkListManager
// DCB00C01-570F-4A9B-8D69-199FDBA5723B  CoClass: CNetworkListManager

[…]

[
    uuid(DCB00000-570F-4A9B-8D69-199FDBA5723B),
    helpstring("Network List Manager"),
    object,
    oleautomation,
    pointer_default(unique),
    dual
]
interface INetworkListManager : IDispatch
{
    typedef [v1_enum] enum NLM_ENUM_NETWORK
    {
        NLM_ENUM_NETWORK_CONNECTED    = 0x01,
        NLM_ENUM_NETWORK_DISCONNECTED = 0x02,
        NLM_ENUM_NETWORK_ALL          = 0x03
    } NLM_ENUM_NETWORK;

    [id(1), helpstring("Enumerate the list of networks in your compartment.")]
    HRESULT GetNetworks ([in] NLM_ENUM_NETWORK Flags,
                         [out, retval] IEnumNetworks **ppEnumNetwork);

    [id(2), helpstring("Get a network given a Network ID.")]
    HRESULT GetNetwork   ([in] GUID gdNetworkId,
                          [out, retval] INetwork **ppNetwork);

    [id(3), helpstring("Enumerate the complete list of all connections in your compartment.")]
    HRESULT GetNetworkConnections ([out, retval] IEnumNetworkConnections **ppEnum);

    [id(4), helpstring("Get a network connection by a Network Connection ID.")]
    HRESULT GetNetworkConnection ([in] GUID gdNetworkConnectionId,
                                  [out, retval] INetworkConnection **ppNetworkConnection);

    [propget, id(5), helpstring("Returns whether this machine has Internet connectivity.")]
    HRESULT IsConnectedToInternet([out, retval] VARIANT_BOOL* pbIsConnected);

    [propget, id(6), helpstring("Returns whether this machine has any network connectivity.")]
    HRESULT IsConnected([out, retval] VARIANT_BOOL* pbIsConnected);

    [id(7), helpstring("Returns the aggregated connectivity state of all networks on this machine.")]
    HRESULT GetConnectivity([out, retval] NLM_CONNECTIVITY* pConnectivity);

    [id(8), helpstring("Sets simulated connection profile information.")]
    HRESULT SetSimulatedProfileInfo([in] NLM_SIMULATED_PROFILE_INFO *pSimulatedInfo);

    [id(9), helpstring("Clear simulated cost data, if it has been set.")]
    HRESULT ClearSimulatedProfileInfo();
}

[…]

[
    uuid(DCB00005-570F-4A9B-8D69-199FDBA5723B),
    helpstring("Network Connection"),
    object,
    oleautomation,
    pointer_default(unique),
    dual
]
interface INetworkConnection : IDispatch
{
    [id(1), helpstring("Returns the associated network of this connection.")]
    HRESULT GetNetwork ([out, retval] INetwork **ppNetwork);

    [propget, id(2), helpstring("Returns whether this network connection has Internet connectivity.")]
    HRESULT IsConnectedToInternet([out, retval] VARIANT_BOOL* pbIsConnected);

    [propget, id(3), helpstring("Returns whether this network connection has any network connectivity.")]
    HRESULT IsConnected([out, retval] VARIANT_BOOL* pbIsConnected);

    [id(4), helpstring("Returns the connectivity state of this network connection.")]
    HRESULT GetConnectivity([out, retval] NLM_CONNECTIVITY* pConnectivity);

    [id(5), helpstring("Returns a unique identifer for this connection.")]
    HRESULT GetConnectionId ([out, retval] GUID *pgdConnectionId);

    [id(6), helpstring("Returns the network adapter used by this connection. There might be multiple connections using the same AdapterId.")]
    HRESULT GetAdapterId ([out, retval] GUID *pgdAdapterId);

    [id(7), helpstring("Returns the domain type of this network connection.")]
    HRESULT GetDomainType ([out, retval] NLM_DOMAIN_TYPE *pDomainType);
}

[…]

Based on this, I can't find any error in my code. 😒

Link to comment
Share on other sites

The problem is that those objects inherit from IDispatch, you will need to add the methods before, like this :

Interface()

Func Interface()
  Local Const $tagIDispatch = _
      "GetTypeInfoCount hresult(dword*);" & _
      "GetTypeInfo hresult(dword;dword;ptr*);" & _
      "GetIDsOfNames hresult(struct*;struct*;dword;dword;struct*);" & _
      "Invoke hresult(uint;struct*;dword;word;struct*;struct*;ptr;uint*);"

  Local Const $sCLSID_NetworkListManager = "{DCB00C01-570F-4A9B-8D69-199FDBA5723B}"
  Local Const $sIID_INetworkListManager = "{DCB00000-570F-4A9B-8D69-199FDBA5723B}"
  Local Const $sTag_INetworkListManager = $tagIDispatch & _
      "GetNetworks hresult(int;ptr*);" & _
      "GetNetwork hresult(ptr;ptr*)" & _
      "GetNetworkConnections hresult(ptr*);" & _
      "GetNetworkConnection hresult(ptr;ptr*);" & _
      "IsConnectedToInternet hresult(short*);" & _
      "IsConnected hresult(short*);" & _
      "GetConnectivity hresult(int_ptr*);" & _
      "SetSimulatedProfileInfo hresult(struct*);" & _
      "ClearSimulatedProfileInfo hresult();"

  Local $oNLM_INetworkListManager = ObjCreateInterface($sCLSID_NetworkListManager, $sIID_INetworkListManager, $sTag_INetworkListManager)
  ConsoleWrite(IsObj($oNLM_INetworkListManager) & @CRLF)
  Local $hResult, $oNWCs
  $hResult = $oNLM_INetworkListManager.GetNetworkConnections($oNWCs)
  ConsoleWrite($hResult & "/" & $oNWCs & @CRLF)
EndFunc   ;==>Interface

 

Link to comment
Share on other sites

47 minutes ago, Nine said:

The problem is that those objects inherit from IDispatch, you will need to add the methods before.

Hi @Nine,

thanks for the tip! 👍
With the interface INetworkListManager it works fine now.
But with the interface INetworkConnection it still doesn't work:

Opt("MustDeclareVars", 1)

Main()

Func Main()
    Local Const $tagIDispatch = _
            "GetTypeInfoCount hresult(uint_ptr*);" & _
            "GetTypeInfo hresult(uint;dword;ptr*);" & _
            "GetIDsOfNames hresult(struct*;wstr*;uint;dword;long_ptr*);" & _
            "Invoke hresult(long;struct*;dword;word;struct*;variant*;ptr*;uint_ptr*);"
    Local Const $sCLSID_NetworkListManager = "{DCB00C01-570F-4A9B-8D69-199FDBA5723B}"
    Local Const $sIID_INetworkConnection = "{DCB00005-570F-4A9B-8D69-199FDBA5723B}"
    Local Const $sTag_INetworkConnection = _
            $tagIDispatch & _
            "GetNetwork hresult(ptr*);" & _
            "IsConnectedToInternet hresult(short*);" & _
            "IsConnected hresult(short*);" & _
            "GetConnectivity hresult(int_ptr*);" & _
            "GetConnectionId hresult(struct*);" & _
            "GetAdapterId hresult(struct*);" & _
            "GetDomainType hresult(int_ptr*);"

    Local $oNLM_INetworkConnection = ObjCreateInterface($sCLSID_NetworkListManager, $sIID_INetworkConnection, $sTag_INetworkConnection)
    ConsoleWrite("ObjCreateInterface: @error = " & @error & @CRLF)
EndFunc

Do you have any idea what the problem is?

Link to comment
Share on other sites

  • Solution
Posted (edited)

Maybe this;

 

Local Const $tagIDispatch = _
        "GetTypeInfoCount hresult(dword*);" & _
        "GetTypeInfo hresult(dword;dword;ptr*);" & _
        "GetIDsOfNames hresult(struct*;struct*;dword;dword;struct*);" & _
        "Invoke hresult(uint;struct*;dword;word;struct*;struct*;ptr;uint*);"


Local Const $sIID_INetworkConnection = "{DCB00005-570F-4A9B-8D69-199FDBA5723B}"

Local Const $sTag_INetworkConnection = $tagIDispatch & _
        "GetNetwork hresult()" & _
        "IsConnectedToInternet hresult();" & _
        "IsConnected hresult();" & _
        "GetConnectivity hresult();" & _
        "GetConnectionId hresult();" & _
        "GetAdapterId hresult(clsid*);" & _
        "GetDomainType hresult(int*);"


Local $oObj = ObjCreate("{DCB00C01-570F-4A9B-8D69-199FDBA5723B}") ;NetworkListManager


Local $oConnections = $oObj.GetNetworkConnections()
For $oConnection In $oConnections
    Local $oConnectionCast = ObjCreateInterface($oConnection, $sIID_INetworkConnection, $sTag_INetworkConnection)
    Local $GUID = ""
    Local $iDomainType=0
    $oConnectionCast.GetAdapterId($GUID)
    $oConnectionCast.GetDomainType($iDomainType)
    ConsoleWrite("GetAdapterId: " & $GUID & @CRLF)
    ConsoleWrite("GetDomainType: " & $iDomainType & @CRLF)
;~  ConsoleWrite($oConnection.GetAdapterId & @CRLF) ;this will not work
;~  ConsoleWrite($oConnection.GetDomainType & @CRLF) ;this will work
Next

 

 

Saludos

Edited by Danyfirex
Link to comment
Share on other sites

Or a full interface version :

#include <Constants.au3>

Opt("MustDeclareVars", True)

Interface()

Func Interface()
  Local Const $sTagIDispatch = _
      "GetTypeInfoCount hresult(dword*);" & _
      "GetTypeInfo hresult(dword;dword;ptr*);" & _
      "GetIDsOfNames hresult(struct*;struct*;dword;dword;struct*);" & _
      "Invoke hresult(uint;struct*;dword;word;struct*;struct*;ptr;uint*);"

  Local Const $sCLSID_NetworkListManager = "{DCB00C01-570F-4A9B-8D69-199FDBA5723B}"
  Local Const $sIID_INetworkListManager = "{DCB00000-570F-4A9B-8D69-199FDBA5723B}"
  Local Const $sTag_INetworkListManager = $sTagIDispatch & _
      "GetNetworks hresult(int;ptr*);" & _
      "GetNetwork hresult(ptr;ptr*)" & _
      "GetNetworkConnections hresult(ptr*);" & _
      "GetNetworkConnection hresult(ptr;ptr*);" & _
      "IsConnectedToInternet hresult(short*);" & _
      "IsConnected hresult(short*);" & _
      "GetConnectivity hresult(int_ptr*);" & _
      "SetSimulatedProfileInfo hresult(struct*);" & _
      "ClearSimulatedProfileInfo hresult();"

  Local Const $sIID_IEnumNetworkConnections = "{DCB00006-570F-4A9B-8D69-199FDBA5723B}"
  Local Const $sTag_IEnumNetworkConnections = $sTagIDispatch & _
      "NewEnum hresult(ptr*);" & _
      "Next hresult(int;ptr*;int*);" & _
      "Skip hresult(int);" & _
      "Reset hresult();" & _
      "Clone hresult(ptr*);"

  Local Enum $NLM_CONNECTIVITY_DISCONNECTED, $NLM_CONNECTIVITY_IPV4_NOTRAFFIC, $NLM_CONNECTIVITY_IPV6_NOTRAFFIC, _
    $NLM_CONNECTIVITY_IPV4_SUBNET = 0x10, $NLM_CONNECTIVITY_IPV4_LOCALNETWORK = 0x20, $NLM_CONNECTIVITY_IPV4_INTERNET = 0x40, _
    $NLM_CONNECTIVITY_IPV6_SUBNET = 0x100, $NLM_CONNECTIVITY_IPV6_LOCALNETWORK = 0x200, $NLM_CONNECTIVITY_IPV6_INTERNET = 0x400
  Local Const $sIID_INetworkConnection = "{DCB00005-570F-4A9B-8D69-199FDBA5723B}"
  Local Const $sTag_INetworkConnection = $sTagIDispatch & _
      "GetNetwork hresult(ptr*)" & _
      "IsConnectedToInternet hresult(boolean*);" & _
      "IsConnected hresult(boolean*);" & _
      "GetConnectivity hresult(int*);" & _
      "GetConnectionId hresult(clsid*);" & _
      "GetAdapterId hresult(clsid*);" & _
      "GetDomainType hresult(int*);"

  Local Enum $NLM_NETWORK_CATEGORY_PUBLIC, $NLM_NETWORK_CATEGORY_PRIVATE, $NLM_NETWORK_CATEGORY_DOMAIN_AUTHENTICATED
  Local Const $sIID_INetwork = "{DCB00002-570F-4A9B-8D69-199FDBA5723B}"
  Local Const $sTag_INetwork = $sTagIDispatch & _
    "GetName hresult(wstr*);" & _
    "SetName hresult(wstr);" & _
    "GetDescription hresult(wstr*);" & _
    "SetDescription hresult(wstr);" & _
    "GetNetworkId hresult(clsid*);" & _
    "GetDomainType hresult(int*);" & _
    "GetNetworkConnections hresult(ptr*);" & _
    "GetTimeCreatedAndConnected hresult(int*;int*;int*;int*);" & _
    "IsConnectedToInternet hresult(boolean*);" & _
    "IsConnected hresult(boolean*);" & _
    "GetConnectivity hresult(int*);" & _
    "GetCategory hresult(int*);" & _
    "SetCategory hresult(int);"

  Local $pNWCs, $iNum, $pConn, $oNWC, $sGUID, $pNet, $oNet, $sName
  Local $oNLM_INetworkListManager = ObjCreateInterface($sCLSID_NetworkListManager, $sIID_INetworkListManager, $sTag_INetworkListManager)
  If Not IsObj($oNLM_INetworkListManager) Then Exit MsgBox($MB_OK, "Error", "INetworkListManager")
  $oNLM_INetworkListManager.GetNetworkConnections($pNWCs)
  Local $oNWCs = ObjCreateInterface($pNWCs, $sIID_IEnumNetworkConnections, $sTag_IEnumNetworkConnections)
  If Not IsObj($oNWCs) Then Exit MsgBox($MB_OK, "Error", "IEnumNetworkConnections")

  While True
    $oNWCs.Next(1, $pConn, $iNum)
    If Not $iNum Then ExitLoop
    $oNWC = ObjCreateInterface($pConn, $sIID_INetworkConnection, $sTag_INetworkConnection)
    If Not IsObj($oNWC) Then Exit MsgBox($MB_OK, "Error", "INetworkConnection")
    $oNWC.GetAdapterId($sGUID)
    ConsoleWrite("GetAdapterId: " & $sGUID & @CRLF)
    $oNWC.GetNetwork($pNet)
    $oNet = ObjCreateInterface($pNet, $sIID_INetwork, $sTag_INetwork)
    If Not IsObj($oNet) Then Exit MsgBox($MB_OK, "Error", "INetwork")
    $oNet.GetName($sName)
    ConsoleWrite("GetName: " & $sName & @CRLF)
  WEnd
EndFunc   ;==>Interface

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...