分享:添加/获取IE受信任站点

jagin 2011-07-07 02:25:17
加精
之前在写获取IE受信任站点时,发现诸多麻烦,需分域名形式及IP形式站点分别设置。
在添加IP形式的站点时出现成功写入注册表,但是在Internet选项中信任站点列表中看不到新增的站点。折腾很久后,终于找到原因,在添加IP类站点时,需写入2项注册表值,必须先设置DWORD类型的值,再设置字符串类型的值,具体查看代码。在此分享,希望有碰到类似问题的童鞋可以得到解决。
另外,还做了进一步扩展,可添加受信任/受限制站点,关于函数第一个参数 HKEY hKey 的说明,一般来说该项为HKEY_CURRENT_USER 即可,但有这么一种情况,若是在服务程序中调用,因为服务程序的用户是SYSTEM,因此可能设置不到当前登录系统的用户,特将该参数独立出来,获取到正确的 hKey 后传入即可。
由于添加时域名形式站点时,规则颇多,总结了大部分,不知道是否完整,欢迎大家补充。另有不足的地方还请多多指教


/************************************************************************/
/* 添加站点
/* DWORD dwType 2--受信任站点 4--受限制站点
/************************************************************************/
BOOL SetTrustfulUrl(HKEY hKey, char *szUrl, DWORD dwType)
{
HKEY hkResult;

int rc = 0;
char *p = NULL;
char szProtocol[MAX_PATH] = {0};
char szData[MAX_PATH] = {0};
char szTemp[MAX_PATH] = {0};

char szRegPath[MAX_PATH] = {0};

strcpy(szTemp, szUrl);
//获取协议
p = strchr(szTemp, ':');

if (p != NULL)
{
*p = '\0';
strcpy(szProtocol, szTemp);
p += 3;
strcpy(szTemp, p);
}
else
{
strcpy(szProtocol, "*");
}

//去除多余的url
p = strrchr(szTemp, '/');
if (p != NULL)
{
*p = '\0';
}

//判断是IP还是域名
if (IsIP(szTemp)) //IP类站点添加
{
DWORD dwKeys = 0;

sprintf(szRegPath, "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\ZoneMap\\Ranges");

rc = RegCreateKey(hKey, szRegPath, &hkResult);
//先获取该key下有多少个项
rc = RegQueryInfoKey(hkResult, NULL, NULL, NULL, &dwKeys, NULL, NULL, NULL, NULL, NULL, NULL, NULL);

RegCloseKey(hkResult);
hkResult = NULL;

if (rc != ERROR_SUCCESS)
{
return FALSE;
}
else
{
sprintf(szRegPath, "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\ZoneMap\\Ranges\\Range%d", dwKeys+10);

rc = RegCreateKey(hKey, szRegPath, &hkResult);
//必须要先创建DWORD值,再创建字符串值,否则不能在Internet选项中信任站点列表中显示
RegSetValueEx(hkResult, szProtocol, NULL, REG_DWORD, (BYTE *)&dwType, sizeof(DWORD));
RegSetValueEx(hkResult, ":Range", NULL, REG_SZ, (BYTE *)&szTemp, strlen(szTemp));

RegCloseKey(hkResult);
hkResult = NULL;
}
}
else //域名类站点添加
{
p = strrchr(szTemp, '.');

if (p == NULL)
{
sprintf(szRegPath, "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\ZoneMap\\Domains\\%s", szTemp);
}
else
{
char szTempStr[MAX_PATH] = {0};
strcpy(szTempStr, p);

*p = '\0';
p = strrchr(szTemp, '.');
if (p == NULL)
{
sprintf(szRegPath, "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\ZoneMap\\Domains\\%s%s", szTemp, szTempStr);
}
else
{
*p = '\0';
p++;
sprintf(szRegPath, "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\ZoneMap\\Domains\\%s%s\\%s", p, szTempStr, szTemp);
}
}

rc = RegCreateKey(hKey, szRegPath, &hkResult);
RegSetValueEx(hkResult, szProtocol, NULL, REG_DWORD, (BYTE*)&dwType, sizeof(DWORD));

RegCloseKey(hkResult);
hkResult = NULL;
}

return TRUE;
}

/************************************************************************/
/* 获取站点
/* DWORD dwType 2--受信任站点 4--受限制站点
/************************************************************************/
BOOL GetTrustfunUrl(HKEY hKey, char *szUrl, DWORD dwType)
{
int rc = 0;
int rc2 = 0;
int rc3 = 0;
int index = 0;
int subIndex = 0;
int protocolIndex = 0;
HKEY hkResult;
HKEY hkSubKey;
HKEY hkHost;
char szKeyName[MAX_PATH] = {0};
char szHost[MAX_PATH] = {0};
char szTemp[MAX_PATH] = {0};
char szProtocol[MAX_PATH] = {0};
// char szUrl[MAX_PATH] = {0};
DWORD dwProtocol = 0;
DWORD dwKeys = 0;
DWORD dwData = 0;
DWORD dwLen = sizeof(DWORD);

//获取域名形式站点
rc = RegCreateKey(hKey, "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\ZoneMap\\Domains", &hkResult);

while (RegEnumKey(hkResult, index, szKeyName, MAX_PATH) == ERROR_SUCCESS)
{
if (RegOpenKey(hkResult, szKeyName, &hkSubKey) == ERROR_SUCCESS)
{
rc = RegQueryInfoKey(hkSubKey, NULL, NULL, NULL, &dwKeys, NULL, NULL, NULL, NULL, NULL, NULL, NULL);

//如果该项下没有子项,即没有主机名,表示为 *.xx.xxx
if (dwKeys < 1)
{
dwProtocol = sizeof(szProtocol);
protocolIndex = 0;
while (RegEnumValue(hkSubKey, protocolIndex, szProtocol, &dwProtocol, NULL, NULL, NULL, NULL) == ERROR_SUCCESS)
{
if (strcmp(szProtocol, "*") == 0)
{
sprintf(szTemp, "*.%s", szKeyName);
}
else
{
sprintf(szTemp, "%s://*.%s", szProtocol, szKeyName);
}
strcat(szUrl, szTemp);
strcat(szUrl, ",");

memset(szTemp, 0, MAX_PATH);

if (strlen(szUrl) > 200) //此处做了长度限制,可根据需要自行修改
{
*strrchr(szUrl, ',') = '\0';
return TRUE;
}

protocolIndex ++;
memset(szProtocol, 0, MAX_PATH);
}

}
else
{
subIndex = 0;
while (RegEnumKey(hkSubKey, subIndex, szHost, MAX_PATH) == ERROR_SUCCESS)
{
if (RegOpenKey(hkSubKey, szHost, &hkHost) == ERROR_SUCCESS)
{
dwProtocol = sizeof(szProtocol);
protocolIndex = 0;
while (RegEnumValue(hkHost, protocolIndex, szProtocol, &dwProtocol, NULL, NULL, NULL, NULL) == ERROR_SUCCESS)
{
RegQueryValueEx(hkHost, szProtocol, NULL, NULL, (LPBYTE)&dwData, &dwLen);

if (dwData == dwType)
{
if (strcmp(szProtocol, "*") == 0)
{
sprintf(szTemp, "%s.%s", szHost, szKeyName);
}
else
{
sprintf(szTemp, "%s://%s.%s", szProtocol, szHost, szKeyName);
}

strcat(szUrl, szTemp);
strcat(szUrl, ",");

memset(szTemp, 0, MAX_PATH);

if (strlen(szUrl) > 200)
{
RegCloseKey(hkHost);
RegCloseKey(hkSubKey);
RegCloseKey(hkResult);
hkHost = NULL;
hkSubKey = NULL;
hkResult = NULL;

*strrchr(szUrl, ',') = '\0';
return TRUE;
}
}

protocolIndex ++;
memset(szProtocol, 0, MAX_PATH);
}

RegCloseKey(hkHost);
hkHost = NULL;
}

subIndex ++;
memset(szHost, 0, MAX_PATH);
}
}

RegCloseKey(hkSubKey);
hkSubKey = NULL;
}

index ++;
memset(szKeyName, 0, MAX_PATH);
}

RegCloseKey(hkResult);
hkResult = NULL;

//获取IP形式站点
rc = RegCreateKey(hKey, "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\ZoneMap\\Ranges", &hkResult);

index = 0;
while (RegEnumKey(hkResult, index, szKeyName, MAX_PATH) == ERROR_SUCCESS)
{
if (RegOpenKey(hkResult, szKeyName, &hkSubKey) == ERROR_SUCCESS)
{
DWORD dwHost = MAX_PATH;
RegQueryValueEx(hkSubKey, ":Range", NULL, NULL, (LPBYTE)szHost, &dwHost);

subIndex = 0;
dwProtocol = sizeof(szProtocol);
while (RegEnumValue(hkSubKey, subIndex, szProtocol, &dwProtocol, NULL, NULL, NULL, NULL) == ERROR_SUCCESS)
{
if (strcmp(szProtocol, ":Range") != 0)
{
RegQueryValueEx(hkSubKey, szProtocol, NULL, NULL, (LPBYTE)&dwData, &dwLen);

if (dwData == dwType)
{
if (strcmp(szProtocol, "*") == 0)
{
sprintf(szTemp, "%s", szHost);
}
else
{
sprintf(szTemp, "%s://%s", szProtocol, szHost);
}

strcat(szUrl, szTemp);
strcat(szUrl, ",");
}

memset(szTemp, 0, MAX_PATH);

if (strlen(szUrl) > 200)
{
RegCloseKey(hkSubKey);
RegCloseKey(hkResult);
hkSubKey = NULL;
hkResult = NULL;

*strrchr(szUrl, ',') = '\0';
return TRUE;
}
}

subIndex++;
memset(szProtocol, 0, MAX_PATH);
}

RegCloseKey(hkSubKey);
hkSubKey = NULL;
}

index ++;
memset(szKeyName, 0, MAX_PATH);
}

RegCloseKey(hkResult);
hkResult = NULL;

*strrchr(szUrl, ',') = '\0';
return TRUE;
}

//判断是否是IP
BOOL IsIP(char *szIP)
{
if (strlen(szIP) == 0)
{
return FALSE;
}

int ip_part1 = 0;
int ip_part2 = 0;
int ip_part3 = 0;
int ip_part4 = 0;

if (sscanf(szIP, "%d.%d.%d.%d", &ip_part1, &ip_part2, &ip_part3, &ip_part4) != 4)
{
return FALSE;
}

if (!((ip_part1 >= 0 && ip_part1 <= 255) && (ip_part2 >= 0 && ip_part2 <= 255) && (ip_part3 >= 0 && ip_part3 <= 255) && (ip_part4 >= 0 && ip_part4 <= 255)))
{
return FALSE;
}

return TRUE;
}
...全文
2511 61 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
61 条回复
切换为时间正序
请发表友善的回复…
发表回复
ttf1 2012-12-14
  • 打赏
  • 举报
回复
必须先设置DWORD类型的值,再设置字符串类型的值
syxiyou 2012-11-01
  • 打赏
  • 举报
回复
请问IE6版本的这个代码可以用吗?IE6和其它高版本的IE写进注册表方式有点不一样啊。
古月无华 2012-09-10
  • 打赏
  • 举报
回复
你好,谢谢提供源码,代码是没问题的,通过程序的方式,可以实现加入站点,使用IE站点窗口删除通过程序加入的站点的时候,是没办法删除的。。。
wildwild1 2011-07-13
  • 打赏
  • 举报
回复
呵呵,回复下信任站点~。~
也在注册表里呵呵
小小卡拉眯 2011-07-12
  • 打赏
  • 举报
回复
看晕了啊
jimzhao 2011-07-12
  • 打赏
  • 举报
回复
谢楼主!
其实写清楚信任站点在注册表的位置可以了,比一堆代码更容易让人明白。
z371537759 2011-07-12
  • 打赏
  • 举报
回复
阿发送到噶的gas的话gas多个啊啊三打哈苏丹红
wangchen310 2011-07-11
  • 打赏
  • 举报
回复
真是麻烦啊,不过很不错
aummer177 2011-07-11
  • 打赏
  • 举报
回复
学习啊
robin_szcs 2011-07-11
  • 打赏
  • 举报
回复
真厉害。
l729684862 2011-07-10
  • 打赏
  • 举报
回复
lihai
kofkyo 2011-07-10
  • 打赏
  • 举报
回复
这个要留名学习
yayiya111 2011-07-09
  • 打赏
  • 举报
回复
支持一下!
jagin 2011-07-09
  • 打赏
  • 举报
回复
[Quote=引用 47 楼 zzxap 的回复:]

不能远程设置客户端ie
[/Quote]

服务发送命令下去,客户端本地调用设置就可以了。
xuzysun 2011-07-09
  • 打赏
  • 举报
回复
支持一下!
wangchao2000819 2011-07-09
  • 打赏
  • 举报
回复
很专业的东西,一般人很少用的到,
gahyyai 2011-07-09
  • 打赏
  • 举报
回复
看晕了!!
aydf1 2011-07-09
  • 打赏
  • 举报
回复
看晕了!!
jixingzhong 2011-07-09
  • 打赏
  • 举报
回复
不错,好东东
zzxap 2011-07-09
  • 打赏
  • 举报
回复
不能远程设置客户端ie
加载更多回复(40)

16,548

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • AIGC Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧