写注册表操作

来源:互联网 发布:nba2016年胜负数据 编辑:程序博客网 时间:2024/06/01 20:27
1.使用RegOpenKeyEx进行打开注册表
2.使用RegCreateKeyEx进行创建注册表
3.使用RegSetValueEx进行设置值
4。RegCloseKey关闭注册表
BOOL CIEOpt::AddPopupWindow()
{
LoadPopupWindowUrlList();
unsigned int i = 0;

DWORD dwReg_type = REG_BINARY;
LONG lRet = ERROR_SUCCESS;
HKEY hkey = NULL;
_bstr_t strUrl = _T("");
BYTE chexValue[4] = { 0,0,0,0 };
_bstr_t strValue = _bstr_t(chexValue)/*_T("")*/;

while (i < m_popupWindowItems.size())
{
lRet = RegOpenKeyEx(HKEY_CURRENT_USER, _T("Software\\Microsoft\\Internet Explorer\\New Windows\\Allow"), 0, KEY_ALL_ACCESS | KEY_WOW64_32KEY, &hkey);

if (lRet != ERROR_SUCCESS)
{
lRet = RegCreateKeyEx(HKEY_CURRENT_USER, _T("Software\\Microsoft\\Internet Explorer\\New Windows\\Allow"), 0, NULL, REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, NULL);
if (lRet != ERROR_SUCCESS)
{
return FALSE;
}
}

strUrl = m_popupWindowItems.at(i);
if (strUrl.length() == 0)
{
}
else
{
lRet = RegSetValueEx(hkey, strUrl, 0, dwReg_type, (const BYTE*)strValue.GetBSTR(), strValue.length());

if (lRet != ERROR_SUCCESS)
{
RegCloseKey(hkey);
return FALSE;
}
}


RegCloseKey(hkey);
i++;
}

return TRUE;
}
BOOL CIEOpt::LoadPopupWindowUrlList()
{
//装载弹出窗口允许的网站地址
_bstr_t bstrConfigPath;
_bstr_t bstrFileName;
if (!GetConfigDir(bstrConfigPath))
{
return FALSE;
}

bstrConfigPath += _T("AllowPopupWindow.xml");
bstrFileName = bstrConfigPath;

_bstr_t m_bstrXPath = _T("TrustDomain/Item");

m_popupWindowItems.clear();
util_xml::CXmlHelper* pXmlHelper = new util_xml::CXmlHelper;

pXmlHelper->m_bstrXmlFileName = bstrFileName;
BOOL bRet = pXmlHelper->Load();
if (!bRet)
{
WRITE_WARNING_LOG(_T("info: allowPopupwindow.xml load fail!\n"));
return FALSE;
}
else
{
CComQIPtr<IXMLDOMNodeList> nodeList;
pXmlHelper->SelectXmlNodes(m_bstrXPath, nodeList);
long lCount;
nodeList->get_length(&lCount);
for (int i = 0; i < lCount; i++)
{
CComQIPtr<IXMLDOMNode> pNode;
nodeList->get_item(i, &pNode);
if (pNode != NULL)
{
_bstr_t pNewItem;
pXmlHelper->GetNodeAttribute(pNode, "url", pNewItem);
m_popupWindowItems.push_back(pNewItem);
}
}
}

return TRUE;
}
原创粉丝点击