troubleshooting Question

C++ c# Conversion

Avatar of brigzy
brigzy asked on
C++Web BrowsersC#
9 Comments2 Solutions1197 ViewsLast Modified:
Dear Experts
I have been tryimg to create a C# version of calls to the Internet Explorer functions IERegCreateKeyEx IERegSetValueEx.

The functions basically allow you to create/set registry functions when IE is in protected mode.  This is useful if you have say a toolbar application, and need to set/recall some settings.

I have made a skeleton class below, which could be included in a new c# project, if anyone could help me get this working it would be a usefull class for the community.

The bits that need completing are where the class is incomplete in the c# functiions SetRegKey and CreateRegKey,  where you will see a question mark  in the function call parameters, as I dont know/understand the values that should be sent to the underlying fuctions.   My C++ experience is zero!

The comments have been gathered from MSDN documentation regarding the IERegCreateKeyEx IERegSetValueEx functions, which I hope may help an Expert.

using System;
using System.Runtime.InteropServices;
using Microsoft.Win32;
// C# class to provide Protected Mode Regsistry functions to IE toolbar type applications
// Class History:
// Who          What                        When
// R.Briggs     Initial Skeleton            28/09/2009
    public enum RegOption
        NonVolatile = 0x0,
        Volatile = 0x1,
        CreateLink = 0x2,
        BackupRestore = 0x4,
        OpenLink = 0x8
    public enum RegSAM
        QueryValue = 0x0001,
        SetValue = 0x0002,
        CreateSubKey = 0x0004,
        EnumerateSubKeys = 0x0008,
        Notify = 0x0010,
        CreateLink = 0x0020,
        WOW64_32Key = 0x0200,
        WOW64_64Key = 0x0100,
        WOW64_Res = 0x0300,
        Read = 0x00020019,
        Write = 0x00020006,
        Execute = 0x00020019,
        AllAccess = 0x000f003f
    public enum RegResult
        CreatedNewKey = 0x00000001,
        OpenedExistingKey = 0x00000002
    public struct SECURITY_ATTRIBUTES
        public int nLength;
        public IntPtr lpSecurityDescriptor;
        public int bInheritHandle;
    /// <summary>   
    /// Native methods class.   
    /// </summary>   
    internal static class ProtectedMode
        //// IE Reg Create value C++
        //HRESULT IERegCreateKeyEx(
        //    LPCWSTR lpSubKey, 
        //    DWORD reserved, 
        //    LPWSTR lpClass, 
        //    DWORD dwOptions, 
        //    REGSAM samDesired, 
        //    LPSECURITY_ATTRIBUTES lpSecurityAttributes, 
        //    PHKEY phkResult, 
        //    LPDWORD lpdwDisposition);
        // IE Reg Create Value c#
        [DllImport("ieframe.dll", SetLastError = true)]
        public static extern int IERegCreateKeyEx(
                    //int hKey,
                    string lpSubKey,
                    uint Reserved,
                    string lpClass,
                    RegOption dwOptions,
                    RegSAM samDesired,
                    SECURITY_ATTRIBUTES lpSecurityAttributes,
                    out int phkResult,
                    out RegResult lpdwDisposition);
        // IE Reg set value C++
        //HRESULT IERegSetValueEx(      
        //  LPCWSTR lpSubKey,     
        //  LPCWSTR lpValueName,     
        //  DWORD Reserved,     
        //  DWORD dwType,     
        //  const BYTE *lpData,     
        //  DWORD cbData );
        // IE Reg set value C#
        [DllImport("ieframe.dll", SetLastError = true)]
        public static extern int IERegSetValueEx(
            String hKey,
            [MarshalAs(UnmanagedType.LPStr)] string lpValueName,
            uint Reserved,
            Microsoft.Win32.RegistryValueKind dwType,
            [MarshalAs(UnmanagedType.LPStr)] string lpData,
            int cbData);
    /// <summary>   
    /// Protected mode IE reg functions.   
    /// </summary>   
    public static class ProtectedModeHelper
        public static uint SetRegKey(string strKey, void KeyValue)
            int hresult = ProtectedMode.IERegCreateKeyEx(
                            strKey, //name of a subkey that this function opens or creates with medium integrity
                            ?,      //in (string)| lpValueName : The name of the value to be set. For more information, see Registry Element Size Limits.
                            0,      //in | reserved : This parameter is reserved and must be zero.
                            ?,      //in dwtype (RegistryValueKind) : The type of data pointed to by the lpData parameter. 
                                    //For a list of the possible types, see Registry Value Types. 
                            ref KeyValue, //lpData (string) : The data to be stored. 
                            ?       // in (int)| cbData :The size of the information pointed to by 
                                    //the lpData parameter, in BYTE. If the data is of type 
                                    //REG_SZ, REG_EXPAND_SZ, or REG_MULTI_SZ, cbData must include 
                                    //the size of the terminating NULL character or characters. 
                return ?; 
        public static int CreateRegKey(string strKey)
            int hresult = ProtectedMode.IERegCreateKeyEx(
                            strKey, //in (uint)| lpSubKey : The name of a lpSubKey that this function opens or 
                                    //creates with medium integrity. This parameter cannot be NULL
                            0,      //in | reserved : This parameter is reserved and must be zero.
                            ?,      //in (string)| lpClass : The class (object type) of this key. 
                                    //This parameter is used for both local and remote 
                                    //registry keys. It may be ignored, or it can be NULL. 
                            ?,      //in (RegOption)| dwOptions : A full list of options can be found in RegCreateKeyEx.
                            ?,      //in | (RegSAM) samDesired: A mask that specifies the access rights for the key. 
                            ?,      //in (SECURITY_ATTRIBUTES)| lpSecurityAttributes : A pointer to a SECURITY_ATTRIBUTES structure 
                                    //that determines whether the returned handle can be inherited by 
                                    //child processes. If lpSecurityAttributes is NULL, the handle 
                                    //cannot be inherited. 
                            ref hresult,  //out (int)| phkResult: A pointer to a variable that receives a handle to the 
                                    //opened or created key. If the key is not one of the predefined registry keys,
                                    //call the RegCloseKey function after you have finished using the handle.
                            ?);      //out (RegResult): lpdwDisposition :A pointer to a variable that receives one of the following disposition values. 
                                    //The key did not exist and was created.
                                    //The key existed and was simply opened without being changed.
            return hresult; 

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 2 Answers and 9 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 9 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros