views:

269

answers:

4

Hi!

I want to check the current color depth of the OS to warn users if they try to run my application with a "wrong" color depth (using c++ & Qt).

I guess there's a win api call to get this information, but I couldn't find anything...

Thaks for any hint, Hannes

+2  A: 

You can do it using WMI.

int bitDepth = -1;
hr = CoInitializeEx( NULL, COINIT_MULTITHREADED );
if ( SUCCEEDED( hr ) )
{
    //  hr = CoInitializeSecurity( NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_DEFAULT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, NULL ); 
    if ( SUCCEEDED( hr ) )
    {
     IWbemLocator* pLoc = NULL;
     hr = CoCreateInstance( CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER, IID_IWbemLocator, (void**)&pLoc );
     if ( SUCCEEDED( hr ) )
     {
      IWbemServices* pSvc = NULL;
      hr = pLoc->ConnectServer( BSTR( L"ROOT\\CIMV2" ),  NULL, NULL, 0, NULL, 0, 0, &pSvc );
      if ( SUCCEEDED( hr ) )
      {
       hr = CoSetProxyBlanket( pSvc, RPC_C_AUTHN_WINNT, RPC_C_AUTHZ_NONE, NULL, RPC_C_AUTHN_LEVEL_CALL, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE );
       if ( SUCCEEDED( hr ) )
       {
        IEnumWbemClassObject* pEnumerator = NULL;
        hr = pSvc->ExecQuery( L"WQL", L"SELECT * FROM Win32_DisplayConfiguration", WBEM_FLAG_FORWARD_ONLY/* | WBEM_FLAG_RETURN_IMMEDIATELY*/, NULL, &pEnumerator );
        if ( SUCCEEDED( hr ) )
        {
         IWbemClassObject* pDisplayObject = NULL;
         ULONG numReturned = 0;
         hr = pEnumerator->Next( WBEM_INFINITE, 1, &pDisplayObject, &numReturned );
         if ( numReturned != 0 )
         {
          VARIANT vtProp;
          pDisplayObject->Get( L"BitsPerPel", 0, &vtProp, 0, 0 );
          bitDepth = vtProp.uintVal;
         }
        }
        pEnumerator->Release();
       }
      }
      pSvc->Release();

     }
     pLoc->Release();
    }
}
// bitDepth wshould now contain the bitDepth or -1 if it failed for some reason.
Goz
APIs like this make me joyfully remember why I switched to .NET development :)
Adrian Grigore
hehehehe ... Its not a vast amount easier accessing WMI from .NET mind ;)
Goz
A: 

Call GetDeviceCaps() to retrieve BITSPIXEL

It's not a "per-machine" property actually, you need a HDC.

MSalters
+4  A: 

On Windows you could use GetDeviceCaps with the BITSPIXEL flag but you'll need a screen DC first (GetDC could fetch you one).

HDC dc = GetDC(NULL);
int bitsPerPixel = GetDeviceCaps(dc, BITSPIXEL);
ReleaseDC(NULL, dc);
Rob
works perfect - thanks a lot!
A: 

You should be able to get the bits per pixel value using

HDC hdc = GetDC(NULL);
int colour_depth = GetDeviceCaps(hdc,BITSPIXEL);
ReleaseDC(NULL,hdc);
Indeera