Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Here is a WBem WMI C++ example

1,605 views
Skip to first unread message

Jimi

unread,
Apr 16, 2002, 11:11:55 AM4/16/02
to
As you know most of the WMI example/docs in the MSDN are geared toward
VB and ASP. Finding a good C++ example seems to be hard. After
taking our team a couple of days to figure out how to use this WMI
stuff, we've decided to post a complete working example in C++.
Hopefully this will save you some time.

void CWbemClientDlg::OnNetworkBtn()
{
IWbemLocator *pLocator = NULL;
IWbemServices *pNamespace = 0;
IWbemClassObject *pClass = NULL;
IWbemClassObject *pMethod = NULL;
IWbemClassObject *pInInst = NULL;
IWbemClassObject *pOutInst = NULL;

// Strings needed later
BSTR className = SysAllocString(L"Win32_NetworkAdapterConfiguration");
BSTR methodName = SysAllocString(L"EnableStatic");
BSTR namespacePath = SysAllocString(L"root\\cimv2");

// Create WbemLocator Instance
HRESULT hr;
hr = CoCreateInstance(CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER,
IID_IWbemLocator, (LPVOID *) &pLocator);

// Using the WbemLocator, connect to the namespace
hr = pLocator->ConnectServer(namespacePath, NULL, NULL, NULL, 0,
NULL, NULL, &pNamespace);

// Get the class object
hr = pNamespace->GetObject(className, 0, NULL, &pClass, NULL);

// Get the method we want to use in this class
hr = pClass->GetMethod(methodName, 0, &pMethod, NULL);

// Spawn an instance of the method so we can set it up (parameters)
hr = pMethod->SpawnInstance(0, &pInInst);

// EnableStatic() expects to receive the args in an array
BSTR ArgNameIP = SysAllocString(L"IPAddress");
BSTR ArgNameSM = SysAllocString(L"SubnetMask");
BSTR IP = SysAllocString(L"172.21.0.1");
BSTR SM = SysAllocString(L"255.255.0.0");

SAFEARRAYBOUND sab[1];
sab[0].cElements = 1;
sab[0].lLbound = 0;

LONG i = 0; // index needed for SafeArrayPutElement()

// var1 is the array that holds one element which is IP
VARIANT var1;
var1.vt = VT_ARRAY | VT_BSTR;
var1.parray = SafeArrayCreate(VT_BSTR, 1, sab); // create the
array
SafeArrayPutElement(var1.parray, &i, IP); // put IP in the array
at pos 0

// var2 is the array that holds one element which is SM
VARIANT var2;
var2.vt = VT_ARRAY | VT_BSTR;
var2.parray = SafeArrayCreate(VT_BSTR, 1, sab); // create the
array
SafeArrayPutElement(var2.parray, &i, SM); // put SM in the array
at pos 0

// put the ArgName and the Argument itself into the Method instance
we have
hr = pInInst->Put(ArgNameIP, 0, &var1, 0);
VariantClear(&var1);
hr = pInInst->Put(ArgNameSM, 0, &var2, 0);
VariantClear(&var2);

// Enumerate the instances of the NetworkAdapter to get at the __PATH
property
// to the one and only network card we really want to modify
IEnumWbemClassObject* pEnum;
hr = pNamespace->CreateInstanceEnum(className,
WBEM_FLAG_RETURN_IMMEDIATELY | WBEM_FLAG_FORWARD_ONLY,
NULL, &pEnum);

// Get the instance
ULONG numRet;
BSTR PropName;
VARIANT desc;
VARIANT dhcp;
VARIANT pathVar;
IWbemClassObject *pInstance;
while ( hr != WBEM_S_FALSE)
{
hr = pEnum->Next(WBEM_INFINITE, (ULONG)1, &pInstance, &numRet);

// we know that the one with DHCP is the one we want
PropName = SysAllocString(L"DHCPEnabled");
pInstance->Get(PropName, 0, &dhcp, 0, 0);
if (dhcp.bVal)
{
// Print out the description just for us to see
SysFreeString(PropName);
PropName = SysAllocString(L"Description");
pInstance->Get(PropName, 0, &desc, 0, 0);
CString message = desc.bstrVal;
MessageBox(message);
SysFreeString(PropName);
break;
}
SysFreeString(PropName);
}

// now we have the instance of the actual network card we want to
modify
// let's query it's PATH property so that we can call the method on
it.
PropName = SysAllocString(L"__PATH");
pInstance->Get(PropName, 0, &pathVar, 0, 0);
BSTR InstancePath = pathVar.bstrVal;

// finally we get to call the actuall method we want (EnableStatic())
hr = pNamespace->ExecMethod(InstancePath, methodName, 0, NULL,
pInInst, &pOutInst, NULL);
if(hr != WBEM_S_NO_ERROR)
{
BSTR Text;
hr = pOutInst->GetObjectText(0, &Text);
CString strText = Text;
MessageBox(strText);
}

// We're done!
// Free up resources
SysFreeString(className);
SysFreeString(methodName);
SysFreeString(namespacePath);
SysFreeString(ArgNameIP);
SysFreeString(ArgNameSM);
SysFreeString(IP);
SysFreeString(SM);
SysFreeString(PropName);
SysFreeString(InstancePath);
pLocator->Release();
pClass->Release();
pMethod->Release();
pNamespace->Release();
pInInst->Release();
pOutInst->Release();
pEnum->Release();
pInstance->Release();
CoUninitialize();
}

Eric Sassaman [MS]

unread,
Apr 16, 2002, 10:06:56 PM4/16/02
to
Please note that Microsoft is not monitoring this newsgroup any
longer. This newsgroup may be deleted at a future date.

We highly recommend you post to microsoft.public.win32.programmer.wmi
as the best place to discuss any WMI programming issues. MSDN managed
newsgroup users need to post future questions there as well. Thanks!

If you are not sure where to post, see http://msdn.microsoft.com/newsgroups
for a good list of newsgroups for programmers, or
http://communities.microsoft.com for more Microsoft products.
_____________
Eric Sassaman
Microsoft Support
This posting is provided "AS IS" with no warranties, and confers no rights.

"Jimi" <Jimi....@GambroBCT.com> wrote in message
news:88c4053e.02041...@posting.google.com...

Schorschi Decker

unread,
Jun 3, 2002, 11:05:10 PM6/3/02
to
Does anyone have a straight-C example? I have worked a quirky version in
that the specific WMI code is compiled with the C++ compiler not the C
compiler in VS C .NET, but this is not a clean solution.


"Jimi" <Jimi....@GambroBCT.com> wrote in message
news:88c4053e.02041...@posting.google.com...

0 new messages