i have created the registry editor in wince when i am going to add new registry it gets added in registry and show in treeview but problem is that when i am going to add child on unexpanded registry or lower most child ie which is not having any child the registry key added by twice means it show tow key with same name but when restart exe it shows only one key Kindly help me
Following is the code
#include <windows.h>
#include <windowsx.h>
#include <commctrl.h>
#include <string.h>
#include <stdlib.h>
#include <tchar.h>
#include "global.h"
#include "regtree.h"
#include "reglist.h"
#include "find.h"
#include "resource.h"
#include "winuser.h"
#include "tree.h"
#define WXUSINGDLL
HANDLE hInst;
HANDLE hAccel;
HWND h_wnd;
HWND hDlg;
HWND h_wndDlg;
HWND hWndCB;
HWND hListWnd;
HWND hTreeWnd;
HIMAGELIST hTreeImages;
HIMAGELIST hListImages;
TCHAR sz_subKey [MAX_PATH*8];
HKEY h_rootKey;
RECT rect;
LPTV_INSERTSTRUCT lpis;
HTREEITEM nTreeRootItem;
TCHAR szCurrSubKey [MAX_PATH*8];
HKEY hCurrRootKey;
HKEY hKeyN2;
DWORD dwDisp;
int rc;
PBYTE pBuff;
DWORD dwValType, dwDSize;
PBYTE pbData;
DWORD dwRegVal = 0x1234;
HTREEITEM h_item ;
BOOL Main_OnInitDialog (HWND hwnd, HWND h_wndFocus, LPARAM l_param);
void Main_OnCommand (HWND h_wnd, int n_id, HWND h_wndCtl, UINT n_codeNotify);
int Main_OnSize (HWND h_wnd, WPARAM fw_sizeType, int n_width, int n_height);
LRESULT Main_OnNotify (HWND hwnd, int idCtl, LPNMHDR pnmh);
INT_PTR CALLBACK _addkey (HWND h_wndAdd, UINT u_msg, WPARAM w_param, LPARAM l_param);
INT_PTR CALLBACK _string(HWND h_String, UINT u_msg, WPARAM w_param, LPARAM l_param);
INT_PTR CALLBACK _about(HWND h_Modify, UINT u_msg, WPARAM w_param, LPARAM l_param);
static BOOL WINAPI _mainDialogHook (HWND h_wndDlg, UINT u_msg, WPARAM w_param, LPARAM l_param);
static BOOL WINAPI _aboutDialogHook (HWND h_wndDlg, UINT u_msg, WPARAM w_param, LPARAM l_param);
static BOOLEAN isParent (HKEY h_parent, TCHAR * psz_key);
int WINAPI WinMain (HINSTANCE h_instance, HINSTANCE h_prevInstance,
#ifdef _WIN32_WCE
LPWSTR lp_cmdLine,
#else
LPSTR lp_cmdLine,
#endif
int n_cmdShow)
{
HWND h_wnd;
MSG msg;
UNREFERENCED_PARAMETER (n_cmdShow);
UNREFERENCED_PARAMETER (lp_cmdLine);
UNREFERENCED_PARAMETER (h_prevInstance);
hInst = h_instance;
InitCommonControls ();
hAccel = LoadAccelerators(hInst, MAKEINTRESOURCE(IDR_ACCELERATOR));
h_wnd = CreateDialog ((HANDLE)h_instance, MAKEINTRESOURCE(IDD_DIALOG_MAIN), NULL, (DLGPROC)_mainDialogHook);
if (h_wnd == NULL)
MessageBox (NULL, TEXT("Can't create main window"), TEXT("Error"), MB_OK|MB_ICONSTOP);
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(h_wnd, hAccel, &msg))
{
TranslateMessage((LPMSG)&msg);
DispatchMessage((LPMSG)&msg);
}
}
return 0;
}
static BOOL WINAPI _mainDialogHook ( HWND h_wndDlg, UINT u_msg, WPARAM w_param, LPARAM l_param)
{
switch (u_msg)
{
HANDLE_MESS (h_wndDlg, WM_INITDIALOG, w_param, l_param, Main_OnInitDialog);
break;
break;
HANDLE_MESS (h_wndDlg, WM_NOTIFY , w_param, l_param, Main_OnNotify);
break;
HANDLE_MESS (h_wndDlg, WM_COMMAND , w_param, l_param, Main_OnCommand);
break;
#ifdef _WIN32_WCE
HANDLE_MESS (h_wndDlg, WM_SIZE , w_param, l_param, Main_OnSize);
#endif
case WM_QUIT:
{
DefWindowProc(h_wndDlg,u_msg,w_param, l_param);
}
}
return 0;
}
BOOL Main_OnInitDialog (HWND h_wndDlg, HWND h_wndFocus, LPARAM l_param)
{
LV_COLUMN col;
HICON h_icon;
int idx;
#ifdef _WIN32_WCE
TBBUTTON buttons[] =
{
{0, 0, TBSTATE_ENABLED, TBSTYLE_SEP, 0, 0, 0, -1},
{STD_FIND , ID_MENUITEM_FIND, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0, 0, -1},
{0, 0, TBSTATE_ENABLED, TBSTYLE_SEP, 0, 0, 0, -1},
{STD_DELETE, ID_MENUITEM_DEL , TBSTATE_ENABLED, TBSTYLE_BUTTON, 0, 0, 0, -1}
};
#endif
SendMessage (h_wndDlg, WM_SETTEXT, 0, (LPARAM)(TEXT("Registry Editor")));
#ifdef _WIN32_WCE
hWndCB = CommandBar_Create (hInst, h_wndDlg, 1);
if (hWndCB == NULL)
MessageBox (h_wndDlg, TEXT("Can't create menu: step 1"), TEXT("Error"), MB_OK|MB_ICONSTOP);
if (CommandBar_AddBitmap (hWndCB, HINST_COMMCTRL, IDB_STD_SMALL_COLOR, 15, 16, 16)==-1)
MessageBox (h_wndDlg, TEXT("Can't create menu: step 2"), TEXT("Error"), MB_OK|MB_ICONSTOP);
if (CommandBar_AddButtons (hWndCB, sizeof(buttons)/sizeof(TBBUTTON),buttons) == FALSE)
MessageBox (h_wndDlg, TEXT("Can't create menu: step 3"), TEXT("Error"), MB_OK|MB_ICONSTOP);
if (CommandBar_InsertMenubar (hWndCB, hInst, IDR_MENU_MAIN, 0)==FALSE)
MessageBox (h_wndDlg, TEXT("Can't create menu: step 4"), TEXT("Error"), MB_OK|MB_ICONSTOP);
#endif
hTreeWnd = GetDlgItem(h_wndDlg, IDC_TREE);
if ((hTreeImages = ImageList_Create(16, 16, ILC_COLOR4, 3, 0)) != NULL)
{
HBITMAP h_bmpTree = LoadBitmap (hInst, MAKEINTRESOURCE(IDB_TREE_SYMBOLS));
idx = ImageList_Add (hTreeImages, h_bmpTree, NULL);
DeleteObject (h_bmpTree);
#ifdef __DEBUG__
idx = ImageList_GetImageCount(hTreeImages);
#endif
TreeView_SetImageList (hTreeWnd, hTreeImages, TVSIL_NORMAL);
}
nTreeRootItem = RegTree_LoadRoot (hTreeWnd);
TreeView_Expand (hTreeWnd, nTreeRootItem, TVE_EXPAND);
hListWnd = GetDlgItem (h_wndDlg, IDC_LIST);
col.mask = LVCF_TEXT|LVCF_WIDTH|LVCF_SUBITEM;
col.cx = 100;
col.pszText = TEXT ("Name");
col.iSubItem = 0;
ListView_InsertColumn (hListWnd, 0, &col);
col.pszText = TEXT ("Data");
col.cx = 250;
col.iSubItem = 1;
ListView_InsertColumn (hListWnd, 1, &col);
if ((hListImages = ImageList_Create(16, 16, ILC_COLOR4, 4, 0)) != NULL)
{
HBITMAP h_bmpList = LoadBitmap (hInst, MAKEINTRESOURCE(IDB_LIST_SYMBOLS));
idx = ImageList_Add (hListImages, h_bmpList, NULL);
DeleteObject (h_bmpList);
ListView_SetImageList (hListWnd, hListImages, LVSIL_SMALL);
}
h_icon = (HICON)LoadImage (hInst, MAKEINTRESOURCE(IDI_ICON_MAIN),
IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);
PostMessage (h_wndDlg, WM_SETICON, (WPARAM)FALSE, (LPARAM)h_icon);
DeleteObject ((HGDIOBJ)h_icon);
{
#ifndef _WIN32_WCE
ShowWindow (h_wndDlg, SW_MAXIMIZE);
#else
int n_width = GetSystemMetrics (SM_CXSCREEN);
int n_height = GetSystemMetrics (SM_CYSCREEN);
POINT point = {n_width - 1, n_height - 1};
HWND h_taskBar = WindowFromPoint (point);
GetWindowRect (h_taskBar, &rect);
SetWindowPos (h_wndDlg, HWND_TOP, 0, 0, n_width, n_height-(rect.bottom-rect.top)+1, SWP_SHOWWINDOW);
#endif
}
return 0;
}
int Main_OnSize (HWND h_wndDlg, WPARAM fw_sizeType, int n_width, int n_height)
{
#ifdef _WIN32_WCE
HWND h_wndItem;
RECT rect;
int n_topOffset = 0;
int n_bottomOffset = 0;
GetWindowRect (hWndCB, &rect);
n_topOffset = rect.bottom-rect.top;
SetWindowPos (hWndCB, h_wndDlg, 0, 0, n_width, n_topOffset, SWP_NOZORDER|SWP_NOMOVE);
CommandBar_AddAdornments (hWndCB, 0, 0);
h_wndItem = GetDlgItem(h_wndDlg, IDC_PATH);
GetWindowRect (h_wndItem, &rect);
n_bottomOffset = rect.bottom-rect.top;
SetWindowPos (h_wndItem, h_wndDlg, 0, n_height-n_bottomOffset,
n_width, n_bottomOffset, SWP_NOZORDER);
GetWindowRect (hTreeWnd, &rect);
SetWindowPos (hTreeWnd, h_wndDlg,
0, n_topOffset+1,
rect.right-rect.left,
n_height - n_bottomOffset - n_topOffset, SWP_NOZORDER);
SetWindowPos (hListWnd, h_wndDlg,
rect.right-rect.left+3, n_topOffset+1,
n_width-(rect.right-rect.left)-5,
n_height - n_bottomOffset - n_topOffset, SWP_NOZORDER);
#endif
return 0;
}
void Main_OnCommand (HWND h_wnd, int n_id, HWND h_wndCtl, UINT n_codeNotify)
{
switch (n_id)
{
case IDCANCEL:
case ID_MENUITEM_EXIT:
EndDialog (h_wnd, 0);
DestroyMenu(NULL);
DestroyIcon(NULL);
DestroyAcceleratorTable(NULL);
CommandBar_Destroy (hWndCB);
DestroyWindow (h_wnd);
DestroyWindow(NULL);
break;
case ID_MENUITEM_DEL: case ID_EDIT_MODIFY:
{
HWND h_foc = GetFocus();
if (h_foc == hListWnd || h_foc == hTreeWnd)
{
if (n_id == ID_MENUITEM_DEL)
SendMessage (h_foc, WM_KEYDOWN, VK_DELETE, 0);
else if (h_foc == hListWnd)
RegList_EditSelectedValue (hListWnd, hCurrRootKey, szCurrSubKey);
}
}
break;
case ID_MENUITEM_FIND:
case ID_HELP_REFRESH:
DialogBox (hInst, MAKEINTRESOURCEW(IDD_DIALOG_ABOUT),
h_wnd, _about);
break;
case ID_MENUITEM_CREATE_KEY:
{
DialogBox(hInst, MAKEINTRESOURCEW(IDD_ADD_KEY), h_wnd,_addkey);
}
break;
case ID_MENUITEM_CREATE_STRING:
{
DialogBox(hInst, MAKEINTRESOURCEW(IDD_STRING_NEW), h_wnd,_string);
}
break;
case IDD_REFRESH:
break;
}
}
LRESULT Main_OnNotify(HWND h_wnd, int idCtl, LPNMHDR pnmh)
{
static char bIs_TVN_ITEMEXPANDING = FALSE;
static char bIs_TVN_SELCHANGED = FALSE;
switch (pnmh->code)
{
case TVN_ITEMEXPANDING:
{
LPNM_TREEVIEW pnmtv;
if (bIs_TVN_ITEMEXPANDING == TRUE)
return 0;
pnmtv = (LPNM_TREEVIEW) pnmh;
if (pnmtv->itemNew.hItem == nTreeRootItem)
return 0;
bIs_TVN_ITEMEXPANDING = TRUE;
if (pnmtv->action == TVE_COLLAPSE)
{
pnmtv->itemNew.mask = TVIF_IMAGE | TVIF_SELECTEDIMAGE;
TreeView_GetItem (pnmh->hwndFrom, &(pnmtv->itemNew));
pnmtv->itemNew.iImage = nIMAGE_CLOSED;
pnmtv->itemNew.iSelectedImage = nIMAGE_CLOSED;
TreeView_SetItem (pnmh->hwndFrom, &(pnmtv->itemNew));
}
else
{
pnmtv->itemNew.mask = TVIF_IMAGE | TVIF_SELECTEDIMAGE;
TreeView_GetItem (pnmh->hwndFrom, &(pnmtv->itemNew));
pnmtv->itemNew.iImage = nIMAGE_OPEN;
pnmtv->itemNew.iSelectedImage = nIMAGE_OPEN;
TreeView_SetItem (pnmh->hwndFrom, &(pnmtv->itemNew));
if ( ! (pnmtv->itemNew.state & TVIS_EXPANDEDONCE) )
{
SetCursor (LoadCursor(NULL, IDC_WAIT));
RegTree_BuildKey (pnmtv->hdr.hwndFrom, pnmtv->itemNew, &h_rootKey, sz_subKey);
RegTree_LoadKey (pnmtv->hdr.hwndFrom, (HTREEITEM)pnmtv->itemNew.hItem, h_rootKey, sz_subKey);
SetCursor (NULL);
}
}
bIs_TVN_ITEMEXPANDING = FALSE;
return 0;
}
break;
case TVN_SELCHANGED:
{
LPNM_TREEVIEW pnmtv = (LPNM_TREEVIEW) pnmh;
if (pnmtv->itemNew.hItem == pnmtv->itemOld.hItem)
return 0;
if (bIs_TVN_SELCHANGED == TRUE)
return 0;
bIs_TVN_SELCHANGED = TRUE;
SetCursor (LoadCursor(NULL, IDC_WAIT));
RegTree_BuildKey (pnmtv->hdr.hwndFrom, pnmtv->itemNew, &h_rootKey, sz_subKey);
RegList_LoadKey (hListWnd, h_rootKey, sz_subKey);
lstrcpy (szCurrSubKey, sz_subKey);
hCurrRootKey = h_rootKey;
SetCursor (NULL);
RegTree_GetRootPath (h_rootKey, sz_subKey);
if (h_rootKey)
lstrcat (sz_subKey, szCurrSubKey);
SetDlgItemText (h_wnd, IDC_PATH, sz_subKey);
bIs_TVN_SELCHANGED = FALSE;
}
break;
case TVN_KEYDOWN:
{
TV_KEYDOWN * pnkd = (TV_KEYDOWN FAR *) pnmh;
if (pnkd->wVKey == VK_DELETE)
{
HTREEITEM h_item = TreeView_GetSelection (pnmh->hwndFrom);
if (h_item == NULL)
break;
if (MessageBox (h_wnd, TEXT("Are you SURE you want to delete this KEY?"),
TEXT("Deleting"), MB_YESNO|MB_ICONQUESTION)==IDYES)
{
if (RegDeleteKey (hCurrRootKey, szCurrSubKey) != ERROR_SUCCESS)
{
MessageBox (h_wnd, TEXT("Can't delete the KEY."),
TEXT("Error"), MB_OK|MB_ICONEXCLAMATION);
break;
}
TreeView_DeleteItem (pnmh->hwndFrom, h_item);
}
}
}
break;
case LVN_KEYDOWN:
{
LV_KEYDOWN * pnkd = (LV_KEYDOWN FAR *) pnmh;
#if 0 //DEBUG
TCHAR ppp [80];
wsprintf (ppp, TEXT("0x%X"), pnkd->wVKey);
MessageBox (h_wnd, ppp, TEXT("Virtual Key"), MB_OK);
#endif
if (pnkd->wVKey == VK_DELETE)
{
UINT is_selected = ListView_GetSelectedCount (pnmh->hwndFrom);
if (is_selected)
{
int n_item = ListView_GetNextItem (pnmh->hwndFrom, -1, LVNI_ALL|LVNI_SELECTED);
if (n_item >= 0)
{
if (MessageBox (h_wnd, TEXT("Are you sure you want to delete this value?"),
TEXT("Deleting"), MB_YESNO|MB_ICONQUESTION)==IDYES)
{
HKEY h_key;
LV_ITEM listItem;
memset (&listItem, 0, sizeof (listItem));
listItem.pszText = sz_subKey;
listItem.cchTextMax = MAX_PATH*7;
listItem.iItem = n_item;
listItem.mask = LVIF_TEXT;
if (ListView_GetItem (pnmh->hwndFrom, &listItem) == FALSE)
break;
if (RegOpenKeyEx (hCurrRootKey, szCurrSubKey, 0, KEY_WRITE, &h_key) != ERROR_SUCCESS)
break;
if (RegDeleteValue (h_key, listItem.pszText) == ERROR_SUCCESS)
ListView_DeleteItem (pnmh->hwndFrom, n_item);
RegCloseKey (h_key);
}
}
}
}
else if (pnkd->wVKey == VK_RETURN)
RegList_EditSelectedValue (pnmh->hwndFrom, hCurrRootKey, szCurrSubKey);
}
break;
case NM_DBLCLK:
if (pnmh->hwndFrom == hListWnd)
RegList_EditSelectedValue (hListWnd, hCurrRootKey, szCurrSubKey);
break;
}
return 0;
}
int len;
TCHAR *szbuf;
HWND hEditbox;
DWORD dwDisp;
HKEY hKey;
LPRECT rectl;
HKEY h_key;
BOOLEAN bool_isSubKeys = 0;
TCHAR psz_nextKey [MAX_PATH+1];
DWORD n_keyNameSize;
DWORD dw_index;
DWORD retCode;
TCHAR pRevCurrSubKey [MAX_PATH*8];
INT_PTR CALLBACK _addkey (HWND h_wndAdd, UINT u_msg, WPARAM w_param, LPARAM l_param)
{
switch (u_msg)
{
case WM_COMMAND:
switch (GET_WM_COMMAND_ID(w_param,l_param))
{
case IDOK:
pRevCurrSubKey[2080]=szCurrSubKey[2080];
hEditbox = GetDlgItem(h_wndAdd, IDC_EDIT1);
len=GetWindowTextLength(hEditbox);
szbuf =(TCHAR*)GlobalAlloc(GPTR, len + 1);
GetWindowText(hEditbox, szbuf,len+1);
GetDlgItemText(hEditbox,IDC_EDIT1,(LPWSTR)szbuf, len + 1);
wcscat(szCurrSubKey,_T("\\"));
wcscat(szCurrSubKey, szbuf);
rc= RegCreateKeyEx(hCurrRootKey,szCurrSubKey,0,TEXT(""),REG_OPTION_NON_VOLATILE, KEY_WRITE|KEY_ALL_ACCESS,NULL,&hKey,&dwDisp);
if (rc == ERROR_SUCCESS)
{
MessageBox(NULL,TEXT("KEY REGISTERED"),TEXT("NOTICE"),MB_OK);
}
else
{
MessageBox(NULL,TEXT("Failed"),TEXT("ERROR"),MB_OK);
}
h_item=TreeView_GetSelection(hTreeWnd);
Tree_AddItem (hTreeWnd, szbuf, h_item,FALSE);
RegCloseKey(hKey);
RegCloseKey(h_key);
RegCloseKey(szCurrSubKey);
RegCloseKey(pRevCurrSubKey);
RegCloseKey(hCurrRootKey);
case IDCANCEL:
EndDialog (h_wndAdd, 0);
break;
}
break;
}
return 0;
}
int len1;
TCHAR *szbuf1;
HWND hEditbox1;
DWORD dwDisp;
HKEY hKeyN2;
LPRECT rectl;
INT_PTR CALLBACK _string(HWND h_String, UINT u_msg, WPARAM w_param, LPARAM l_param)
{
switch (u_msg)
{
case WM_COMMAND:
switch (GET_WM_COMMAND_ID(w_param,l_param))
{
case IDOK:
hEditbox1 = GetDlgItem(h_String, IDC_EDIT2);
len1=GetWindowTextLength(hEditbox1);
szbuf1 =(TCHAR*)GlobalAlloc(GPTR, len1 + 1);
GetWindowText(hEditbox1, szbuf1,len1+1);
GetDlgItemText(hEditbox1,IDC_EDIT2,(LPWSTR)szbuf1, len1 + 1);
rc = RegOpenKeyEx (hCurrRootKey,szCurrSubKey
, 0,
KEY_SET_VALUE, &hKeyN2);
if(rc != ERROR_SUCCESS)
{
MessageBox (NULL, TEXT("RegOpenKeyEx Failed"),
TEXT("Key not opened"), MB_OK);
return FALSE;
}
rc = RegSetValueEx (hKeyN2,szbuf1, 0,
REG_DWORD , (LPBYTE)&dwRegVal,
sizeof(dwRegVal));
if (rc != ERROR_SUCCESS)
{
MessageBox (NULL, TEXT("RegSetValueEx Failed"),
TEXT("Couldn't delete value"), MB_OK);
}
else
{
MessageBox (NULL, TEXT("String Name Added "),
TEXT("NOTICE"), MB_OK);
}
RegCloseKey( hKeyN2 );
case IDCANCEL:
EndDialog (h_String, 0);
break;
}
break;
}
return 0;
}
INT_PTR CALLBACK _about(HWND h_Modify, UINT u_msg, WPARAM w_param, LPARAM l_param)
{
switch (u_msg)
{
case WM_COMMAND:
switch (GET_WM_COMMAND_ID(w_param,l_param))
{
case IDOK:
case IDCANCEL:
EndDialog (h_Modify, 0);
break;
}
break;
}
return 0;
}
static BOOLEAN isParent (HKEY h_parent, TCHAR * psz_key)
{
DWORD n_numSubKeys;
DWORD n_maxSubKeyLen;
DWORD n_maxClassLen;
DWORD n_numValues;
DWORD n_maxValueNameLen;
HKEY h_key;
DWORD retCode = RegOpenKeyEx (h_parent, psz_key, 0, KEY_EXECUTE, &h_key);
if (retCode != ERROR_SUCCESS)
return FALSE;
n_numSubKeys = 0;
retCode = RegQueryInfoKey (h_key, NULL, NULL, NULL,
&n_numSubKeys, &n_maxSubKeyLen,
&n_maxClassLen,
&n_numValues, &n_maxValueNameLen, NULL,
NULL, NULL);
RegCloseKey (h_key);
return (n_numSubKeys != 0);
}
|