Conversion Question

Hi Folks;

I'm running a WMI query to get the sizes of the disk drives in a computer. The result of course is a large number but I want to convert that number into a nicer format (i.e. 80G).

So this is what I have thus far:

oid GetWMITotal(HRESULT hres, IWbemServices *pSvc, char * sQuery, LPCWSTR sCol, char * data)

{

_variant_t varData;

IEnumWbemClassObject* pEnumerator = NULL;

ULONG uReturn = 1;

IWbemClassObject *pclsObj;

char cSuffix;

int iStringOp = 0;

hres = pSvc->ExecQuery(

bstr_t("WQL"),

bstr_t(sQuery),

WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,

NULL,

&pEnumerator);

HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn);

hr = pclsObj->Get(sCol, 0, &varData, 0, 0);

INT Size = varData; // CAN'T GET DRIVE SIZE HERE

It's here that's the problem. I can get RAM size but not disk drive size. Is there something other than INT that I can use for both or do I have to create another function

if (Size >= 1000000000)

{

Size /= 1000000000;

cSuffix = 'G';

}

else if (Size >= 1000000)

{

Size /= 1000000;

cSuffix = 'M';

}

itoa(Size, data, 10);

while (data[iStringOp] != '\0')

iRAMStringOp++;

data[iStringOp] = cSuffix; data[iStringOp + 1] = '\0';

pEnumerator->Release();

pclsObj->Release();

}




Answer this question

Conversion Question

  • Priya Shekhar

    If you're requesting the field Size of the Win32_DiskDrive table, that's a uint64. This value won't fit in your INT Size, which is 32 bit. Representing the drive size with such a number would limit us to 2^32, 4294967296 or rather 4.2GB. What you have to do is use a

    unsigned __int64 LargeSize = varData;
    int Size;

    then do your

    if(LargeSize >= 1000000000)
    {
    Size = LargeSize / 1000000000;
    cSuffix = 'G';

    and so forth.



  • Abdullah Sowayan

    Still says it can't convert _variant_t to __int64. This is what I have:

    _variant_t varData;

    ...

    HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn);

    hr = pclsObj->Get(sCol, 0, &varData, 0, 0);

    __int64 Size = varData;

    ...

    I've noticed that _variant_t can take inputs. Up until late last week I've never used it before but is there something I can do with the inputs



  • LievenI

    Add

    #define _WIN32_WINNT 0x501

    to the top of your precompiled header (if any). At least be sure that this line precedes your #include <comutil.h>.



  • SoniaJulka

    I've got to blame this one on the temperature. My error reading abilities are melting away

    __int64 LargeSize = (__int64)varData; ought to do the trick.



  • robinjam

    It says cannot convert from '_variant_t' to unsigned __int64. What am I missing



  • Chimme

    Maybe the heat is messing up my computer since it still insists 'type cast' : cannot convert '_variant_t' to '__int64'

  • Kestutis

     Viorel. wrote:

    It seems that you are working with VC 6.0. In this case __int64 is not supported by _variant_t. Try long type instead.

    #if (_WIN32_WINNT >= 0x0501)
        _variant_t(__int64 i8Src) throw();                                  // Creates a VT_I8
        _variant_t(unsigned __int64 ui8Src) throw();                        // Creates a VT_UI8
    #endif

    That's what gave him a hard time. I was puzzled by why the variant was so reluctant to pick the right extractor operator.. This somewhat explains it.



  • Davids Learning

    That seemed to work! Thanks!

  • Dracosveen

    Try removing the "unsigned" prefix of your __int64.

  • ozhonetech

    It seems that you are working with VC 6.0. In this case __int64 is not supported by _variant_t. Try long type instead.


  • Conversion Question