Windows中有一个Shlwapi.dll文件,包含了大量的Windows字符串处理方法,这些方法,在通常的程序应用中,经常会用到,有一部分处理方法,在CRuntime中也存在,但不方便使用。有一部分函数,也有相关的Windows API函数,如StrCpy对应的lstrcpy之类的字符串处理函数。从Shlwapi名称看,这个应该是Windows的Shell API函数。
不管怎么说,Shlwapi.dll提供了大量处理Windows字符串的方法,这些方法,可以让你工作更加简便,通常,我们需要获取一个文件名的后缀名,使用MFC的CString查找字符串可以获取,但是麻烦,每个人的实现都不一样,用Shlwapi中提供的函数PathFindExtension直接就返回一个后缀名的指针。
Shlwapi主要分为三类函数,第一类是字符串处理函数,通常以Str开头,第二类是路径或者文件夹处理函数,通常以Path开头,第三类是注册表处理函数,通常以SH开头。每一类函数,都要区分A和W。
常见的字符串处理函数有:
StrChr ,StrChrI,StrCmpN,StrCmpNI,StrCSpn,StrCSpnI,StrDup,StrFormatByteSize(字节大小格式化),StrFromTimeInterval,StrIsIntlEqual,StrNCat,StrPBrk,StrRChr, StrRChrI,StrRStrI,StrSpn,StrStr,StrStrI,StrToInt,StrToIntEx(可以讲16进制字符串转换为10进制值),StrTrim(删除收尾空字符串),StrCat,StrCmp,StrCpy,ChrCmpI,
常见的Path处理函数有:
PathAddBackslash,PathAddExtension,PathAppend,PathBuildRoot, PathCanonicalize,PathCombine,PathCompactPath,PathCompactPathEx,PathCommonPrefix,PathFileExists,PathFindExtension,PathFindFileName(类似与basename中后面部分,获取得到整个文件名), PathFindNextComponent(获取得到除盘符外的所有文件名),PathFindOnPath,PathGetArgs,PathGetCharType,PathGetDriveNumber,PathIsDirectory,PathIsFileSpec(仅有文件名部分,不能够包括:和\符号),PathIsPrefix,PathIsRelative,PathIsRoot,PathIsSameRoot,PathIsUNC,PathIsUNCServer,PathIsUNCServerShare,PathIsContentType,PathIsURL,
PathMakePretty,PathMatchSpec,PathParseIconLocation,PathQuoteSpaces,PathRelativePathTo,PathRemoveArgs,PathRemoveBackslash,PathRemoveBlanks,
PathRemoveExtension,PathRemoveFileSpec,PathRenameExtension,PathSearchAndQualify,PathSetDlgItemPath,PathSkipRoot,PathStripPath,
PathStripToRoot,PathUnquoteSpaces,PathMakeSystemFolder,PathUnmakeSystemFolder,PathIsSystemFolder
常见的注册表处理函数有:
SHDeleteEmptyKey,SHDeleteKey, SHDeleteValue,SHGetValue,SHSetValue,SHQueryValueEx, SHEnumKeyEx,SHEnumValue,SHQueryInfoKey,
SHRegCreateUSKey,SHRegOpenUSKey,SHRegQueryUSValue,SHRegWriteUSValue,SHRegDeleteUSValue,SHRegDeleteEmptyUSKey,SHRegEnumUSKey,
SHRegEnumUSValue,SHRegQueryInfoUSKey,SHRegGetUSValue,SHRegSetUSValue,SHRegGetBoolUSValue,SHOpenRegStream,
其他函数
创建GDI帮助函数
SHCreateShellPalette
DLLGETVERSIONPROC指针,用来获取dll的版本号
和DllInstall原型定义
STDAPI DllInstall(BOOL bInstall, LPCWSTR pszCmdLine);
/*
Copyright (C) =USTC= Fu Li
Author : Fu Li
Create : 2008-6-27
Home : http://www.phoxo.com
Mail : crazybitwps@hotmail.com
This file is part of UIStone
The code distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Redistribution and use the source code, with or without modification,
must retain the above copyright.
*/
#pragma once
#include <shlwapi.h>
#pragma comment (lib, "shlwapi.lib")
//-------------------------------------------------------------------------------------
/**
File helper.
*/
class FCFileEx
{
public:
/**
Get folder that module locate, with backslash('\') append \n
hModule - NULL to get current process folder.
*/
static CString GetModuleFolder (HMODULE hModule=NULL)
{
TCHAR s[MAX_PATH] = {0} ;
GetModuleFileName (hModule, s, MAX_PATH) ;
PathRemoveFileSpec (s) ;
PathAddBackslash (s) ;
return s ;
}
/// Get folder that file locate, with backslash('\') append.
static CString GetFileFolder (LPCTSTR strFilename)
{
TCHAR s[MAX_PATH] = {0} ;
lstrcpyn (s, strFilename, MAX_PATH) ;
PathRemoveFileSpec (s) ;
PathAddBackslash (s) ;
return s ;
}
/// Get file's size.
static INT64 GetSize (LPCTSTR strFilename)
{
WIN32_FILE_ATTRIBUTE_DATA fd ;
if (GetFileAttributesEx (strFilename, GetFileExInfoStandard, &fd))
{
LARGE_INTEGER n ;
n.LowPart = fd.nFileSizeLow ;
n.HighPart = (LONG)fd.nFileSizeHigh ;
return n.QuadPart ;
}
return 0 ;
}
/// Get file's extension name, not including leading period.
static CString GetExtName (LPCTSTR strFilename)
{
CString s(strFilename) ;
int n = s.ReverseFind('.') ;
if (n != -1)
{
return s.Mid(n+1) ;
}
return _T("") ;
}
/// Read file to memory.
static BOOL Read (LPCTSTR strFilename, std::vector<BYTE>& file_data)
{
file_data.clear() ;
BOOL b = FALSE ;
HANDLE f = CreateFile (strFilename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL) ;
if (f != INVALID_HANDLE_VALUE)
{
DWORD nLength = ::GetFileSize(f, NULL) ;
if (nLength)
{
file_data.resize (nLength) ;
DWORD dwRead ;
::ReadFile (f, &file_data[0], nLength, &dwRead, NULL) ;
b = (dwRead == nLength) ;
}
CloseHandle (f) ;
}
assert(b && file_data.size()) ;
return b ;
}
/// Write buffer to file, if file already exist, it will be delete before write.
static BOOL Write (LPCTSTR strFilename, const void* p, int nLength)
{
SetFileAttributes (strFilename, FILE_ATTRIBUTE_NORMAL) ;
BOOL b = FALSE ;
HANDLE f = CreateFile (strFilename, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL) ;
if (f != INVALID_HANDLE_VALUE)
{
DWORD nWrite ;
::WriteFile (f, p, nLength, &nWrite, NULL) ;
b = ((int)nWrite == nLength) ;
CloseHandle (f) ;
}
assert(b) ;
return b ;
}
/// Get a temp file path.
static CString QueryTempFilePath()
{
TCHAR szTmpPath[MAX_PATH],
szFilename[MAX_PATH] ;
::GetTempPath (MAX_PATH, szTmpPath) ;
::GetTempFileName (szTmpPath, _T("t"), 0, szFilename) ;
return szFilename ;
}
/// @name Read/Write INI file.
//@{
/// Read string key from ini file, return FALSE if key doesn't exist.
static BOOL INIRead (LPCTSTR strFilename, LPCTSTR strKey, CString& s, LPCTSTR strSection=_T("app"))
{
TCHAR b[512] ;
DWORD dwWrite = GetPrivateProfileString (strSection, strKey, _T("\n"), b, 512, strFilename) ;
if ((b[0] == '\n') && (b[1] == 0))
return FALSE ;
if (dwWrite > (512 - 8))
{
std::vector<TCHAR> buf (4096) ;
GetPrivateProfileString (strSection, strKey, _T("\n"), &buf[0], (DWORD)buf.size(), strFilename) ;
s = &buf[0] ;
}
else
{
s = b ;
}
return TRUE ;
}
/// Read int key from ini file, return FALSE if key doesn't exist.
static BOOL INIRead (LPCTSTR strFilename, LPCTSTR strKey, INT64& n, LPCTSTR strSection=_T("app"))
{
TCHAR b[32] ;
GetPrivateProfileString (strSection, strKey, _T("\n"), b, 32, strFilename) ;
if ((b[0] == '\n') && (b[1] == 0))
return FALSE ;
n = _ttoi64(b) ;
return TRUE ;
}
/// Write a int key to ini file.
static BOOL INIWrite (LPCTSTR strFilename, LPCTSTR strKey, INT64 n, LPCTSTR strSection=_T("app"))
{
CString s ;
s.Format(_T("%I64d"), n) ;
return INIWrite (strFilename, strKey, s, strSection) ;
}
/// Write a string key to ini file.
static BOOL INIWrite (LPCTSTR strFilename, LPCTSTR strKey, LPCTSTR strValue, LPCTSTR strSection=_T("app"))
{
return WritePrivateProfileString (strSection, strKey, strValue, strFilename) ;
}
//@}
};