Rodgersmith
August 29th, 2008, 02:06 PM
Hi all,
i'm trying to use the IInternetZoneManager interface for calling GetZoneActionPolicy, but i'm having the hardest time getting it to work. Does anybody have a clue as to what i'm doing wrong here. There error that comes back when I try to execute the GetZoneActionPolicy method is
AccessViolationException: Attempted to read or write protected memory...
public class InternetZoneManger
{
//zone manager
private static IInternetZoneManager _izm; // IInternetZoneManager
private static object _zoneManager;
private static Guid CLSID_InternetZoneManager = new Guid("7b8a2d95-0ac9-11d1-896c-00c04Fb6bfc4");
private static Guid IID_IInternetZoneManager = new Guid("79eac9ef-baf9-11ce-8c82-00aa004ba90b");
// constants from urlmon.h
public const UInt32 URLZONE_LOCAL_MACHINE = 0;
public const UInt32 URLZONE_INTRANET = URLZONE_LOCAL_MACHINE + 1;
public const UInt32 URLZONE_TRUSTED = URLZONE_INTRANET + 1;
public const UInt32 URLZONE_INTERNET = URLZONE_TRUSTED + 1;
public 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);
public InternetZoneManger()
{
Type t2 = Type.GetTypeFromCLSID(CLSID_InternetZoneManager);
_zoneManager = Activator.CreateInstance(t2);
_izm = (IInternetZoneManager)_zoneManager;
}
public void GetZoneActionPolicy( )
{
UInt32 cbPolicy = 0;
byte[] profileBytes = new byte[4096]; // Not sure how big a profile is?
IntPtr ptrProfile = Marshal.AllocHGlobal(profileBytes.Length);
int result = _izm.GetZoneActionPolicy(URLZONE_INTERNET, URLACTION_ACTIVEX_SCRIPTLETRUN, ptrProfile, cbPolicy, URLZONEREG.URLZONEREG_HKCU);
for (int i = 0; i < profileBytes.Length; i++)
profileBytes[i] = Marshal.ReadByte(ptrProfile, i);
Marshal.FreeHGlobal(ptrProfile);
}
[ComImport, GuidAttribute("79eac9ef-baf9-11ce-8c82-00aa004ba90b"), InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
public interface IInternetZoneManager
{
[return: MarshalAs(UnmanagedType.I4)]
[PreserveSig]
int GetZoneActionPolicy(UInt32 dwZone, UInt32 dwAction, IntPtr pPolicy, UInt32 cbPolicy, URLZONEREG urlZoneReg);
}
//[DllImport("urlmon.dll", CharSet = CharSet.Auto,PreserveSig= true)]
//public interface IInternetZoneManager
//{
// int GetZoneActionPolicy(UInt32 dwZone, UInt32 dwAction, ref byte pPolicy, UInt32 cbPolicy, URLZONEREG urlZoneReg);
//}
public enum URLZONEREG
{
URLZONEREG_DEFAULT = 0, //default location
URLZONEREG_HKLM, //local machine
URLZONEREG_HKCU //current user
}
}
i'm trying to use the IInternetZoneManager interface for calling GetZoneActionPolicy, but i'm having the hardest time getting it to work. Does anybody have a clue as to what i'm doing wrong here. There error that comes back when I try to execute the GetZoneActionPolicy method is
AccessViolationException: Attempted to read or write protected memory...
public class InternetZoneManger
{
//zone manager
private static IInternetZoneManager _izm; // IInternetZoneManager
private static object _zoneManager;
private static Guid CLSID_InternetZoneManager = new Guid("7b8a2d95-0ac9-11d1-896c-00c04Fb6bfc4");
private static Guid IID_IInternetZoneManager = new Guid("79eac9ef-baf9-11ce-8c82-00aa004ba90b");
// constants from urlmon.h
public const UInt32 URLZONE_LOCAL_MACHINE = 0;
public const UInt32 URLZONE_INTRANET = URLZONE_LOCAL_MACHINE + 1;
public const UInt32 URLZONE_TRUSTED = URLZONE_INTRANET + 1;
public const UInt32 URLZONE_INTERNET = URLZONE_TRUSTED + 1;
public 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);
public InternetZoneManger()
{
Type t2 = Type.GetTypeFromCLSID(CLSID_InternetZoneManager);
_zoneManager = Activator.CreateInstance(t2);
_izm = (IInternetZoneManager)_zoneManager;
}
public void GetZoneActionPolicy( )
{
UInt32 cbPolicy = 0;
byte[] profileBytes = new byte[4096]; // Not sure how big a profile is?
IntPtr ptrProfile = Marshal.AllocHGlobal(profileBytes.Length);
int result = _izm.GetZoneActionPolicy(URLZONE_INTERNET, URLACTION_ACTIVEX_SCRIPTLETRUN, ptrProfile, cbPolicy, URLZONEREG.URLZONEREG_HKCU);
for (int i = 0; i < profileBytes.Length; i++)
profileBytes[i] = Marshal.ReadByte(ptrProfile, i);
Marshal.FreeHGlobal(ptrProfile);
}
[ComImport, GuidAttribute("79eac9ef-baf9-11ce-8c82-00aa004ba90b"), InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
public interface IInternetZoneManager
{
[return: MarshalAs(UnmanagedType.I4)]
[PreserveSig]
int GetZoneActionPolicy(UInt32 dwZone, UInt32 dwAction, IntPtr pPolicy, UInt32 cbPolicy, URLZONEREG urlZoneReg);
}
//[DllImport("urlmon.dll", CharSet = CharSet.Auto,PreserveSig= true)]
//public interface IInternetZoneManager
//{
// int GetZoneActionPolicy(UInt32 dwZone, UInt32 dwAction, ref byte pPolicy, UInt32 cbPolicy, URLZONEREG urlZoneReg);
//}
public enum URLZONEREG
{
URLZONEREG_DEFAULT = 0, //default location
URLZONEREG_HKLM, //local machine
URLZONEREG_HKCU //current user
}
}