📄 tracetool.cpp
字号:
// We are only interested in directories, since at this level, that should
// be the only thing in this directory, i.e, LCID sub dirs
if (wfdw.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
// Skip current and previous dirs, "." and ".."
if (!_tcscmp(wfdw.cFileName, _T(".")) || !_tcscmp(wfdw.cFileName, _T("..")))
continue;
// Does this dir have a copy of the dll?
szPathTemp[nPathEnd] = L'\0';
_tcsncat_s(szPathTemp,_countof(szPathTemp), wfdw.cFileName, PATHLEFT(szPathTemp));
_tcsncat_s(szPathTemp,_countof(szPathTemp), _T("\\"), PATHLEFT(szPathTemp));
_tcsncat_s(szPathTemp,_countof(szPathTemp), szDllName, PATHLEFT(szPathTemp));
if (GetFileAttributes(szPathTemp) != 0xFFFFFFFF)
{
// Got it!
lcidFound = (LCID)_tstol(wfdw.cFileName);
hr = S_OK;
break;
}
}
}
FindClose(hDirs);
}
}
Done:
if (SUCCEEDED(hr))
{
// Set the default LCID
if (plcidOut)
{
if (lcidFound == (LCID)-1)
{
return E_UNEXPECTED;
}
*plcidOut = lcidFound;
}
// Finally, attempt to load the library
// Beware! A dll loaded with LOAD_LIBRARY_AS_DATAFILE won't
// let you use LoadIcon and things like that (only general calls like
// FindResource and LoadResource).
if (phinstOut != NULL)
{
*phinstOut = LoadLibraryEx(szPathTemp, NULL, dwExFlags);
hr = (*phinstOut) ? S_OK : E_FAIL;
}
if ( szFullPathOut )
{
_tcsncpy_s(szFullPathOut,sizeInCharacters, szPathTemp, _MAX_PATH-1);
}
}
return hr;
}
//////////////////////////////////////////////////////////////////////////
//Purpose: Iterates env("PATH") directories to try to find (using LoadUILibrary)
// resource dll a directory below PATH dirs. Ex: if PATH="c:\bin;d:\win"
// and szDllName="ToolUI.dll", then the first of c:\bin\1033\ToolUI.dll
// and d:\win\SomeFolder\ToolUI.dll will be loaded.
// See LoadLocResDll doc (below) for example.
//Input: szDllName - the string resource dll name to search. Ex: ToolUI.dll
//Output: TCHAR *szPathOut - filled with absolute path to dll, if found.
// size_t sizeInCharacters - buffer size in characters
//Returns: Success - HMODULE of found dll, Failure - NULL
//////////////////////////////////////////////////////////////////////////
HMODULE LoadSearchPath(LPCTSTR szDllName,TCHAR *szPathOut, size_t sizeInCharacters)
{
TCHAR * szEnvPATH = NULL;
TCHAR * szEnvPATHBuff = NULL;
int nPathLen = 0;
int nPathIndex = 0;
HMODULE hmod = NULL;
if (DuplicateEnvString(&szEnvPATHBuff,NULL,_T("PATH"))==0 && (szEnvPATH=szEnvPATHBuff) != NULL)
{
while (*szEnvPATH)
{
/* skip leading white space and nop semicolons */
for (; *szEnvPATH == L' ' || *szEnvPATH == L';'; ++szEnvPATH)
{} /* NOTHING */
if (*szEnvPATH == L'\0')
{
break;
}
++nPathIndex;
/* copy this chunk of the path into our trypath */
nPathLen = 0;
TCHAR szPath[_MAX_PATH+1];
TCHAR * pszTry = NULL;
for (pszTry = szPath; *szEnvPATH != L'\0' && *szEnvPATH != L';'; ++szEnvPATH)
{
++nPathLen;
if (nPathLen < _MAX_PATH)
{
*pszTry++ = *szEnvPATH;
} else
{
break;
}
}
*pszTry = L'\0';
if (nPathLen == 0 || nPathLen >= _MAX_PATH)
{
continue;
}
LoadUILibrary(szPath, szDllName, LOAD_LIBRARY_AS_DATAFILE,
&hmod, szPathOut,sizeInCharacters, NULL);
if ( hmod )
{
break;
}
}
}
if (szEnvPATHBuff!=NULL)
{
delete [] szEnvPATHBuff;
}
return hmod;
}
//Example: Say PATH="c:\bin;d:\win", resource dll name (szDllName) is "ToolUI.dll",
// user locale is 936, and the .exe calling LoadLocResDll is c:\MyTools\Tool.exe
// Search order:
// a) c:\MyTools\936\ToolUI.dll (exe path + user default UI lang)
// b) c:\MyTools\1033 (same with eng)
// c) c:\MyTools\*\ToolUI.dll (where * is sub folder).
// d) c:\bin\936\ToolUI.dll (first in path)
// e) c:\bin\1033\ToolUI.dll (first in path + eng)
// f) c:\bin\*\ToolUI.dll
// g) d:\win\936\ToolUI.dll (second in path)
// h) d:\win\1033\ToolUI.dll (second in path + eng)
// i) d:\win\*\ToolUI.dll (second in path + eng)
// j) if bExeDefaultModule and not found, return exe HINSTANCE.
// Note: The primary lang (without the sublang) is tested after the user ui lang.
// Main Input: szDllName - the name of the resource dll <ToolName>ui.dll. Ex: vcdeployUI.dll
// Main Output: HMODULE of resource dll or NULL - if not found (see bExeDefaultModule).
HMODULE LoadLocResDll(LPCTSTR szDllName,BOOL bExeDefaultModule=TRUE,DWORD dwExFlags=LOAD_LIBRARY_AS_DATAFILE,LPTSTR pszPathOut = NULL,size_t sizeInCharacters = 0 )
{
HMODULE hmod = NULL;
TCHAR driverpath[_MAX_PATH + 1], exepath[_MAX_PATH + 1];
LPTSTR p = NULL;
GetModuleFileName(GetModuleHandle(NULL), driverpath, _MAX_PATH);
// find path of tool
ATLENSURE(AtlIsValidString(driverpath, _MAX_PATH-1));
p = driverpath + _TCSNLEN(driverpath, _MAX_PATH-1)-1;
while ( *p != L'\\' && p != driverpath)
{
p--;
}
*p = '\0';
LoadUILibrary(driverpath, szDllName, dwExFlags,
&hmod, exepath,_countof(exepath), NULL);
if ( hmod == NULL )
{
// search PATH\<lcid> for <ToolName>ui.dll
hmod = LoadSearchPath(szDllName,exepath,_countof(exepath));
}
if ( hmod && pszPathOut )
{
_tcsncpy_s(pszPathOut,sizeInCharacters, exepath, _MAX_PATH-1);
}
//Not found dll, return the exe HINSTANCE as a fallback.
if (hmod == NULL && bExeDefaultModule)
{
hmod=GetModuleHandle(NULL);
}
return hmod;
}
//End loc routines
////////////////////////////////////////////////////////////////////
const TCHAR* szTraceToolUIDll = _T("AtlTraceToolUI.dll");
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
// CTraceToolApp
BEGIN_MESSAGE_MAP(CTraceToolApp, CWinApp)
ON_COMMAND(ID_HELP, CWinApp::OnHelp)
END_MESSAGE_MAP()
// CTraceToolApp construction
CTraceToolApp::CTraceToolApp()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance
EnableHtmlHelp();
}
// The one and only CTraceToolApp object
CTraceToolApp theApp;
// CTraceToolApp initialization
static const LPCTSTR g_pszEventName = _T( "ATLTraceTool_Instance" );
BOOL CTraceToolApp::InitInstance()
{
// Check to see if another instance of the tool is running
BOOL bCreated = m_hSingleInstanceEvent.Create( NULL, FALSE, FALSE, g_pszEventName );
if( !bCreated )
{
return( FALSE );
}
HINSTANCE hInstRes=LoadLocResDll(szTraceToolUIDll,TRUE,0); //Do not load resource dll as data file
AfxSetResourceHandle(hInstRes);
if( ::GetLastError() == ERROR_ALREADY_EXISTS )
{
m_hSingleInstanceEvent.Set(); // Notify the original instance
return( FALSE );
}
CWinApp::InitInstance();
CTraceToolDlg dlg;
m_pMainWnd = &dlg;
INT_PTR nResponse = dlg.DoModal();
if (nResponse == IDOK)
{
// TODO: Place code here to handle when the dialog is
// dismissed with OK
}
else if (nResponse == IDCANCEL)
{
// TODO: Place code here to handle when the dialog is
// dismissed with Cancel
}
// Since the dialog has been closed, return FALSE so that we exit the
// application, rather than start the application's message pump.
return FALSE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -