Discussion:
How to get a string array from a VARIANT
(too old to reply)
Joao Rego
2006-01-06 10:04:01 UTC
Permalink
Hello,

Using VisualStudio 2003 and C language.
I'm writing a service to get infromation from the system through WMI.
I want to get IPAddress from Win32_NetworkAdapterConfiguration.
I started from the sample
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/example__getting_wmi_data_from_the_local_computer.asp

The point is that IPAddress is a string array:

....
IWbemClassObject *pclsObj;
HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn);
VARIANT vtProp;
VariantInit(&vtProp);

// Get the value of the property
hr = pclsObj->Get(L"IPAddress", 0, &vtProp, 0, 0);
...

How do I get the IP's from the vtProp?

The following does not work with the string[] data type of IPAddress:
szIPAddress char[128];
sprintf(svIpAddress, "IPAddress : %s", W2A(vtProp.bstrVal));

Thanks for any help on this question.

João Rêgo
Simon Trew
2006-01-06 10:53:17 UTC
Permalink
Post by Joao Rego
IWbemClassObject *pclsObj;
HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn);
VARIANT vtProp;
VariantInit(&vtProp);
hr = pclsObj->Get(L"IPAddress", 0, &vtProp, 0, 0);
...
How do I get the IP's from the vtProp?
szIPAddress char[128];
sprintf(svIpAddress, "IPAddress : %s", W2A(vtProp.bstrVal));
Presumably if the data type is string[] then the resultant VARIANT will be
of type VT_ARRAY|VT_BSTR? In which case, you will need to access the
safearray, e.g:

BSTR firstIPAddr;
static const long index = 0; // index of the element to get
hr = ::SafeArrayGetElement(V_ARRAY(&vtProp, &index, &firstIPAddr));
if (SUCCEEDED(hr))
{
// do some stuff

// remember to free the string when you're done with it
::SysFreeString(firstIPAddr);
}
Simon Trew
2006-01-06 10:55:09 UTC
Permalink
Sorry small typo in last post- parenthesis in the wrong place. The line
should read:

hr = ::SafeArrayGetElement(V_ARRAY(&vtProp), &index, &firstIPAddr);
Joao Rego
2006-01-06 11:14:02 UTC
Permalink
Simon,

Thanks for the tip... now it's working perfect!!
Post by Simon Trew
Sorry small typo in last post- parenthesis in the wrong place. The line
hr = ::SafeArrayGetElement(V_ARRAY(&vtProp), &index, &firstIPAddr);
Loading...