RegOpenKey and similar .... marshal/P-Invoke??
-
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 -
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, PaoloTry 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 -
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 -
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 doprivate 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 -
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 doprivate 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 PaoloI 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