Programmatically adding sites to the "Trusted" Zone in IE
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13

Thread: Programmatically adding sites to the "Trusted" Zone in IE

Hybrid View

  1. #1
    Join Date
    Dec 2005
    Posts
    3

    Programmatically adding sites to the "Trusted" Zone in IE

    Alright, I have a working version in C# .NET, but not all my clients will have the .NET runtime installed, so instead we've decided we need to port it to C++.

    Objectives of the Application:
    The application will have a list of URLs to add to IE's "Trusted" zone. After adding the zones it will display a message box with any successes/failures.

    Current Code:
    Code:
    #include "windows.h"
    #include "stdafx.h"
    #include "urlmon.h"
    #using <mscorlib.dll>
    #include <atldef.h>
    #include <atlconv.h>
    using namespace System;
    using namespace System::Runtime::InteropServices;
    #define MAX_LOADSTRING 100
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    HRESULT hr;
    	IInternetSecurityManager *pSecurityMgr;
    	IInternetZoneManager *pZoneMgr;
    	LPCWSTR site1 = SysAllocString(L"http://www.mydomain.com");
    
    
    	hr = CoCreateInstance(CLSID_InternetSecurityManager, NULL, CLSCTX_INPROC_SERVER, IID_IInternetSecurityManager, (void**)&pSecurityMgr);
    	pSecurityMgr->SetZoneMapping((DWORD)2, site1, (DWORD)0); // 2 = Trusted Site, site1 is the URL to add, and 0 is to create the entry.
    }
    That is just a snippet, but it gives you everything I'm trying to do.

    The problem is that when I call CoCreateInstance(), I get a return value of -2147221008. When that happens pSecurityMgr is pointing to null and the SetZoneMapping() results in a null pointer exception.

    My question is why is CoCreateInstance failing?

    Any help would be appreciated, especially since I am following word for word what is happening here:
    http://education.dewsoftoverseas.com...rview.htm]here

    (Note: Scroll all the way to the bottom for their code example.)

    Just incase you want to compile it, here is the WHOLE source code:

    Code:
    // test6.cpp : Defines the entry point for the console application.
    //
    
    #include "windows.h"
    #include "stdafx.h"
    #include "urlmon.h"
    #using <mscorlib.dll>
    #include <atldef.h>
    #include <atlconv.h>
    using namespace System;
    using namespace System::Runtime::InteropServices;
    #define MAX_LOADSTRING 100
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	// constants from urlmon.h
    	const int URLZONE_LOCAL_MACHINE = 0;
    	const int URLZONE_INTRANET = URLZONE_LOCAL_MACHINE + 1;
    	const int URLZONE_TRUSTED = URLZONE_INTRANET + 1;
    	const int URLZONE_INTERNET = URLZONE_TRUSTED + 1;
    	const int URLZONE_UNTRUSTED = URLZONE_INTERNET + 1;
    	const int URLZONE_ESC_FLAG = 0x100;
    	const int SZM_CREATE  = 0;
    	const int SZM_DELETE  = 0x1;
    
    	HRESULT hr;
    	IInternetSecurityManager *pSecurityMgr;
    	IInternetZoneManager *pZoneMgr;
    	LPCWSTR site1 = SysAllocString(L"http://www.mydomain.com");
    
    	hr = CoCreateInstance(CLSID_InternetSecurityManager, NULL, CLSCTX_INPROC_SERVER, IID_IInternetSecurityManager, (void**)&pSecurityMgr);
    
    	pSecurityMgr->SetZoneMapping((DWORD)2, site1, (DWORD)0); // 2 = Trusted Site, site1 is the URL to add, and 0 is to create the entry.
    
    	pSecurityMgr->Release();
    	pZoneMgr->Release();
    
    	return 0;
    }

  2. #2
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354

    Re: Programmatically adding sites to the "Trusted" Zone in IE


  3. #3
    Join Date
    Dec 2005
    Posts
    3

    Re: Programmatically adding sites to the "Trusted" Zone in IE

    That is so cool. I knew it had to be something simple. Thanks Kirants!

  4. #4
    Join Date
    Dec 2005
    Posts
    3

    Re: Programmatically adding sites to the "Trusted" Zone in IE

    Just wanted to give the full working code. The problem was that I did not call CoInitialize(NULL); in the program. Here is the complete working code:

    Code:
    #include "windows.h"
    #include "stdafx.h"
    #include "urlmon.h"
    #using <mscorlib.dll>
    #include <atldef.h>
    #include <atlconv.h>
    using namespace System;
    using namespace System::Runtime::InteropServices;
    #define MAX_LOADSTRING 100
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	// constants from urlmon.h
    	const int URLZONE_LOCAL_MACHINE = 0;
    	const int URLZONE_INTRANET = URLZONE_LOCAL_MACHINE + 1;
    	const int URLZONE_TRUSTED = URLZONE_INTRANET + 1;
    	const int URLZONE_INTERNET = URLZONE_TRUSTED + 1;
    	const int URLZONE_UNTRUSTED = URLZONE_INTERNET + 1;
    	const int URLZONE_ESC_FLAG = 0x100;
    	const int SZM_CREATE  = 0;
    	const int SZM_DELETE  = 0x1;
    
    	HRESULT hr;
    	IInternetSecurityManager *pSecurityMgr;
    	LPCWSTR sites = SysAllocString(L"http://*.mydomain.com");
    
    	CoInitialize(NULL);
    
    	hr = CoCreateInstance(CLSID_InternetSecurityManager, NULL, CLSCTX_INPROC_SERVER, IID_IInternetSecurityManager, (void**)&pSecurityMgr);
    
    	pSecurityMgr->SetZoneMapping(URLZONE_TRUSTED, sites, SZM_CREATE);
    
    	pSecurityMgr->Release();
    
    	return 0;
    }

  5. #5
    Join Date
    Oct 2002
    Location
    Germany
    Posts
    6,205

    Re: Programmatically adding sites to the "Trusted" Zone in IE

    For information's sake if any of you would like to know where IE stored your information... Then, it's here -

    HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap
    Figuring this out is what delayed my post...

  6. #6
    Join Date
    Feb 2008
    Posts
    28

    Re: Programmatically adding sites to the "Trusted" Zone in IE

    Quote Originally Posted by Siddhartha View Post
    For information's sake if any of you would like to know where IE stored your information... Then, it's here -

    HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap

    Figuring this out is what delayed my post...
    I need to do pretty much same thing in a installer for an application. Installer and the application is designed to work on a per-machine basis, so I need an API that add the URL for all users? How can we do that?

    Secondly, there is check for require https://. I think this is check is accessible/editable via registry:

    @"Software\Microsoft\Windows\CurrentVersion\Intern et
    Settings\Zones\" + ie_zone.ToString(),true);

    Int32 keyValue = (Int32)key.GetValue("Flags");

    However, is it possible to access/edit this via the API rather than direct editing the registry?

    Thanks in advance.
    Last edited by JoderCoder; December 8th, 2008 at 11:52 AM.

  7. #7
    Join Date
    Oct 2002
    Location
    Germany
    Posts
    6,205

    Re: Programmatically adding sites to the "Trusted" Zone in IE

    The problem is that when I call CoCreateInstance(), I get a return value of -2147221008.
    This is because you have not initialized the COM Library...

    At the beginning of the program (or the thread for a Multithreaded one) call -
    Code:
    ::CoInitialize (NULL);
    ...to initialize the COM Library.

    As for how I could tell this given the error code... There is a handy utility called Error Lookup that you can locate via the Tools --> Error Lookup option in your version of Visual Studio.

    This is the first change you need to make.

    EDIT: Darn, I am late... Kiran's post is complete enough...
    Last edited by Siddhartha; December 12th, 2005 at 12:24 PM.

  8. #8
    Join Date
    Feb 2008
    Posts
    6

    Re: Programmatically adding sites to the "Trusted" Zone in IE

    I am trying to run the code in Visual Studio.

    but the following error is coming

    Error 1 fatal error C1190: managed targeted code requires a '/clr' option

    at line number 7.....

    At line number #using <mscorlib.dll> statement is there...

    could you please reply as early as possible...it is very urgent to me..to run this code....

  9. #9
    Join Date
    Oct 2002
    Location
    Germany
    Posts
    6,205

    Re: Programmatically adding sites to the "Trusted" Zone in IE

    That is managed C++ code. So, unless you are using MC++, you cant use that code or mscorlib.dll...

  10. #10
    Join Date
    Feb 2008
    Posts
    6

    Re: Programmatically adding sites to the "Trusted" Zone in IE

    Thanks for ur response siddharth...

    I am new to Visual Studio. Can you give brief introduction like..what type of proj i have to create to run this code successfully...

    what changes I have to do?

  11. #11
    Join Date
    Oct 2002
    Location
    Germany
    Posts
    6,205

    Re: Programmatically adding sites to the "Trusted" Zone in IE

    I think you should first create a new thread with your questions, with an appropriate title in the appropriate forum.

    The project for this sample needs to to be a "Win32 --> Win32 Console Application" with Common Language Runtime Support (can be set via Project Properties).
    Last edited by Siddhartha; February 6th, 2008 at 06:57 AM.

  12. #12
    Join Date
    May 2013
    Posts
    2

    Re: Programmatically adding sites to the "Trusted" Zone in IE

    PLEASE IGNORE MY ABOVE COMMENT^^^^

    I sussed out that this was because it was not a valid URL address.
    I am now stuck on my next problem. How can I make it so that the 'L "http://*.mydomain.com"' is text of my input. so the user gets asked for their input i.e. http://www.example.com. This will be saved into a string? ? and then output this where 'http://*.mydomain.com'

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center