The Mask
The Mask

Reputation: 17427

using INetwork::GetNetworkConnections() in C++

I'm new in C++ and I'm looking for an code example how to write the enumeration to set in GetNetworkConnections(). the documentation have no an code example how do it.

my code:

#include "stdafx.h"
#include "windows.h"
#include "Netlistmgr.h"

int _tmain(int argc, _TCHAR* argv[])
{
    HRESULT hResult = INetwork::GetNetworkConnections( ?? );
    return 0;
}

Upvotes: 3

Views: 4527

Answers (1)

Cody Gray
Cody Gray

Reputation: 244732

First things first! Your sample code is already wrong. System headers are included using a different syntax than headers in your project.

Headers in your project are included with quotation marks around the name of the header, like so:

#include "stdafx.h"

System headers (like windows.h) are included using angle brackets, like so:

#include <windows.h>
#include <netlistmgr.h>

Make sure that you get that correct!


The prototype for the function given in the documentation indicates that it accepts a single parameter that is a pointer to a pointer:

HRESULT STDMETHODCALLTYPE GetNetworkConnections(
  [out]  IEnumNetworkConnections **ppEnum
);

I assume you know the C++ language well enough to know what that means. The function is going to modify a pointer variable, and the only way that it can modify something is for it to work on a pointer to that object. So you end up with double pointers, because you're modifying a pointer.

Again, the documentation gives you a clue as to how it works when it describes the parameters:

ppEnum [out]

Pointer to a pointer that receives an IEnumNetworkConnections interface instance that enumerates all network connections on the machine.

You call it by declaring a pointer variable of the correct type, and then passing the address of that variable into the function.

The function returns an HRESULT value, which is a common way that COM functions indicate success or failure. You can use the SUCCEEDED macro to test whether the function call was successful.


That's how you call the GetNetworkConnections function. But uh-oh, I just mentioned COM in that last paragraph. Sure enough, this is actually a COM API, provided by the INetworkListManager interface. So it gets a whole lot more complicated than just calling this single function.

GetNetworkConnections is not a static method, so it can't be called directly from the interface. You have to instantiate an object instance that implements that interface, and then call the member method on that object. Thus, you need to initialize COM first in your application, and then create the CLSID_NetworkListManager COM object.

If you go a level up in the documentation, away from the API "Reference" and to a page "About" the API, you'll generally see some sample code. For example, here.

Unfortunately, this won't tell or show you everything you need to know about COM. It's going to assume you already know how to do COM programming. And you should. Look up some links for more information; I can't write a full tutorial here, and there are lots of dark corners and alleys just waiting to trip you up.

Alas, working sample code for the single function that you're attempting to call:

#include "stdafx.h"       // include your app's precompiled header
#include <windows.h>      // include the base Windows header
#include <ObjBase.h>      // include the base COM header
#include <netlistmgr.h>

// Instruct linker to link to the required COM libraries
#pragma comment(lib, "ole32.lib")

int _tmain(int argc, _TCHAR* argv[])
{
    // Initialize COM.
   if (SUCCEEDED(CoInitializeEx(NULL, COINIT_MULTITHREADED)))
   {
      // Initializing COM was successful, so declare a pointer
      // to an INetworkListManager object.
      INetworkListManager* pNetworkListManager; 

      // Create an instance of the CLSID_NetworkListManger COM object,
      // using the SUCCEEDED macro to test for success.
      if (SUCCEEDED(CoCreateInstance(CLSID_NetworkListManager, NULL,
                                     CLSCTX_ALL, IID_INetworkListManager,
                                     (LPVOID*)&pNetworkListManager)))
      {
         // Creating the object was successful.
         //
         // Declare your pointer to an IEnumNetworkConnections object,
         // which the function call will set.
         IEnumNetworkConnections* pEnum;

         // Call the function, passing in the address of your pointer,
         // and test for success using the SUCCEEDED macro.
         if (SUCCEEDED(pNetworkListManager->GetNetworkConnections(&pEnum)))
         {
               // The function call succeeded.
               // 
               // pEnum contains a valid pointer to an IEnumNetworkConnections
               // object, which you can now use.
               //
               // ...
         }
      }
   }

   // Uninitialize COM.
   // (This should be called on application shutdown.)
   CoUninitialize();

   return 0;
}

(Yes, COM code is often ugly. You can omit the error-checking if you're brave, but I don't recommend it.)

Upvotes: 12

Related Questions