3
\$\begingroup\$

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);
DestroyWindow(hwnd);  
hwnd = GetDlgItem(hWnd, IDC_BTN_FOOB);
DestroyWindow(hwnd);  
hwnd = GetDlgItem(hWnd, IDC_BTN_FOOC);

// add the control wanted
hwndButton = CreateWindow("BUTTON", "Start", WS_VISIBLE|WS_CHILD|BS_OWNERDRAW|BS_PUSHBUTTON, 
        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) { 
    DestroyWindow(hwnd); 
}
else { 
    errorResult = GetLastError(); 
}

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

\$\endgroup\$
  • 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 '14 at 19:56
1
\$\begingroup\$

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);
DestroyWindow(hwnd);  

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);
DestroyWindow(doomedHandle);
\$\endgroup\$
  • \$\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\$ – Evan Carslake Sep 12 '14 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 '14 at 2:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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