h() 3{ 4 T szExePath[MAX_PATH]; 5 select_variable::value>(GetModuleFileNameA,GetModuleFileNameW)(NULL,szExePath); 6 return szExePath; 7} (3)内部实现类型化,比如计算某一目录或文件的大小,因内部用到了FirstFirstFile和FirstNextFile,而这两个API不仅路径,而且WIN32_FIND_DATA结构体都有A和W版本,因此需要选择定义正确的结构体变量和调用正确的API函数,如下所示:
1template 2inline ULONGLONG GetDirSize(const charT* path) 3{ 4 int ret = IsDirectoryOrFile(path); 5 if (0==ret) return 0L; 6 7 std::basic_string strPath = path; 8 if (1==ret) 9 { 10 if (strPath.length() - 1 != strPath.rfind((charT)'\\')) 11 strPath += (charT)'\\'; 12 strPath += select_variable::value>("*.*",L"*.*"); 13 } 14 ULONGLONG ullSumSize = 0; 15 typename select_type::value,WIN32_FIND_DATAA,WIN32_FIND_DATAW>::type findData; 16 HANDLE hFindFile = select_variable::value>(FindFirstFileA,FindFirstFileW)(strPath.c_str(), &findData); 17 18 for(BOOL bResult = (hFindFile != INVALID_HANDLE_VALUE); 19 bResult; bResult = select_variable::value>(FindNextFileA,FindNextFileW)(hFindFile, &findData)) 20 { 21 if(findData.cFileName[0] == (charT)'.') 22 continue; 23 if(findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) 24 { 25 strPath = strPath.substr(0,strPath.rfind((charT)'\\')+1)+findData.cFileName; 26 ullSumSize += GetDirSize(strPath.c_str(), bExitCalc); 27 } 28 else 29 ullSumSize += (((ULONGLONG)findData.nFileSizeHigh) << 32) + findData.nFileSizeLow; 30 } 31 ::FindClose(hFindFile); 32 return ullSumSize; 33}
摘自 天道酬勤
|