// 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;
}