IE浏览器之可信任站点

// AddTrustSite.cpp : Defines the entry point for the application.

//一个网站当被某个客户端认为是可信站点后,从该站点下载的网页控件将被客户端信任且运行,那如何向客户端增加可信站点,是一项技术。

//传统的办法是直接操作注册表。但是据我研究,在winXP和win2003及winVista上,可信站点列表在注册表中的位置并不相同,且根据ie的不同版本,该位置也有不少差异。

//因此如果在不同平台和ie版本上增加可信站点,需要大量的判断。

//这里给出了一个办法,是直接调用ie的com组件,通过该com组件的接口,向系统中直接添加可信站点,且不被系统中是否为https站点的限制。

//编译后,支持命令行的方式运行。运行方式为:” AddTrustSite.exe http://www.sina.com.cn

//#include “urlmon.h”

#include “windows.h”
#include “stdio.h”

bool AddTrustSite(LPCTSTR lpSiteName);

int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
if(AddTrustSite(lpCmdLine))
{
MessageBox(NULL, “AddTrustSite success!”, “Note”, MB_OK | MB_ICONEXCLAMATION);
}
else
{
MessageBox(NULL, “AddTrustSite failed!”, “Note”, MB_OK | MB_ICONEXCLAMATION);
}
return 0;
}

bool AddTrustSite(LPCTSTR lpSiteName)
{
HRESULT hr;
IInternetSecurityManager *pSecurityMgr;
IInternetZoneManager *pZoneMgr;
int iUnicodeLen = 0;
WCHAR *pwcharUnicode = NULL;
ZONEATTRIBUTES zoneAttributes;
LPWSTR lpTrustSite = NULL;
#ifdef _UNICODE
lpTrustSite = SysAllocString(lpSiteName);
#else
iUnicodeLen = MultiByteToWideChar(CP_ACP, 0, lpSiteName, strlen(lpSiteName), NULL, 0);
if(iUnicodeLen < 1)
{
return false;
}

 pwcharUnicode = new WCHAR[iUnicodeLen+1];
 memset(pwcharUnicode, 0x00, sizeof(WCHAR)*(iUnicodeLen+1));

 if(iUnicodeLen != MultiByteToWideChar(CP_ACP, 0, lpSiteName, strlen(lpSiteName), pwcharUnicode, iUnicodeLen))
 {
     if(pwcharUnicode != NULL)
     {
         delete [] pwcharUnicode;
         pwcharUnicode = NULL;
     }
     return false;
 }

lpTrustSite = SysAllocString(pwcharUnicode);
#endif
::CoInitialize(NULL);
hr = CoCreateInstance(CLSID_InternetSecurityManager, NULL, CLSCTX_INPROC_SERVER, IID_IInternetSecurityManager, (void**)&pSecurityMgr);
hr = CoCreateInstance(CLSID_InternetZoneManager,NULL,CLSCTX_INPROC_SERVER,IID_IInternetZoneManager,(void**)&pZoneMgr);

 pZoneMgr->GetZoneAttributes((DWORD)2,&zoneAttributes);
 if((zoneAttributes.dwFlags & ZAFLAGS_REQUIRE_VERIFICATION) == ZAFLAGS_REQUIRE_VERIFICATION)
 {
     zoneAttributes.dwFlags &= ~ZAFLAGS_REQUIRE_VERIFICATION;
     pZoneMgr->SetZoneAttributes((DWORD)URLZONE_TRUSTED,&zoneAttributes);
     pSecurityMgr->SetZoneMapping((DWORD)URLZONE_TRUSTED, lpTrustSite, (DWORD)SZM_CREATE );
     zoneAttributes.dwFlags |= ZAFLAGS_REQUIRE_VERIFICATION;
     pZoneMgr->SetZoneAttributes((DWORD)URLZONE_TRUSTED,&zoneAttributes);
 }
 else
 {
     pSecurityMgr->SetZoneMapping((DWORD)URLZONE_TRUSTED, lpTrustSite, (DWORD)SZM_CREATE );
 }


 pSecurityMgr->Release();
 pZoneMgr->Release();

SysFreeString(lpTrustSite);

 if(pwcharUnicode != NULL)
 {
     delete [] pwcharUnicode;
     pwcharUnicode = NULL;
 }

::CoUninitialize();
return true;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值