Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. RegOpenKey and similar .... marshal/P-Invoke??

RegOpenKey and similar .... marshal/P-Invoke??

Scheduled Pinned Locked Moved C#
csharpquestionwindows-adminjson
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Paolo Ponzano
    wrote on last edited by
    #1

    hello, I just want to write my own Regedit in C#, just start developening it for windows xp and then move to Windows CE .NET, but remaining in the contest of the thread, I saw that Registry's API are not implemented as method, so I think I shall use P-invoke?? my question is : in C I've got LONG RegOpenKey( HKEY hKey, LPCTSTR lpSubKey, PHKEY phkResult ); I can do: [DllImport("advapi32.dll")] public static extern int RegOpenKeyEx(System.UInt32 hKey, String lpSubKey, System.UInt32 ulOptions, System.UInt32 samDesired, System.UInt32 phkResult); 1) I'm not sure if I've setted types correctly 2) how I define in C# code variable for phkResult? If I define it as UInt32 I can't do as I do in C &phResult... Thanks in advance, Paolo

    N A 2 Replies Last reply
    0
    • P Paolo Ponzano

      hello, I just want to write my own Regedit in C#, just start developening it for windows xp and then move to Windows CE .NET, but remaining in the contest of the thread, I saw that Registry's API are not implemented as method, so I think I shall use P-invoke?? my question is : in C I've got LONG RegOpenKey( HKEY hKey, LPCTSTR lpSubKey, PHKEY phkResult ); I can do: [DllImport("advapi32.dll")] public static extern int RegOpenKeyEx(System.UInt32 hKey, String lpSubKey, System.UInt32 ulOptions, System.UInt32 samDesired, System.UInt32 phkResult); 1) I'm not sure if I've setted types correctly 2) how I define in C# code variable for phkResult? If I define it as UInt32 I can't do as I do in C &phResult... Thanks in advance, Paolo

      N Offline
      N Offline
      Nick Parker
      wrote on last edited by
      #2

      Try the following:

      [DllImport("advapi32.dll", CharSet=CharSet.Unicode, EntryPoint="RegOpenKeyEx")]
      static extern long RegOpenKeyEx(
      IntPtr hKey,
      String subKey,
      uint options,
      int sam,
      out IntPtr phkResult);

      - Nick Parker
      My Blog | My Articles

      1 Reply Last reply
      0
      • P Paolo Ponzano

        hello, I just want to write my own Regedit in C#, just start developening it for windows xp and then move to Windows CE .NET, but remaining in the contest of the thread, I saw that Registry's API are not implemented as method, so I think I shall use P-invoke?? my question is : in C I've got LONG RegOpenKey( HKEY hKey, LPCTSTR lpSubKey, PHKEY phkResult ); I can do: [DllImport("advapi32.dll")] public static extern int RegOpenKeyEx(System.UInt32 hKey, String lpSubKey, System.UInt32 ulOptions, System.UInt32 samDesired, System.UInt32 phkResult); 1) I'm not sure if I've setted types correctly 2) how I define in C# code variable for phkResult? If I define it as UInt32 I can't do as I do in C &phResult... Thanks in advance, Paolo

        A Offline
        A Offline
        Ami Bar
        wrote on last edited by
        #3

        .NET already implements the registry API's. Check the Microsoft.Win32.Registry class.

        P 1 Reply Last reply
        0
        • A Ami Bar

          .NET already implements the registry API's. Check the Microsoft.Win32.Registry class.

          P Offline
          P Offline
          Paolo Ponzano
          wrote on last edited by
          #4

          thanks you opened my eyes =), I was just playing around with that class but a new problem came at my hands...... if I want to use a recorsive function to process keys how I do that?? In Win32 API, I've RegEnumerateKey/Values here the most similar thing is String [] subkey = rk.GetSubKeyNames();, so I do private void AddRegNode(TreeNode node, RegistryKey root) { RegistryKey rk = root; String [] subkey = rk.GetSubKeyNames(); if(subkey.Length==0) return; //if I've no subkeys so I've got only leap foreach (String s in subkey) { String livia = rk.Name + "\\" + s; RegistryKey temp = Registry.CurrentUser.CreateSubKey(livia); TreeNode newNode = new TreeNode(s); this.Tree.SelectedNode.Nodes.Add(newNode); AddRegNode(newNode, temp); but doing so it doesn't work, first all the nodes are added to selected node, but I'l fix it, second it desn't go deeper ( HKEY_CURRENT_USER\AppEvents\EventLabels is max deep it reaches) thanks Paolo

          A 1 Reply Last reply
          0
          • P Paolo Ponzano

            thanks you opened my eyes =), I was just playing around with that class but a new problem came at my hands...... if I want to use a recorsive function to process keys how I do that?? In Win32 API, I've RegEnumerateKey/Values here the most similar thing is String [] subkey = rk.GetSubKeyNames();, so I do private void AddRegNode(TreeNode node, RegistryKey root) { RegistryKey rk = root; String [] subkey = rk.GetSubKeyNames(); if(subkey.Length==0) return; //if I've no subkeys so I've got only leap foreach (String s in subkey) { String livia = rk.Name + "\\" + s; RegistryKey temp = Registry.CurrentUser.CreateSubKey(livia); TreeNode newNode = new TreeNode(s); this.Tree.SelectedNode.Nodes.Add(newNode); AddRegNode(newNode, temp); but doing so it doesn't work, first all the nodes are added to selected node, but I'l fix it, second it desn't go deeper ( HKEY_CURRENT_USER\AppEvents\EventLabels is max deep it reaches) thanks Paolo

            A Offline
            A Offline
            Ami Bar
            wrote on last edited by
            #5

            I wrote a simple window application that fills a treeview with registry keys. The code fills the tree with the registry keys (no values)

            private void FillNode(TreeView treeView, TreeNode parentNode, RegistryKey key)
            {
            int index = key.Name.LastIndexOf("\\");
            string keyname = key.Name;
            if (index > 0)
            {
            keyname = key.Name.Remove(0, index + 1);
            }

            TreeNode node = null;
            
            if (null == parentNode)
            {
                node = treeView.Nodes.Add(keyname);
            }
            else
            {
                node = parentNode.Nodes.Add(keyname);
            }
            string\[\] subkeyNames = key.GetSubKeyNames();
            RegistryKey subkey = null;
            foreach (string subkeyName in subkeyNames)
            {
                try
                {
                    subkey = key.OpenSubKey(subkeyName);
                }
                catch (SecurityException e)
                {
                    // Ignore keys that we don't have security to read them
                    continue;
                }
            
                FillNode(treeView, node, subkey);
                subkey.Close();
            }
            

            }

            To use it call:

            FillNode(treeView1, null, Microsoft.Win32.Registry.CurrentUser);
            

            Where treeView1 is your TreeView control. Notes: 1. If you read the keys them use OpenSubKey and not CreateSubKey 2. Make sure you catch exception. There are keys you cannot open. 3. Don't forget to close keys that you open. Good luck, Ami

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups