-
December 12th, 2005, 01:04 PM
#1
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;
}
-
December 12th, 2005, 01:13 PM
#2
Re: Programmatically adding sites to the "Trusted" Zone in IE
-
December 12th, 2005, 01:22 PM
#3
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 01:24 PM.
-
December 12th, 2005, 01:23 PM
#4
Re: Programmatically adding sites to the "Trusted" Zone in IE
That is so cool. I knew it had to be something simple. Thanks Kirants!
-
December 12th, 2005, 01:25 PM
#5
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;
}
-
December 12th, 2005, 01:34 PM
#6
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...
-
February 5th, 2008, 06:59 AM
#7
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....
-
February 5th, 2008, 07:08 AM
#8
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...
-
February 6th, 2008, 06:44 AM
#9
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?
-
February 6th, 2008, 07:05 AM
#10
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 07:57 AM.
-
December 8th, 2008, 12:46 PM
#11
Re: Programmatically adding sites to the "Trusted" Zone in IE
Originally Posted by Siddhartha
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 12:52 PM.
-
May 31st, 2013, 08:06 AM
#12
Re: Programmatically adding sites to the "Trusted" Zone in IE
Hi guys. In the working example the URL used is: 'http://*.mydomain.co.uk'. If i take out the '*.' then it still adds a '*.'.
Please note that I am trying to add a 'https' site which would go as followed 'https://example.com'.
Thanks all for your help if you reply as I know that this is an old forum.
-
May 31st, 2013, 08:30 AM
#13
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|