What I am working on has sets of window controls. Controls are constantly being either destroyed, or created.

void ChangeControl() {

// remove all controls, existing, or not
hwnd = GetDlgItem(hWnd, IDC_BTN_FOOA);
hwnd = GetDlgItem(hWnd, IDC_BTN_FOOB);
hwnd = GetDlgItem(hWnd, IDC_BTN_FOOC);

// add the control wanted
        left, top, BUTTON_WIDTH, BUTTON_HEIGHT, hWnd, (HMENU)IDC_BTN_FOOB, GetModuleHandle(NULL), NULL);

Test case:

DWORD errorResult;
testHwnd = GetDlgItem(hWnd, IDC_BTN_START); 
if (testHwnd) { 
else { 
    errorResult = GetLastError(); 

I am wondering if this method is safe, or if I should be checking if a control exists before calling DestroyWindow().

  • 2
    \$\begingroup\$ Can you rig up a test to pass DestroyWindow a junk handle and then read GetLastError? That might let you know how safe it is. \$\endgroup\$
    – Mike
    Sep 11, 2014 at 19:56

1 Answer 1


If the control doesn't exist then GetDlgItem returns NULL. Passing NULL to DestroyWindow should be "save" in the sense that it won't crash - it will return FALSE and GetLastError will be returning an "Invalid Handle" error (or something like that) (can't find any docs for it but that's the behaviour I've seen in the past).

A remark to your code:

hwnd = GetDlgItem(hWnd, IDC_BTN_FOOA);

The use of two different variables which only differ in the capitalization of a single letter is ugly and error prone and you should use a different name. One convention I tend to follow if I select objects which are to be destroyed is to call them doomed. So maybe something like this:

doomedHandle = GetDlgItem(hWnd, IDC_BTN_FOOA);
  • \$\begingroup\$ Alright, the main code is updated, and works. There is one questionable thing left, and that is: does GetLastError() reset when you call it? \$\endgroup\$ Sep 12, 2014 at 17:58
  • 1
    \$\begingroup\$ @EvanCarslake: I'm pretty sure GetLastError() doesn't reset the error code. To my knowledge the error code is only change when SetLastError() is called. \$\endgroup\$
    – ChrisWue
    Sep 13, 2014 at 2:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.