trouble implementing IInternetZoneManager

Announcements and discussion for PInvoke

Moderators: Luke Jefferson, Robert

trouble implementing IInternetZoneManager

Postby RodgerSmith » Thu Aug 28, 2008 4:02 pm

I'm writing a C# app that needs to get the ActiveXControl settings for trusted sites in IE. I've come to the conclusion that what I need to do is implement the IInternetZoneManger interface and call GetZoneActionPolicy, but when I call it I get the following error \"Attempted to read or write protected memory. This is often an indication that other memory is corrupt.\" Can someone please tell me what i'm doing wrong in the GetZoneActionPolicy method?

Below is the code from my calling class:

Code: Select all
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace SmartApplication
{
    public class InternetZoneManager
    {
        private static Guid CLSID_InternetZoneManager = new Guid(\"7b8a2d95-0ac9-11d1-896c-00c04Fb6bfc4\");
        private static Guid IID_IInternetZoneManager = new Guid(\"79eac9ef-baf9-11ce-8c82-00aa004ba90b\");
        private IInternetZoneManager _IInternetZoneManager;

        #region URLMon Constants
        private const int SET_FEATURE_ON_THREAD = 0x00000001;
        private const int SET_FEATURE_ON_PROCESS = 0x00000002;
        private const int SET_FEATURE_IN_REGISTRY = 0x00000004;
        private const int SET_FEATURE_ON_THREAD_LOCALMACHINE = 0x00000008;
        private const int SET_FEATURE_ON_THREAD_INTRANET = 0x00000010;
        private const int SET_FEATURE_ON_THREAD_TRUSTED = 0x00000020;
        private const int SET_FEATURE_ON_THREAD_INTERNET = 0x00000040;
        private const int SET_FEATURE_ON_THREAD_RESTRICTED = 0x00000080;
        private const UInt32 URLZONE_LOCAL_MACHINE = 0;
        private const UInt32 URLZONE_INTRANET = URLZONE_LOCAL_MACHINE + 1;
        private const UInt32 URLZONE_TRUSTED = URLZONE_INTRANET + 1;
        private const UInt32 URLZONE_INTERNET = URLZONE_TRUSTED + 1;
        private const UInt32 URLZONE_UNTRUSTED = URLZONE_INTERNET + 1;
        public const int URLACTION_ACTIVEX_SCRIPTLETRUN = unchecked((int)0x00001209);
        public const int URLACTION_DOWNLOAD_UNSIGNED_ACTIVEX = unchecked((int)0x00001004);
        #endregion

        public InternetZoneManager()
        {
            _IInternetZoneManager = CreateInternetZoneManager();
        }

        private IInternetZoneManager CreateInternetZoneManager()
        {
            try
            {
                Type t = Type.GetTypeFromCLSID(CLSID_InternetZoneManager);
                object instance = Activator.CreateInstance(t);
                return (IInternetZoneManager)instance;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public int GetZoneActionPolicy()
        {
            IntPtr out1 = IntPtr.Zero;
            IntPtr out2 = IntPtr.Zero;
            UInt32 cbPolicy = 0;
            int result = _IInternetZoneManager.GetZoneActionPolicy(URLZONE_INTERNET, URLACTION_ACTIVEX_SCRIPTLETRUN, out out1, cbPolicy, URLZONEREG.URLZONEREG_HKCU);
            if (result == Hresults.NOERROR)
            {
                //need to do something with the results here...
            }
            else
            {
                //throw an error
                return result;
            }
        }

        #region IInternetZoneManger interface
        [ComImport, GuidAttribute(\"79eac9ef-baf9-11ce-8c82-00aa004ba90b\"), InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
        public interface IInternetZoneManager
        {
            [return: MarshalAs(UnmanagedType.I4)]
            [PreserveSig]
            int GetZoneActionPolicy(UInt32 dwZone, UInt32 dwAction, out IntPtr pPolicy, UInt32 cbPolicy, URLZONEREG urlZoneReg);
        }
        #endregion

        public enum INTERNETFEATURELIST
        {
            FEATURE_ZONE_ELEVATION = 1,
            FEATURE_MIME_HANDLING = 2,
            FEATURE_MIME_SNIFFING = 3,
            FEATURE_WINDOW_RESTRICTIONS = 4,
            FEATURE_WEBOC_POPUPMANAGEMENT = 5,
            FEATURE_BEHAVIORS = 6,
            FEATURE_DISABLE_MK_PROTOCOL = 7,
            FEATURE_LOCALMACHINE_LOCKDOWN = 8,
            FEATURE_SECURITYBAND = 9,
            FEATURE_RESTRICT_ACTIVEXINSTALL = 10,
            FEATURE_VALIDATE_NAVIGATE_URL = 11,
            FEATURE_RESTRICT_FILEDOWNLOAD = 12,
            FEATURE_ADDON_MANAGEMENT = 13,
            FEATURE_PROTOCOL_LOCKDOWN = 14,
            FEATURE_HTTP_USERNAME_PASSWORD_DISABLE = 15,
            FEATURE_SAFE_BINDTOOBJECT = 16,
            FEATURE_UNC_SAVEDFILECHECK = 17,
            FEATURE_GET_URL_DOM_FILEPATH_UNENCODED = 18,
            FEATURE_TABBED_BROWSING = 19,
            FEATURE_SSLUX = 20,
            FEATURE_DISABLE_NAVIGATION_SOUNDS = 21,
            FEATURE_DISABLE_LEGACY_COMPRESSION = 22,
            FEATURE_FORCE_ADDR_AND_STATUS = 23,
            FEATURE_XMLHTTP = 24,
            FEATURE_DISABLE_TELNET_PROTOCOL = 25,
            FEATURE_FEEDS = 26,
            FEATURE_BLOCK_INPUT_PROMPTS = 27,
            FEATURE_ENTRY_COUNT = 28
        }

        public enum URLZONEREG
        {
            URLZONEREG_DEFAULT = 0, //default location
            URLZONEREG_HKLM, //local machine
            URLZONEREG_HKCU //current user
        }
    }
}
RodgerDodger
RodgerSmith
 
Posts: 6
Joined: Thu Aug 28, 2008 4:01 pm

Postby Robert » Thu Aug 28, 2008 4:48 pm

Hi,

From what I can see, there's two likely problems you've got. The first is that the method positions within an interface are important - so what I tend to do is to have empty signatures for all the methods in the interface, even if I'm not using them, then actually bother to "fill out" the signatures for the ones I do use. There's a list of the methods, in the correct order, at http://tinyurl.com/5a5hfq.

The second problem is the pPolicy parameter. Although this is an output parameter in the sense that it's used to pass data out from the function, it's not a C# "out" parameter. It should just be a normal IntPtr, which you'll have to allocate using something like Marshal.AllocHGlobal() before calling the method. The size of the allocation you just made is what you then pass in as the cbPolicy parameter. And remember to free that IntPtr with Marshal.FreeHGlobal once you're done with it ;-).

Hope that helps,
Rob
Robert Chipperfield
Developer, Red Gate Software Ltd
Robert
 
Posts: 410
Joined: Mon Oct 30, 2006 11:15 am
Location: Cambridge, UK

Postby RodgerSmith » Thu Aug 28, 2008 6:01 pm

thank you for your timely reply. I'll take a look at what you've said and see if i can figure it out from here.
RodgerDodger
RodgerSmith
 
Posts: 6
Joined: Thu Aug 28, 2008 4:01 pm

Postby RodgerSmith » Thu Aug 28, 2008 11:16 pm

sir, do you think you would have any kind of vb or c# code that would execute this I'm having a very hard time with this and would very much appreciate a little further information.
RodgerDodger
RodgerSmith
 
Posts: 6
Joined: Thu Aug 28, 2008 4:01 pm

Postby Robert » Fri Aug 29, 2008 9:30 am

Which bit in particular? In order to actually make the call, you can do something like this:

Code: Select all
byte[] profileBytes = new byte[4096]; // Not sure how big a profile is?
IntPtr ptrProfile = Marshal.AllocHGlobal (profileBytes.Length);
// ...Make the call to GetZoneActionPolicy here...
for (int i = 0; i < profileBytes.Length; i++)
  profileBytes[i] = Marshal.ReadByte (ptrProfile, i);
Marshal.FreeHGlobal (ptrProfile);


That'll copy the contents of the buffer pointed to by ptrProfile into the more .NET friendly byte array. It's not necessarily the most efficient way of doing it, by the way, but it's the simplest to write without going into unsafe code.

I'm afraid I don't know anything about the IE zones myself, so I don't know exactly what a zone action policy is, or what to do with it from there, but hopefully this'll at least get you to the point of getting the data...

Cheers,
Rob
Robert Chipperfield
Developer, Red Gate Software Ltd
Robert
 
Posts: 410
Joined: Mon Oct 30, 2006 11:15 am
Location: Cambridge, UK

Postby RodgerSmith » Fri Aug 29, 2008 2:59 pm

that got me a little further along. Now i'm getting a different error, "AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt"

do you think the profile bytes isn't big enough?
RodgerDodger
RodgerSmith
 
Posts: 6
Joined: Thu Aug 28, 2008 4:01 pm

Postby Robert » Fri Aug 29, 2008 3:27 pm

In theory that shouldn't matter, as long as you're passing the same size into cbPolicy parameter as you're allocating in the AllocHGlobal call.

It's worth checking you've got all the methods defined in the interface, and in the correct order, or all bets are off ;-).

Is it the call to GetZoneActionPolicy that causes the AccessViolationException, or does it happens somewhere else?
Robert Chipperfield
Developer, Red Gate Software Ltd
Robert
 
Posts: 410
Joined: Mon Oct 30, 2006 11:15 am
Location: Cambridge, UK

Postby RodgerSmith » Fri Aug 29, 2008 3:31 pm

ah, that i had not done, i had only created an inteface method for the one one method that I wanted to execute. I'll try creating a method for everything the IInternetZoneManager does and see if that helps.
RodgerDodger
RodgerSmith
 
Posts: 6
Joined: Thu Aug 28, 2008 4:01 pm

Postby RodgerSmith » Fri Aug 29, 2008 3:49 pm

btw, it's in the call to the GetZoneActionPolicy.
RodgerDodger
RodgerSmith
 
Posts: 6
Joined: Thu Aug 28, 2008 4:01 pm


Return to PInvoke.Net

Who is online

Users browsing this forum: No registered users and 0 guests