天天看點

轉:VS2010和VS2008 KB2465361之後,MFC靜态連結 EXE體積變大問題

原文連結:http://tedwvc.wordpress.com/2011/04/16/static-mfc-code-bloat-problem-from-vc2010-is-now-in-vc2008-sp1security-fix/

大意是:

在KB2465361之後。themehelper.cpp 中調用了afxglobals.cpp中的 AfxLoadSystemLibraryUsingFullPath函數。造成afxglobals.obj 的引入。進而産生代碼至少增加1.2M。

解決方法

1.解除安裝KB2465361,這個方法太弱智了。不小心也許又打上了。

2.在你自己的工程的stdafx.cpp中增加以下代碼

// this is our own local copy of the AfxLoadSystemLibraryUsingFullPath function

HMODULE AfxLoadSystemLibraryUsingFullPath(const WCHAR *pszLibrary)

{

WCHAR wszLoadPath[MAX_PATH+1];

if (::GetSystemDirectoryW(wszLoadPath, _countof(wszLoadPath)) == 0)

{

return NULL;

}

if (wszLoadPath[wcslen(wszLoadPath)-1] != L'//')

{

if (wcscat_s(wszLoadPath, _countof(wszLoadPath), L"//") != 0)

{

return NULL;

}

}

if (wcscat_s(wszLoadPath, _countof(wszLoadPath), pszLibrary) != 0)

{

return NULL;

}

return(::AfxCtxLoadLibraryW(wszLoadPath));

}

其實這段代碼與afxglobals.cpp中的代碼一樣。目的隻是避免引入afxglobals.obj。

這樣,你的程式體積又恢複正常了。減肥成功。。。恭喜恭喜。

微軟在KB2538241之後已經修複了這個問題。如果以上代碼,打了KB2538241之後,會出現link error AfxLoadSystemLibraryUsingFullPath.符号重定義,去掉上述代碼即可。

繼續閱讀