How can I get an HDC object from a CDC object?

20,464

Solution 1

CDC class has operator HDC() defined which allows the compiler to convert a CDC object to HDC implicitly. Hence if you have CDC* and a function which takes HDC then you just dereference the pointer and send it to the function.

Solution 2

When you have CDC object it will be implicitly converted to HDC when necessary:

CDC dc;
HDC hdc = dc; // HDC hdc = dc.operator HDC();

If you have pointer to CDC object then using function GetSafeHdc will look more clear:

CDC* pdc = SOME;
HDC hdc = pdc->GetSafeHdc();

Solution 3

CDC is a C++ class which - to a reasonable approximation - encapsulates an HDC, which is a handle to a device context.

The documenation which you link to describes a conversion operator, which is a C++ construct that classes can supply to allow implicit conversion from an instance of a class to some other type. In this case the implicit conversion results in the underlying handle (HDC) which the CDC instance encapsulates.

You can perform the conversion by using a CDC instance anywhere were it needs to be converted to an HDC.

Most simply:

void f( const CDC& cdc )
{
    HDC hdc = cdc;

    // use hdc here
}

Solution 4

HDC hDC = dc;

Solution 5

Just assign it.

CDC cdc = something.
HDC hdc = cdc;
if (hdc != 0)
{
  //success...
}
Share:
20,464
samoz
Author by

samoz

I am a cryptographer, working in the West Lafayete, IN area. My interests include reverse engineering, information security, ellipic curves, trusted computing environments, operating systems, and embedded systems. Most of my reversing is on Windows, but I like to practice my make-fu and Unix-fu on Arch Linux.

Updated on August 06, 2020

Comments

  • samoz
    samoz over 3 years

    I have an object, dc, of type CDC and I'd like to get an HDC object.

    I read the MSDN documentation here, but don't really understand it.

    Can someone provide me with a brief example/explanation on how to do this?