DestroyWindowRemarks
A thread cannot use DestroyWindow to destroy a window created by a different thread.
If the window being destroyed is a child window that does not have the WS_EX_NOPARENTNOTIFY style, aWM_PARENTNOTIFY message is sent to the parent.
有些視窗是寫在 dll 裡的,建立和銷毀都有對外介面。上面的是 MSDN 的一些描述。說的就是建立和銷毀不在同一個線程裡會出現問題。問題:前些時候自己調整了VC編譯器的配置,全部編譯都不最佳化,產生相應的 pdb 檔案,這種情況如果出現問題的話,就很容易暴露。別人的代碼,建立視窗是在 dll 外的線程裡進行,但是在響應菜單的時候,卻在原來視窗裡進行 DestroyWindow。在 windows xp 的機器上沒有問題,但是在 win7 下就沒有任何提示退出了。還好問題能重現,用 windbg 綁定進程進行跟蹤,進程崩潰了就看堆棧,每次定位的堆棧都不一樣,這種情況很大的可能性是記憶體錯亂,或者訊息發給已經銷毀的視窗處理,或者代碼調用已經銷毀的視窗的某些介面導致的不確定性報錯。結果:用 windbg 無法定位問題,只好走一下原理代碼的邏輯了,後來發現 DestroyWindow 的代碼調用多次而且邏輯有錯誤,查一下 MSDN 就明白問題在哪裡了。將 DestroyWindow 改為 PostMessage(WM_CLOSE) 就解決問題了。Remarks
An application can prompt the user for confirmation, prior to destroying a window, by processing the
WM_CLOSE message and calling the DestroyWindow function only if the user confirms the choice.
By default, the DefWindowProc function calls the
DestroyWindow function to destroy the window.