解析从INI配置文件读取到操作命令
2009年2月23日星期一
由于工作中遇到文件更新到开发,自然涉及到从配置文件中读取操作命令,以便中更新过程中进行操作。下面以工作中的Demo为示例:
[Setting]
Version=1.2.3.0
[Files]
umini_console.exe={desktop}
install_driver.exe={thisdir}
umini_ocx.ocx={thisdir}/..
umini_info.info={system32}/TS_Info
[ AfterUpdate]
RegFile={thisdir}/../umini_ocx.ocx
Execute={thisdir}/ install_driver.exe –i
DeleteFile={thisdir}/ install_driver.exe
RegEdit=HKEY_LOCAL_MACHINE;SOFTWARE/UMINI/ KeyLogon;Rules;REG_DWORD;1
这里我不讲怎样读取配置文件,主要说下怎么样通过从配置文件中读取到到参数进行解析。主要涉及两个参数:KeyName和KeyValue,KeyName可以取值RegFile/Excute/DeleteFile…,KeyValue可以取值{thisdir}/../umini_ocx.ocx。具体不再讲解,下面给出详细源码:
BOOL ExcuteINIOrder(LPCTSTR szKeyName, LPCTSTR szKeyValue)
{
// Explain the order
CString strValue,strKeyValue(szKeyValue);
strKeyValue.Remove(' ');
TCHAR szTemp[MAX_PATH];
if (0 == strKeyValue.Find(_T("{thisdir}")))
{
// {thisdir}
CString strDir(_T("{thisdir}"));
strValue = strKeyValue.Right(strKeyValue.GetLength() - strDir.GetLength());
// Get Current dir
strValue = _T(".") + strValue;
}
else if (0 == strKeyValue.Find(_T("{desktop}")))
{
// {desktop}
CString strDir(_T("{desktop}"));
strValue = strKeyValue.Right(strKeyValue.GetLength() - strDir.GetLength());
// Get Desktop
::SHGetSpecialFolderPath(NULL,szTemp,CSIDL_COMMON_DESKTOPDIRECTORY,FALSE);
CString strTemp(szTemp);
strValue = strTemp + _T("//") + strValue;
}
else if (0 == strKeyValue.Find(_T("{system32}")))
{
// {system32}
CString strDir(_T("{system32}"));
strValue = strKeyValue.Right(strKeyValue.GetLength() - strDir.GetLength());
// Get System32
::SHGetSpecialFolderPath(NULL,szTemp,CSIDL_SYSTEM,FALSE);
CString strTemp(szTemp);
strValue = strTemp + _T("//") + strValue;
}
else if (0 == strKeyValue.Find(_T("{sysdriver}")))
{
// {sysdriver}
CString strDir(_T("{sysdriver}"));
strValue = strKeyValue.Right(strKeyValue.GetLength() - strDir.GetLength());
// Get Sysdriver
::SHGetSpecialFolderPath(NULL,szTemp,CSIDL_SYSTEM,FALSE);
CString strTemp(szTemp);
strValue = strTemp + _T("//") + strValue;
}
else if (0 == strKeyValue.Find(_T("{startup}")))
{
// {startup}
CString strDir(_T("{startup}"));
strValue = strKeyValue.Right(strKeyValue.GetLength() - strDir.GetLength());
// Get Startup
::SHGetSpecialFolderPath(NULL,szTemp,CSIDL_STARTUP,FALSE);
CString strTemp(szTemp);
strValue = strTemp + _T("//") + strValue;
}
// Excute the order
if (0 == _tcscmp(szKeyName,_T("Execute")))
{
// Excute
SHELLEXECUTEINFO ShExecInfo = {0};
ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
ShExecInfo.hwnd = NULL;
ShExecInfo.lpVerb = NULL;
ShExecInfo.lpFile = strValue;
ShExecInfo.lpParameters = _T("");
ShExecInfo.lpDirectory = _T("");
ShExecInfo.nShow = SW_SHOW;
ShExecInfo.hInstApp = NULL;
ShellExecuteEx(&ShExecInfo);
DWORD dw = GetLastError();
WaitForSingleObject(ShExecInfo.hProcess,INFINITE);
}
else if (0 == _tcscmp(szKeyName,_T("DeleteFile")))
{
// DeleteFile
::DeleteFile(strValue);
}
else if (0 == _tcscmp(szKeyName,_T("RegFile")))
{
// RegFile
RegisterOcx(strValue);
}
else if (0 == _tcscmp(szKeyName,_T("UnRegFile")))
{
// UnRegFile
UnRegisterOcx(strValue);
}
else if (0 == _tcscmp(szKeyName,_T("RegEdit")))
{
// RegEdit
CString strKey, strSubKey, strSubKeyName, strKeyType, strSubKeyValue, strTmp;
int i = strKeyValue.FindOneOf(_T(";"));
strKey = strKeyValue.Left(i);
strTmp = strKeyValue.Right(strKeyValue.GetLength() - i - 1);
i = strTmp.FindOneOf(_T(";"));
strSubKey = strTmp.Left(i);
strTmp = strTmp.Right(strTmp.GetLength() - i - 1);
i = strTmp.FindOneOf(_T(";"));
strSubKeyName = strTmp.Left(i);
strTmp = strTmp.Right(strTmp.GetLength() - i - 1);
i = strTmp.FindOneOf(_T(";"));
strKeyType = strTmp.Left(i);
strSubKeyValue = strTmp.Right(strTmp.GetLength() - i - 1);
HKEY hKey;
if (0 == _tcscmp(strKey.GetBuffer(),_T("HKEY_CLASSES_ROOT")))
{
strKey.ReleaseBuffer();
hKey = HKEY_CLASSES_ROOT;
}
else if (0 == _tcscmp(strKey.GetBuffer(),_T("HKEY_CURRENT_CONFIG")))
{
strKey.ReleaseBuffer();
hKey = HKEY_CURRENT_CONFIG;
}
else if (0 == _tcscmp(strKey.GetBuffer(),_T("HKEY_CURRENT_USER")))
{
strKey.ReleaseBuffer();
hKey = HKEY_CURRENT_USER;
}
else if (0 == _tcscmp(strKey.GetBuffer(),_T("HKEY_LOCAL_MACHINE")))
{
strKey.ReleaseBuffer();
hKey = HKEY_LOCAL_MACHINE;
}
else if (0 == _tcscmp(strKey.GetBuffer(),_T("HKEY_USERS")))
{
strKey.ReleaseBuffer();
hKey = HKEY_USERS;
}
HKEY hRetKey;
if (ERROR_SUCCESS != RegCreateKey(hKey,strSubKey.GetBuffer(),&hRetKey))
{
strSubKey.ReleaseBuffer();
return FALSE;
}
strSubKey.ReleaseBuffer();
// Edit
int nSubKeyValue = _ttoi(strSubKeyValue.GetBuffer());
strSubKeyValue.ReleaseBuffer();
if (ERROR_SUCCESS == RegSetValueEx(hRetKey,strSubKeyName.GetBuffer(),NULL,REG_DWORD,
(const BYTE *)&nSubKeyValue,sizeof(nSubKeyValue)))
{
strSubKeyName.ReleaseBuffer();
RegCloseKey(hRetKey);
return TRUE;
}
else
{
strSubKeyName.ReleaseBuffer();
RegCloseKey(hRetKey);
return FALSE;
}
}
else if (0 == _tcscmp(szKeyName,_T("RegDel")))
{
// RegDel
CString strKey, strSubKey, strTmp;
int i = strKeyValue.FindOneOf(_T(";"));
strKey = strKeyValue.Left(i);
strTmp = strKeyValue.Right(strKeyValue.GetLength() - i - 1);
int j = strTmp.FindOneOf(_T(";"));
strSubKey = strTmp.Left(j);
HKEY hKey;
if (0 == _tcscmp(strKey.GetBuffer(),_T("HKEY_CLASSES_ROOT")))
{
strKey.ReleaseBuffer();
hKey = HKEY_CLASSES_ROOT;
}
else if (0 == _tcscmp(strKey.GetBuffer(),_T("HKEY_CURRENT_CONFIG")))
{
strKey.ReleaseBuffer();
hKey = HKEY_CURRENT_CONFIG;
}
else if (0 == _tcscmp(strKey.GetBuffer(),_T("HKEY_CURRENT_USER")))
{
strKey.ReleaseBuffer();
hKey = HKEY_CURRENT_USER;
}
else if (0 == _tcscmp(strKey.GetBuffer(),_T("HKEY_LOCAL_MACHINE")))
{
strKey.ReleaseBuffer();
hKey = HKEY_LOCAL_MACHINE;
}
else if (0 == _tcscmp(strKey.GetBuffer(),_T("HKEY_USERS")))
{
strKey.ReleaseBuffer();
hKey = HKEY_USERS;
}
if (ERROR_SUCCESS == SHDeleteKey(hKey,strSubKey.GetBuffer()))
{
strSubKey.ReleaseBuffer();
return TRUE;
}
else
{
strSubKey.ReleaseBuffer();
return FALSE;
}
}
return FALSE;
}
其中用到到两个函数:RegisterOcx和UnRegisterOcx来自网上(详细网址已经记不清了),下面也给出源码:
BOOL RegisterOcx(LPCTSTR OcxFileName)
{
LPCTSTR pszDllName = OcxFileName ; //ActiveX控件的路径及文件名
HINSTANCE hLib = LoadLibrary(pszDllName); //装载ActiveX控件
if (hLib < (HINSTANCE)HINSTANCE_ERROR)
{
return FALSE ;
}
FARPROC lpDllEntryPoint;
lpDllEntryPoint = GetProcAddress(hLib,"DllRegisterServer"); //获取注册函数DllRegisterServer地址
if(lpDllEntryPoint!=NULL) //调用注册函数DllRegisterServer
{
if(FAILED((*lpDllEntryPoint)()))
{
FreeLibrary(hLib);
return FALSE ;
}
return TRUE ;
}
else
return FALSE ;
}
BOOL UnRegisterOcx(LPCTSTR OcxFileName)
{
LPCTSTR pszDllName = OcxFileName ; //ActiveX控件的路径及文件名
HINSTANCE hLib = LoadLibrary(pszDllName); //装载ActiveX控件
if (hLib < (HINSTANCE)HINSTANCE_ERROR)
{
return FALSE ;
}
FARPROC lpDllEntryPoint;
lpDllEntryPoint = GetProcAddress(hLib,"DllUnregisterServer"); //获取注册函数DllUnregisterServer地址
if(lpDllEntryPoint!=NULL) //调用注册函数DllUnregisterServer
{
if(FAILED((*lpDllEntryPoint)()))
{
FreeLibrary(hLib);
return FALSE ;
}
return TRUE ;
}
else
return FALSE ;
}