输入:注册表完整路径,如:HKEY_LOCAL_MACHINE\Software\Microsoft\ABC\dwPoints

输出:键值(如dwPoints对应的值)

CString GetKeyValue(CString bstrRegPath)
{
CString strPath = _T("");

CString path = bstrRegPath;

long length = path.GetLength();

int index = path.Find(_T('\\'));
if (index == -1)
{
return L"";
}

CString registryType = path.Left(index);

if (length - index - 1 < 0)
{
return L"";
}
CString fullPath = path.Right(length - index - 1);

length = fullPath.GetLength();
index = fullPath.ReverseFind(_T('\\'));
if (index == -1)
{
return L"";
}

CString pathDir = fullPath.Left(index);

if (length - index - 1 < 0)
{
return L"";
}
CString keyValue = fullPath.Right(length - index -1);

HKEY hkey;

DWORD dwSize = MAX_PATH;
TCHAR string[MAX_PATH];

HKEY registryConstType;

if (_tcscmp(registryType,L"HKEY_CLASSES_ROOT") == 0)
{
registryConstType = HKEY_CLASSES_ROOT;
}
else if (_tcscmp(registryType,L"HKEY_CURRENT_USER") == 0)
{
registryConstType = HKEY_CURRENT_USER;
}
else if (_tcscmp(registryType,L"HKEY_LOCAL_MACHINE") == 0)
{
registryConstType = HKEY_LOCAL_MACHINE;
}
else if (_tcscmp(registryType,L"HKEY_USERS") == 0)
{
registryConstType = HKEY_USERS;
}
else
{
return L"";
}

LONG lRet = RegOpenKeyEx(registryConstType,pathDir,0,KEY_READ,&hkey);
if(lRet != ERROR_SUCCESS)
{
return L"";
}

lRet = RegQueryValueEx(hkey, keyValue, NULL, REG_NONE,(LPBYTE)string,&dwSize);

if(lRet != ERROR_SUCCESS)
{
return L"";
}
else
{
strPath = string;

return strPath;
}
}

posted on 2013-04-28 20:42  ximenchuixie  阅读(895)  评论(0编辑  收藏  举报