Hannes
Hannes

Reputation:

How can I find out the current color depth of a machine running vista/w7?

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.

Upvotes: 6

Views: 2955

Answers (4)

Indy9000
Indy9000

Reputation: 8851

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);

Upvotes: 3

Rob
Rob

Reputation: 78628

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);

Upvotes: 10

MSalters
MSalters

Reputation: 179789

Call GetDeviceCaps() to retrieve BITSPIXEL

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

Upvotes: 0

Goz
Goz

Reputation: 62323

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.

Upvotes: 3

Related Questions