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;
}
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:
System headers (like
windows.h) are included using angle brackets, like so: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:
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:
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
HRESULTvalue, which is a common way that COM functions indicate success or failure. You can use theSUCCEEDEDmacro to test whether the function call was successful.That’s how you call the
GetNetworkConnectionsfunction. But uh-oh, I just mentioned COM in that last paragraph. Sure enough, this is actually a COM API, provided by theINetworkListManagerinterface. So it gets a whole lot more complicated than just calling this single function.GetNetworkConnectionsis 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 theCLSID_NetworkListManagerCOM 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:
(Yes, COM code is often ugly. You can omit the error-checking if you’re brave, but I don’t recommend it.)