// 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;
}
//一个网站当被某个客户端认为是可信站点后,从该站点下载的网页控件将被客户端信任且运行,那如何向客户端增加可信站点,是一项技术。
//传统的办法是直接操作注册表。但是据我研究,在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;
}