Добрый день.Возникла проблема при работе с реестром. Выдается ошибка о переполнении стека.Помогите, пожалуйста!
Код:
#include <windows.h>
#include <commctrl.h>
#include <winreg.h>
#pragma comment(lib,"comctl32.lib")
#define hKeyMin 0x80000000
#define hKeyMax 0x80000006
HINSTANCE hInst;
HWND hTreeChild;
TV_INSERTSTRUCT InsertStruct;
LRESULT CALLBACK RegistryWndProc(HWND,UINT,UINT,LONG);
void FillTree(HWND,HTREEITEM);
void FillBranch(ULONG,DWORD,HWND,HTREEITEM);
void CALLBACK FillSubBranch(HKEY,char*,HWND,HTREEITEM);
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpszCmdParam, int nCmdShow)
{
HWND hWnd;
WNDCLASS WndClass;
MSG Msg;
wchar_t szClassName[]=L"Registry";
hInst=hInstance;
WndClass.style=CS_HREDRAW|CS_VREDRAW;
WndClass.lpfnWndProc=RegistryWndProc;
WndClass.cbClsExtra=0;
WndClass.cbWndExtra=0;
WndClass.hInstance=hInstance;
WndClass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
WndClass.hCursor=LoadCursor(NULL,IDC_ARROW);
WndClass.hbrBackground=(HBRUSH) GetStockObject(WHITE_BRUSH);
WndClass.lpszMenuName=L"";
WndClass.lpszClassName= szClassName;
if(!RegisterClass(&WndClass))
{
MessageBox(NULL,L"Cannot register class",L"Error",MB_OK);
return 0;
}
hWnd=CreateWindow(szClassName,L"Eighth Example",
WS_POPUPWINDOW|WS_VISIBLE|WS_CAPTION,
100,100,300,400,
NULL,NULL,
hInst,NULL);
if(!hWnd)
{
MessageBox(NULL,L"Cannot create window",L"Error",MB_OK);
return 0;
}
//
ShowWindow(hWnd,nCmdShow);
UpdateWindow(hWnd);
while(GetMessage(&Msg,NULL,0,0))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}
LRESULT CALLBACK RegistryWndProc(HWND hWnd,UINT Message,
UINT wParam,LONG lParam)
{
RECT Rect;
static HWND hTreeChild;
static HTREEITEM hParentItem;
switch(Message)
{
case WM_CREATE:
GetClientRect(hWnd,&Rect);
hTreeChild=CreateWindow(WC_TREEVIEW,L"",
WS_VISIBLE|WS_TABSTOP|WS_CHILD|
TVS_HASLINES|TVS_LINESATROOT|
TVS_HASBUTTONS|WS_DLGFRAME,
0,0,Rect.right,Rect.bottom,
hWnd,NULL,
hInst,NULL);
InsertStruct.item.mask=TVIF_TEXT;
InsertStruct.item.hItem=NULL;
InsertStruct.item.pszText=L"Registry Keys";
InsertStruct.item.cchTextMax=14;
InsertStruct.hParent=TVI_ROOT;
InsertStruct.hInsertAfter=TVI_LAST;
hParentItem=TreeView_InsertItem(hTreeChild,&InsertStruct);
FillTree(hTreeChild,hParentItem);
TreeView_Expand(hTreeChild,hParentItem,TVE_EXPAND);
TreeView_SelectItem(hTreeChild,hParentItem);
return 0;
case WM_SIZE:
MoveWindow(hTreeChild,0,0,LOWORD(lParam),HIWORD(lParam),
TRUE);
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hWnd,Message,wParam,lParam);
}
void FillTree(HWND hTreeWnd, HTREEITEM hParentItem)
{
ULONG i;
TV_ITEM Item;
TV_INSERTSTRUCT InsertStruct;
LPWSTR lpszKeys[7]={L"HKEY_CLASSES_ROOT",
L"HKEY_CURRENT_USER",
L"HKEY_LOCAL_MACHINE",
L"HKEY_USERS",
L"HKEY_PERFORMANCE_DATA",
L"HKEY_CURRENT_CONFIG",
L"HKEY_DYN_DATA"};
wchar_t cClass[80]=L"";
DWORD dwSize=80,
dwSubKeys,
dwMaxLength,
dwMaxClass,
dwValues,
dwMaxValue,
dwMaxData,
dwSec;
FILETIME ftFileTime;
HTREEITEM hNewParentItem;
for(i=hKeyMin; i<=hKeyMax; i++)
{
if(ERROR_SUCCESS==RegQueryInfoKey((HKEY)i,
cClass,&dwSize,
NULL,
&dwSubKeys,&dwMaxLength,&dwMaxClass,
&dwValues,&dwMaxValue,&dwMaxData,&dwSec,
&ftFileTime))
{
Item.mask=TVIF_TEXT;
Item.pszText=lpszKeys[i-hKeyMin];
InsertStruct.item=Item;
InsertStruct.hParent=hParentItem;
hNewParentItem=TreeView_InsertItem(hTreeWnd,&InsertStruct);
FillBranch(i,dwSubKeys,hTreeWnd,hNewParentItem);
}
}
}
void FillBranch(ULONG i,DWORD dwSubKeys, HWND hTreeWnd,
HTREEITEM hNewParentItem)
{
unsigned int j;
DWORD dwClassNameSize=80;
char cClassName[80]="";
if(dwSubKeys==0)
return;
else
for(j=0; j<dwSubKeys; j++)
{
RegEnumKey((HKEY)i,(DWORD)j,(LPWSTR)cClassName,dwClassNameSize);
dwClassNameSize=80;
FillSubBranch((HKEY)i, cClassName,hTreeWnd,hNewParentItem);
}
}
void CALLBACK FillSubBranch(HKEY hKey, char* cClassName,
HWND hTreeWnd,HTREEITEM hParentItem)
{
HKEY hNewKey;
char cClass[80], cNewClass[80];
DWORD dwClassSize=80,dwSK,j;
TV_ITEM Item;
TV_INSERTSTRUCT InsertStruct;
HTREEITEM hNewParentItem;
Item.mask=TVIF_TEXT;
Item.pszText=(LPWSTR)cClassName;
InsertStruct.item=Item;
InsertStruct.hParent=hParentItem;
hNewParentItem=TreeView_InsertItem(hTreeWnd,&InsertStruct);//ЗАПАДЛО
RegOpenKey(hNewKey,(LPCWSTR)cClassName,&hNewKey);
RegQueryInfoKey(hNewKey,(LPWSTR)cClass,&dwClassSize,NULL,&dwSK,NULL,
NULL,NULL,NULL,NULL,NULL,NULL);
dwClassSize=80;
if(dwSK!=0)
for(j=0;j<dwSK; j++)
{
RegEnumKey(hNewKey,j,(LPWSTR)cNewClass,dwClassSize);
FillSubBranch(hNewKey,cNewClass,hTreeWnd,
hNewParentItem);
}
RegCloseKey(hNewKey);
}