AFX_MANAGE_STATE(AfxGetStaticModuleState())的作用:用於模組切換時的狀態保護,
1.AfxGetStaticModuleState()指向當前模組狀態;
2.當前函數調用結束後原模組的狀態自動被恢複;
3.用於DLL中所調用MFC函數、類、資源時的模組狀態切換;
[MSDN]
By default, MFC uses the resource handle of the main application to load the resource template. If you have an exported function in a DLL, such as one that launches a dialog box in the DLL, this template is actually stored in the DLL module. You need to switch the module state for the correct handle to be used. You can do this by adding the following code to the beginning of the function:
AFX_MANAGE_STATE(AfxGetStaticModuleState( ));
This swaps the current module state with the state returned from AfxGetStaticModuleState until the end of the current scope
也就是說,並不是每一個dll的輸出函數前都要調用它,只有在要輸出對話框等用到資源時要調用!
dll中資源是共用的用了這個函數的防止不同的進程修改資源產生錯誤!
預設情況下MFC使用主應用程式的資源控制代碼來載入資源模板,而DLL中的資源模板是存在於DLL模板中,因此要使用這一語句切換到