private void Do_RegEnumKeys(IntPtr pRootKey, RegistryEnumKeyInfo KeyInfo)
        {
            if (pRootKey == IntPtr.Zero)
                return;

            List<RegistryEnumKeyInfo> keys = null;
            List<RegistryValueInfo> values = null;

            int ret = RegistryInteropWrapper.ApiRegQueryInfoEx(
                                    plugin.handle.Handle,
                                    pRootKey,
                                    out KeyInfo.sSubKeyCount,
                                    out KeyInfo.sValueCount);

            if (KeyInfo.sSubKeyCount != 0)
            {
                ret = RegistryInteropWrapper.ApiRegEnumKeyEx(
                                        plugin.handle.Handle,
                                        pRootKey,
                                        KeyInfo.sSubKeyCount,
                                        out keys);

            }

            if (KeyInfo.sValueCount != 0)
            {
                ret = RegistryInteropWrapper.ApiRegEnumValues(
                                       plugin.handle.Handle,
                                       pRootKey,
                                       KeyInfo.sValueCount,
                                       out values);
            }

            EnumChildNodes(keys, values);
		}
        private void Do_CreateKey(RegistryEnumKeyInfo keyInfo, LACTreeNode node)
        {
            if (keyInfo != null)
            {
                RegistryAddSubKeyDlg addDlg = new RegistryAddSubKeyDlg(string.Empty, true, keyInfo);
                if (addDlg.ShowDialog(this) == DialogResult.OK)
                {
                    IntPtr phkResult = IntPtr.Zero;
                    int ret = RegistryInteropWrapper.ApiRegCreateKeyEx(plugin.handle.Handle, keyInfo.pKey, Marshal.StringToHGlobalUni(addDlg.KeyName), out phkResult);
                    if (ret == 0)
                    {
                        if (phkResult != IntPtr.Zero)
                            RegistryInteropWrapper.ApiRegCloseKey(plugin.handle.Handle, phkResult);

                        if (!keyInfo.sKeyname.Trim().Equals(Properties.Resources.HKEY_THIS_MACHINE))
							RegistryInteropWrapper.ApiRegCloseKey(plugin.handle.Handle, keyInfo.pKey);

                        node.IsModified = true;
                        node.sc.ShowControl(node);
                    }
                    else
                    {
                        container.ShowError(this, "Registry cannot create the key with the name specified.\n It is because of either the name exists or parent key info is wrong");
                    }
                }
            }
        }
        private void Do_MoveSubKeys(RegistryEnumKeyInfo oldKey, IntPtr newKey)
        {
            List<RegistryValueInfo> values = new List<RegistryValueInfo>();
            List<RegistryEnumKeyInfo> keys = new List<RegistryEnumKeyInfo>();

            // Copy the Value names to newKey
            if (oldKey.sValueCount != 0)
            {
                RegistryInteropWrapper.ApiRegEnumValues(
                                        plugin.handle.Handle,
                                        oldKey.pKey,
                                        oldKey.sValueCount,
                                        out values);

                if (values != null && values.Count != 0)
                {
                    foreach (RegistryValueInfo value in values)
                    {
						value.pParentKey = oldKey.pKey;

                        RegistryInteropWrapper.GetRegGetValueW(plugin.handle.Handle, value);

                        RegistryInteropWrapper.ApiRegSetValueEx(
                                        plugin.handle.Handle,
                                        newKey,
                                        value.pValueName,
                                        (uint)value.pType,
                                        value.bDataBuf as byte[]);
                    }
                }
            }

            // Copy All the subkeys of oldKey to newKey
            if (oldKey.sSubKeyCount != 0)
            {
                RegistryInteropWrapper.ApiRegEnumKeyEx(
                                        plugin.handle.Handle,
                                        oldKey.pKey,
                                        oldKey.sSubKeyCount,
                                        out keys);

                if (keys != null && keys.Count != 0)
                {
                    IntPtr phkResult;
                    foreach (RegistryEnumKeyInfo key in keys)
                    {
                        phkResult = IntPtr.Zero;
                        RegistryInteropWrapper.ApiRegCreateKeyEx(
                                        plugin.handle.Handle,
                                        newKey,
                                        Marshal.StringToHGlobalUni(key.sKeyname.LastIndexOf(@"\") < 0 ? key.sKeyname : key.sKeyname.Substring(key.sKeyname.LastIndexOf(@"\")+1)),
                                        out phkResult);

                        if (key.pKey == IntPtr.Zero)
                        {
                            RegistryInteropWrapper.ApiRegOpenKeyExW(
                                                  plugin.handle.Handle,
                                                  plugin.pRootHandle,
                                                  key.sKeyname,
                                                  out key.pKey);
                        }

                        RegistryInteropWrapper.ApiRegQueryInfoEx(
                                   plugin.handle.Handle,
                                   key.pKey,
                                   out key.sSubKeyCount,
                                   out key.sValueCount);

                        Do_MoveSubKeys(key, phkResult);

                        if (phkResult != IntPtr.Zero)
                            RegistryInteropWrapper.ApiRegCloseKey(
                                        plugin.handle.Handle, phkResult);

                        if (key.pKey != IntPtr.Zero)
                            RegistryInteropWrapper.ApiRegCloseKey(plugin.handle.Handle, key.pKey);
                    }
                }
            }
        }
        private void Do_CloseRegTree(RegistryEnumKeyInfo keyInfo)
        {
            if (keyInfo != null)
            {
                if (keyInfo.sSubKeyCount != 0)
                {
                    List<RegistryEnumKeyInfo> keys = new List<RegistryEnumKeyInfo>();

                    RegistryInteropWrapper.ApiRegEnumKeyEx(plugin.handle.Handle,
                                                keyInfo.pKey,
                                                keyInfo.sSubKeyCount,
                                                out keys);

                    if (keys != null && keys.Count != 0)
                    {
                        foreach (RegistryEnumKeyInfo key in keys)
                        {
                            if (key.pKey == IntPtr.Zero)
                            {
                                RegistryInteropWrapper.ApiRegOpenKeyExW(
                                                    plugin.handle.Handle,
                                                    key.pRootKey,
                                                    key.sKeyname,
                                                    out key.pKey);
                            }

                            RegistryInteropWrapper.ApiRegQueryInfoEx(
                                                plugin.handle.Handle,
                                                key.pKey,
                                                out key.sSubKeyCount,
                                                out key.sValueCount);

                            if (keyInfo.sSubKeyCount != 0)
                                Do_DeleteTree(key);

                            RegistryInteropWrapper.ApiRegDeleteKey(
                                        plugin.handle.Handle,
                                        key.pRootKey,
                                        key.pKey,
                                        key.sKeyname);
                        }
                    }
                }
            }
        }
        public override void Refresh()
        {
            base.Refresh();
            HKEY hKey = HKEY.HEKY_CURRENT_USER;
            IntPtr pRootKey = IntPtr.Zero;
            RegistryKey sSubKey = null;
            RegistryEnumKeyInfo KeyInfo = null;

            if (plugin.IsConnectionSuccess)
            {
                if (Configurations.currentPlatform == LikewiseTargetPlatform.Windows)
                {
                    if (!plugin.Do_LogonUserSet())
                    {
                        Logger.Log("RegistryEditorPage.Refresh(): Failed to authenticate the specified user");
                        return;
                    }
                }
                else
                {
                    if (plugin != null && (plugin.handle == null || plugin.handle.Handle == IntPtr.Zero))
                    {
                        Logger.Log("Failed to get the Registry handle");
                        return;
                    }
                }
            }
            else
                return;

            switch (nodeType)
            {
                case RegistryViewerPlugin.NodeType.HKEY_CURRENT_CONFIG:

                    hKey = HKEY.HKEY_CURRENT_CONFIG;
                    plugin.RegRootKeySelected = treeNode.Text.Trim();
                    RegistryInteropWrapperWindows.Win32RegOpenRemoteBaseKey(
                                                        hKey,
                                                        out sSubKey);
                    break;

                case RegistryViewerPlugin.NodeType.HKEY_CURRENT_USER:

                    hKey = HKEY.HEKY_CURRENT_USER;
                    plugin.RegRootKeySelected = treeNode.Text.Trim();
                    RegistryInteropWrapperWindows.Win32RegOpenRemoteBaseKey(hKey,
                                                        out sSubKey);

                    //IntPtr pSD = RegistryInteropWrapperWindows.ApiRegGetKeySecurity(
                    //            RegistryInteropWrapperWindows.GetRegistryHive(HKEY.HEKY_CURRENT_USER),
                    //            "AppEvents1");
                    //SecurityDescriptor SecurityDescriptor = new SecurityDescriptor();
                    //SecurityDescriptorWrapper.ReadSecurityDescriptor(pSD, ref SecurityDescriptor);

                    //if (SecurityDescriptor.IsAccessDenied)
                    //{
                    //    MessageBox.Show("Access Denied", "Likewise Administrative Console", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    //    return;
                    //}
                    //PermissionsControlDlg dlg = new PermissionsControlDlg(SecurityDescriptor, "AppEvent1");
                    //if (dlg.ShowDialog(this) == DialogResult.OK)
                    //{
                    //    RegistryInteropWrapperWindows.ApiRegSetKeySecurity(
                    //            RegistryInteropWrapperWindows.GetRegistryHive(HKEY.HEKY_CURRENT_USER),
                    //            "AppEvents1",
                    //            SecurityDescriptor.pSecurityDescriptorOut);
                    //}
                    break;

                case RegistryViewerPlugin.NodeType.HKEY_LOCAL_MACHINE:

                    hKey = HKEY.HKEY_LOCAL_MACHINE;
                    plugin.RegRootKeySelected = treeNode.Text.Trim();
                    RegistryInteropWrapperWindows.Win32RegOpenRemoteBaseKey(hKey,
                                                        out sSubKey);
                    break;

                case RegistryViewerPlugin.NodeType.HKEY_USERS:

                    hKey = HKEY.HKEY_USERS;
                    plugin.RegRootKeySelected = treeNode.Text.Trim();
                    RegistryInteropWrapperWindows.Win32RegOpenRemoteBaseKey(hKey,
                                                        out sSubKey);
                    break;

                case RegistryViewerPlugin.NodeType.HKEY_CLASSES_ROOT:

                    hKey = HKEY.HKEY_CLASSES_ROOT;
                    plugin.RegRootKeySelected = treeNode.Text.Trim();
                    RegistryInteropWrapperWindows.Win32RegOpenRemoteBaseKey(hKey,
                                                        out sSubKey);
                    break;

                case RegistryViewerPlugin.NodeType.HKEY_LIKEWISE:
                    plugin.RegRootKeySelected = treeNode.Text.Trim();
                    hKey = HKEY.HKEY_LIKEWISE;
                    KeyInfo = treeNode.Tag as RegistryEnumKeyInfo;

					if(plugin.pRootHandle != IntPtr.Zero)
						RegistryInteropWrapper.ApiRegCloseKey(plugin.handle.Handle, plugin.pRootHandle);

                    RegistryInteropWrapper.ApiRegOpenKeyExW(plugin.handle.Handle,
                                                            IntPtr.Zero,
                                                            plugin.RegRootKeySelected,
                                                            out pRootKey);

					plugin.pRootHandle = pRootKey;
                    KeyInfo = new RegistryEnumKeyInfo();
                    KeyInfo.pRootKey = pRootKey;
                    KeyInfo.sKeyname = plugin.RegRootKeySelected;
                    treeNode.Tag = KeyInfo;
                    KeyInfo.OrigKey = plugin.RegRootKeySelected;

                    break;

                case RegistryViewerPlugin.NodeType.HKEY_LIKEWISE_SUBKEY:
                    hKey = HKEY.HKEY_LIKEWISE_SUBKEY;
                    KeyInfo = treeNode.Tag as RegistryEnumKeyInfo;
                    if (KeyInfo != null)
                    {
                        RegistryInteropWrapper.ApiRegOpenKeyExW(plugin.handle.Handle,
                                                            plugin.pRootHandle,
                                                            KeyInfo.sKeyname,
                                                            out pRootKey);
                        treeNode.Tag = KeyInfo;
                    }
					else
					{
						MessageBox.Show("Invalid key ");
					}
                    break;

                case RegistryViewerPlugin.NodeType.HKEY_SUBKEY:
                    SubKeyInfo subkeyInfo = treeNode.Tag as SubKeyInfo;
                    if (subkeyInfo != null)
                    {
                        sSubKey = subkeyInfo.sSubKey;
                        hKey = subkeyInfo.hKey;
                    }
                    break;

                case RegistryViewerPlugin.NodeType.NONE:
                default:
                    //For default displays
                    ListViewItem[] itemlist = new ListViewItem[treeNode.Nodes.Count];
                    int index = 0;

                    foreach (LACTreeNode node in treeNode.Nodes)
                    {
                        ListViewItem item = new ListViewItem(new string[] { node.Text });
                        itemlist[index] = item;
                        index++;
                    }
                    if (itemlist.Length != 0)
                        lvRegistryPage.Items.AddRange(itemlist);
                    break;
            }

            if (Configurations.currentPlatform == LikewiseTargetPlatform.Windows)
            {
                EnumChildNodes(sSubKey, hKey);
                plugin.Do_LogonUserHandleClose();
            }
            else
            {
                Do_RegEnumKeys(pRootKey, KeyInfo);

				if(nodeType != RegistryViewerPlugin.NodeType.HKEY_LIKEWISE && pRootKey != IntPtr.Zero)
					RegistryInteropWrapper.ApiRegCloseKey(plugin.handle.Handle, pRootKey);
            }
        }