#include "StdAfx.h"
#include "FindPath.h"
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
CFindPath::CFindPath(void)
{
}
CFindPath::~CFindPath(void)
{
}
/*****************************************************************************
函数名称:GetKeyValue()
功能描述:获取注册表指定项下某个名称对应的值
输入参数:注册表项:HKEY,子项:szDesKeyItem,需要获取值的名称:szValueName
输出参数:CString:该名称对应的值
返 回 值: CString
*****************************************************************************/
CString CFindPath::GetKeyValue(HKEY hKey,LPCTSTR szDesKeyItem,LPCTSTR szValueName)
{
CString szKeyValue = _T("");
HKEY m_hkey;
if (::RegOpenKeyExW(hKey,szDesKeyItem,0,KEY_ALL_ACCESS,&m_hkey) == ERROR_SUCCESS)
{
DWORD m_kk = 500;//记录字符串的长度+1(包括NULL字符
WCHAR* wcBuf = new WCHAR[500];
memset(wcBuf,0,500);
DWORD dType = REG_SZ;
m_kk = 500;
if (::RegQueryValueExW(m_hkey,szValueName,NULL,&dType,(LPBYTE)wcBuf,&m_kk) == ERROR_SUCCESS)
{
szKeyValue = wcBuf;
return szKeyValue;
}
}
return szKeyValue;
}
/*****************************************************************************
函数名称:FindPathByOcx()
功能描述:通过OCX注册信息获得安装程序路径
输入参数:OCX名称
输出参数:ocx程序的路径
返 回 值: CString
*****************************************************************************/
CString CFindPath::FindPathByOcx(CString OcxName)
{
CString OCXpath = _T("");
HKEY m_hkey;
//先根据CLSID来查找
long ret0 = ::RegOpenKeyEx(HKEY_CLASSES_ROOT,
L"Clsid\\{E81D3060-D081-4012-A754-88D3FF824169}\\InprocServer32\\",0,
KEY_ALL_ACCESS,&m_hkey);
DWORD m_kk = 500;//记录字符串的长度+1(包括NULL字符
WCHAR* wcBuf = new WCHAR[500];
memset(wcBuf,0,500);
DWORD dType = REG_SZ;
m_kk = 500;
ret0 = (::RegQueryValueExW(m_hkey,L"",NULL,&dType,(LPBYTE)wcBuf,&m_kk));
OCXpath = wcBuf;
if( ret0 == ERROR_SUCCESS && OCXpath != _T(""))
{
if (OCXpath.Find(OcxName) > 0)
{
OCXpath = OCXpath.Left(OCXpath.ReverseFind(_T('\\')));
::RegCloseKey(m_hkey);
delete wcBuf;
return OCXpath;
}
}
else
{
OCXpath = EnumSubKey(HKEY_CLASSES_ROOT,L"Clsid",OcxName);
::RegCloseKey(m_hkey);
delete wcBuf;
return OCXpath;
}
return OCXpath;
}
/*****************************************************************************
函数名称:EnumSubKey()
功能描述:枚举注册表指定项下的所有键值,返回与ocx相同的键值
输入参数:注册表项:HKEY,子项:szDesKeyItem,ocx名称
输出参数:ocx程序的路径
返 回 值: CString
*****************************************************************************/
CString CFindPath::EnumSubKey(HKEY hKey,LPCTSTR szDesKeyItem,const CString OCXName)
{
CString OCXpath = _T("");
HKEY m_hkey;
long lResult;
DWORD dwIndex = 0;
FILETIME ftLastWriteTime;
DWORD m_kk = 500;//记录字符串的长度+1包括NULL字符
WCHAR* wcBuf = new WCHAR[500];
DWORD dType = REG_SZ;
if (::RegOpenKeyExW(hKey,szDesKeyItem,0,KEY_READ,&m_hkey) == ERROR_SUCCESS)
{
TCHAR szKeyName[256]={0};
DWORD cbName = 256;
lResult = RegEnumKeyEx(m_hkey,dwIndex,szKeyName,&cbName,0,NULL,NULL,NULL);
while(lResult == ERROR_SUCCESS && lResult != ERROR_MORE_DATA)
{
CString str = szKeyName;
str += _T("\n");
OutputDebugString(str);
dwIndex = dwIndex + 1;
//注意一定要给cbName重新赋值,因为其值已变,否则导致下一个RegEumKeyEx错误
cbName = 256 * sizeof(TCHAR);
//lResult = RegEnumKeyExW(m_hkey,dwIndex,szKeyName,&cbName,NULL,NULL,NULL,&ftLastWriteTime);
lResult = RegEnumKeyEx(m_hkey,dwIndex,szKeyName,&cbName,0,NULL,NULL,NULL);
HKEY m_hkeyTmp = NULL;
CString szKeyNameTemp= _T("");
szKeyNameTemp = szKeyName;
szKeyNameTemp = _T("Clsid\\") + szKeyNameTemp + _T("\\InprocServer32\\");
if (m_hkeyTmp != NULL)
{
RegCloseKey(m_hkeyTmp);
}
long ret0 = ::RegOpenKeyExW(hKey,szKeyNameTemp,0,KEY_ALL_ACCESS,&m_hkeyTmp);
if( ret0 != ERROR_SUCCESS )
{
OCXpath = L"";
continue;
}
else
{
m_kk = 500;
ret0 = (::RegQueryValueExW(m_hkeyTmp,L"",NULL,&dType,(LPBYTE)wcBuf,&m_kk));
OCXpath = wcBuf;
if( ret0 != ERROR_SUCCESS )
{
OCXpath = L"";
continue;
}
if (OCXpath.Find(OCXName) > 0)
{
OCXpath = OCXpath.Left(OCXpath.ReverseFind(_T('\\')));
::RegCloseKey(m_hkey);
RegCloseKey(m_hkeyTmp);
delete wcBuf;
return OCXpath;
}
}
if (m_hkeyTmp != NULL)
{
RegCloseKey(m_hkeyTmp);
}
}
delete wcBuf;
return OCXpath;
}
return OCXpath;
}
/*****************************************************************************
函数名称:CreateSubKey()
功能描述:在注册表指定项下的创建新的子项
输入参数:注册表项:HKEY,父项:szParentKeyPath,子项:szNewKeyName
输出参数:BOOL,true:创建成功,false:创建失败
返 回 值: BOOL
*****************************************************************************/
BOOL CFindPath::CreateSubKey(HKEY hKey,LPCTSTR szParentKeyPath,LPCTSTR szNewKeyName)
{
HKEY hSoftKey;
HKEY hPlaceKey;
//打开注册表指定的项
if (RegOpenKeyEx(hKey, szParentKeyPath, 0, KEY_WRITE|KEY_READ,&hSoftKey) == ERROR_SUCCESS)
{
DWORD dw=0;
//创建并打开子项
if (RegCreateKeyEx(hSoftKey, szNewKeyName, 0, REG_NONE, REG_OPTION_NON_VOLATILE, KEY_WRITE|KEY_READ, NULL,&hPlaceKey, &dw) == ERROR_SUCCESS)
{
//创建并打开HKEY_LOCAL_MACHINE\SOFTWARE\YanTai\BandSoft
RegCloseKey(hSoftKey);
RegCloseKey(hPlaceKey);
return TRUE;
}
RegCloseKey(hSoftKey);
}
return FALSE;
}
/*****************************************************************************
函数名称:AddKeyValue()
功能描述:设置注册表指定项下的键值
输入参数:注册表项:HKEY,子项:szNewKeyName,要设置的数量类型:NewKeyType,包含数据的缓冲区中的第一个字节:NewKeyValue,lpData缓冲区的长度:NewKeyValueByteLen
输出参数:ocx程序的路径
返 回 值: CString
*****************************************************************************/
BOOL CFindPath::AddKeyValue(HKEY hKey,LPCTSTR szDesKeyItem,LPCTSTR szNewKeyName,DWORD NewKeyType,LPBYTE NewKeyValue,DWORD NewKeyValueByteLen)
{
HKEY hCompanyKey;
//打开指定项的注册表
NewKeyValueByteLen*=sizeof(TCHAR);
if(RegOpenKeyEx(hKey, szDesKeyItem, 0, KEY_WRITE|KEY_READ,&hCompanyKey)==ERROR_SUCCESS)
{
//新建值
if(RegSetValueEx(hCompanyKey,szNewKeyName,0,NewKeyType,(const BYTE*)NewKeyValue,NewKeyValueByteLen) == ERROR_SUCCESS)
{
RegCloseKey(hCompanyKey);
return TRUE;
}
}
RegCloseKey(hCompanyKey);
return FALSE;
}