转载请标明是引用于 http://blog.csdn.net/chenyujing1234
相关代码:
http://www.rayfile.com/zh-cn/files/f19ea46e-77e3-11e1-b980-0015c55db73d/
安装WTL请参考
http://blog.csdn.net/chenyujing1234/article/details/7399014
WTL环境优化:
修改AppWiz文件夹下setup90x.js 。把第152行 fileDest.WriteLine("Param=\"VC_EXPRESS = 1\""); 这句删除。
WTL向导认为如果是Express版本的VC一定是和psdk2003是的atl配合的,具体和atlthunk相关。如果不删除这句,用向导生成程序是运行不了的
然后在AppWiz\Files\Templates\1033文件夹找到stdafx.h 在32行添加#pragma comment(lib,”atlthunk.lib”)。这样用向导生成的程序就不会有链接错误了。
工程名为WTLTest。
选择Generate .cpp Files是为了产生cpp文件,这样更符合我们的习惯,不然只有.h文件产生.
调试过程后得知,WTL的调用如下:
1、_tWinMain入口
int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE /*hPrevInstance*/, LPTSTR lpstrCmdLine, int nCmdShow) { HRESULT hRes = ::CoInitialize(NULL); // If you are running on NT 4.0 or higher you can use the following call instead to // make the EXE free threaded. This means that calls come in on a random RPC thread. // HRESULT hRes = ::CoInitializeEx(NULL, COINIT_MULTITHREADED); ATLASSERT(SUCCEEDED(hRes));
// this resolves ATL window thunking problem when Microsoft Layer for Unicode (MSLU) is used ::DefWindowProc(NULL, 0, 0, 0L);
AtlInitCommonControls(ICC_BAR_CLASSES); // add flags to support other controls
hRes = _Module.Init(NULL, hInstance); // 初始化 ATLASSERT(SUCCEEDED(hRes));
int nRet = Run(lpstrCmdLine, nCmdShow);
_Module.Term(); // 结束 ::CoUninitialize();
return nRet; }
2、采用ATL初始化模块.
CAppModule _Module;
_Module.Init(
// Overrides of CComModule::Init and Term HRESULT Init(ATL::_ATL_OBJMAP_ENTRY* pObjMap, HINSTANCE hInstance, const GUID* pLibID = NULL) { HRESULT hRet = CComModule::Init(pObjMap, hInstance, pLibID); if(FAILED(hRet)) return hRet; m_dwMainThreadID = ::GetCurrentThreadId(); typedef ATL::CSimpleMap<DWORD, CMessageLoop*> _mapClass; m_pMsgLoopMap = NULL; ATLTRY(m_pMsgLoopMap = new _mapClass); if(m_pMsgLoopMap == NULL) return E_OUTOFMEMORY; m_pSettingChangeNotify = NULL; return hRet; }
结束时是_Module.Term();
void Term() { TermSettingChangeNotify(); delete m_pMsgLoopMap; CComModule::Term(); }
3、创建ATL窗口
int Run(LPTSTR /*lpstrCmdLine*/ = NULL, int nCmdShow = SW_SHOWDEFAULT) { CMessageLoop theLoop; _Module.AddMessageLoop(&theLoop); CMainDlg dlgMain; if(dlgMain.Create(NULL) == NULL) { ATLTRACE(_T("Main dialog creation failed!\n")); return 0; } dlgMain.ShowWindow(nCmdShow); int nRet = theLoop.Run(); _Module.RemoveMessageLoop(); return nRet; }