Adam Driscoll
Adam Driscoll

Reputation: 9483

UINT64 in Native WMI provider doesn't return data on certain systems

I have created a native WMI provider following This tutorial on MSDN. My MOF for the class looks similar to this:

[dynamic, provider("CacheProvider")]
class Cache
{
    [key]
    String Path;
    uint32 Type;
    uint64 MaxSize;
    uint64 Size;
    boolean Enabled;
};

All the data is returned correctly except the uint64 values. I have read that for uint64 values you need to actually provide the data as a BSTR. It does work on 99% of the machines I have tried it on. This is how I am accomplishing that.

v.vt = VT_BSTR;
v.bstrVal = ToLongLongString(MaxSize);
sc = (*pNewInst)->Put(L"MaxSize", 0, &v, 0);
VariantClear(&v);

The ToLongLongString function looks like this.

bstr_t ToLongLongString(LONGLONG llValue)
{
    wchar_t szValue[21];
    SecureZeroMemory(szValue, sizeof(szValue));
    swprintf_s(szValue, L"%lld", llValue);
    bstr_t bstrFormat(szValue);
    return bstrFormat;
}

I have validated that the string being returned by this function is formatted correctly. It just appears that it is not making it through the WMI system for whatever reason. The only machine I do not seeing it working on is a 2012 R2 server.

Upvotes: 0

Views: 567

Answers (2)

Adam Driscoll
Adam Driscoll

Reputation: 9483

The problem was due to how I was assigning the BSTR.

Since ToLongLongString returns a bstr_t, it passes the BSTR value to the variant. Since I never detached the BSTR from the bstr_t the BSTR was being released once the bstr_t went out of scope.

Calling Detach fixed the issue.

Upvotes: 0

Sunny Chakraborty
Sunny Chakraborty

Reputation: 405

From here: http://msdn.microsoft.com/en-us/library/aa393262(v=vs.85).aspx

Note When querying for property values with a uint64 or sint64 data type in a scripting language like VBScript, WMI returns string values. Unexpected results can occur when comparing these values, because comparing strings returns different results than comparing numbers. For example, "10000000000" is less than "9" when comparing strings, and 9 is less than 10000000000 when comparing numbers. To avoid confusion you should use the CDbl method in VBScript when properties of type uint64 or sint64 are retrieved from WMI.

Can you try using CDBL ?

Upvotes: 1

Related Questions