Thread: registry

  1. #1
    unregistered
    Guest

    registry

    Does anyone have a c source which they would be willing to share that enumerates and displays all the registry keys and values it contains?

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Sorry, dont have any code, but some helpfull functions to lookup on mSDN

    RegOpenKeyEx()
    RegQueryValueEx()
    RegEnumKeyEx()
    RegEnumValue()
    ..............................

    All the functions begin like that....so have a look at what you need

  3. #3
    Unregistered
    Guest

    Question

    /* RegKeysEnumed.c */
    /* A simple header control example. */

    #include <windows.h>
    #include <commctrl.h>
    #include <string.h>
    #include <stdio.h>
    #include "head.h"

    #define NUMCOLS 4
    #define DEFWIDTH 100
    #define MINWIDTH 10
    #define SPACING 8
    #define NUMENTRIES 6

    char *HSubKey;

    LRESULT CALLBACK WindowFunc(HWND, UINT, WPARAM, LPARAM);
    HWND InitHeader(HWND hParent);
    void InitDatabase(void);
    //long LastWriteTime;
    unsigned long datatype, datasize, RegType, RegEnumData, LastWriteTime;
    char *RegKeyClass;
    HKEY hRegKey;
    char szWinName[] = "MyWin"; /* name of window class */

    HINSTANCE hInst;
    HWND hHeadWnd;

    int HeaderHeight, Name;

    int columns[NUMCOLS] = {DEFWIDTH, DEFWIDTH,
    DEFWIDTH, DEFWIDTH};

    struct RegKeyDatabase {
    // char RegKey[80];
    char RegKey;
    char value[80];
    char date[80];
    // char othervalue[80];
    } data[NUMENTRIES];


    int WINAPI WinMain(HINSTANCE hThisInst, HINSTANCE hPrevInst,
    LPSTR lpszArgs, int nWinMode)
    {
    MSG msg;
    WNDCLASSEX wcl;
    HACCEL hAccel;
    HWND hwnd;
    INITCOMMONCONTROLSEX cc;

    /* Define a window class. */
    wcl.cbSize = sizeof(WNDCLASSEX);

    wcl.hInstance = hThisInst; /* handle to this instance */
    wcl.lpszClassName = szWinName; /* window class name */
    wcl.lpfnWndProc = WindowFunc; /* window function */
    wcl.style = 0; /* default style */

    wcl.hIcon = LoadIcon(NULL, IDI_APPLICATION); /* standard icon */
    wcl.hIconSm = LoadIcon(NULL, IDI_APPLICATION); /* small icon */
    wcl.hCursor = LoadCursor(NULL, IDC_ARROW); /* cursor style */

    wcl.lpszMenuName = "MyMenu"; /* main menu */
    wcl.cbClsExtra = 0; /* no extra */
    wcl.cbWndExtra = 0; /* information needed */

    /* Make the window white. */
    wcl.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);

    /* Register the window class. */
    if(!RegisterClassEx(&wcl)) return 0;

    /* Now that a window class has been registered, a window
    can be created. */
    hwnd = CreateWindow(
    szWinName, /* name of window class */
    "Registry Keys", /* title */
    WS_OVERLAPPEDWINDOW, /* standard window */
    CW_USEDEFAULT, /* X coordinate - let Windows decide */
    CW_USEDEFAULT, /* Y coordinate - let Windows decide */
    CW_USEDEFAULT, /* width - let Windows decide */
    CW_USEDEFAULT, /* height - let Windows decide */
    HWND_DESKTOP, /* no parent window */
    NULL, /* no override of class menu */
    hThisInst, /* handle of this instance of the program */
    NULL /* no additional arguments */
    );

    hInst = hThisInst; /* save the current instance handle */

    /* Load accelerators. */
    hAccel = LoadAccelerators(hThisInst, "MyMenu");

    /* Initialize the common controls. */
    cc.dwSize = sizeof(INITCOMMONCONTROLSEX);
    cc.dwICC = ICC_LISTVIEW_CLASSES;
    InitCommonControlsEx(&cc);

    /* Display the window. */
    ShowWindow(hwnd, nWinMode);
    UpdateWindow(hwnd);

    /* Create the message loop. */
    while(GetMessage(&msg, NULL, 0, 0))
    {
    if(!TranslateAccelerator(hwnd, hAccel, &msg)) {
    TranslateMessage(&msg); /* translate keyboard message */
    DispatchMessage(&msg); /* return control to Windows 98 */
    }
    }
    return msg.wParam;
    }

    /* This function is called by Windows 98 and is passed
    messages from the message queue.
    */
    LRESULT CALLBACK WindowFunc(HWND hwnd, UINT message,
    WPARAM wParam, LPARAM lParam)
    {
    int response;
    RECT rect;
    HDLAYOUT layout;
    WINDOWPOS winpos;
    NMHEADER *hdnptr;
    HDITEM *hdiptr;
    PAINTSTRUCT ps;
    TEXTMETRIC tm;
    SIZE size;

    char str[80];
    int i, j, ColStart, chrs;
    int entry;
    int linespacing;

    HDC hdc;
    INITCOMMONCONTROLSEX cc;

    switch(message) {
    case WM_INITDIALOG:
    /* Initialize common controls */
    cc.dwSize = sizeof(INITCOMMONCONTROLSEX);
    cc.dwICC = ICC_UPDOWN_CLASS;
    InitCommonControlsEx(&cc);
    int datasize = sizeof(DWORD);
    char datatype, *LWT, SubKey;
    // int Name;
    //
    FILETIME LastWriteTime;
    // RegQueryValueEx(HKEY_CURRENT_USER, "Software\\ODBC\\ODBC.INI",
    // NULL, &datatype, (LPBYTE) str, &datasize);

    RegEnumKeyEx(HKEY_CURRENT_USER,
    0,
    &datatype,
    &Name,
    NULL,
    RegKeyClass,
    &datasize,
    &LastWriteTime);
    // char *HSubKey;
    //
    RegOpenKeyEx(HKEY_CURRENT_USER,
    &SubKey,
    // hRegKey,
    0,
    KEY_ENUMERATE_SUB_KEYS,
    &HSubKey);

    RegEnumValue(HKEY_CURRENT_USER,
    0,
    &datatype,
    &Name,
    NULL,
    &RegType, //REG_EXPAND_SZ
    &RegEnumData,
    &datasize);

    // char RegKeyValue = -->datatype;
    // break;
    case WM_CREATE:
    hHeadWnd = InitHeader(hwnd);
    InitDatabase();
    break;
    case WM_COMMAND:

    // int datasize = sizeof(DWORD);
    // char datatype, *LWT, SubKey;
    // int Name;
    //
    // FILETIME LastWriteTime;
    // RegQueryValueEx(HKEY_CURRENT_USER, "Software\\ODBC\\ODBC.INI",
    // NULL, &datatype, (LPBYTE) str, &datasize);

    RegEnumKeyEx(HKEY_CURRENT_USER,
    0,
    &datatype,
    &Name,
    NULL,
    RegKeyClass,
    &datasize,
    &LastWriteTime);
    // char *HSubKey;
    //
    RegOpenKeyEx(HKEY_CURRENT_USER,
    &SubKey,
    // hRegKey,
    0,
    KEY_ENUMERATE_SUB_KEYS,
    &HSubKey);

    RegEnumValue(HKEY_CURRENT_USER,
    1,
    &datatype,
    &Name,
    NULL,
    &RegType, //REG_EXPAND_SZ
    &RegEnumData,
    &datasize);


    switch(LOWORD(wParam)) {
    case IDM_EXIT:
    response = MessageBox(hwnd, "Quit the Program?",
    "Exit", MB_YESNO);
    if(response == IDYES) PostQuitMessage(0);
    break;
    case IDM_HELP:
    MessageBox(hwnd, "Try resizing the header.",
    "Help", MB_OK);
    break;
    }
    break;
    case WM_SIZE:
    /* Resize the header control when its parent window
    changes size. */
    GetClientRect(hwnd, &rect);
    layout.prc = &rect;
    layout.pwpos = &winpos;
    Header_Layout(hHeadWnd, &layout);

    MoveWindow(hHeadWnd, winpos.x, winpos.y,
    winpos.cx, winpos.cy, 1);
    break;
    case WM_NOTIFY:
    if(LOWORD(wParam) == ID_HEADCONTROL) {
    hdnptr = (NMHEADER *) lParam;
    hdiptr = (HDITEM *) hdnptr->pitem;
    switch(hdnptr->hdr.code) {
    case HDN_TRACK: /* user changing column width */
    GetClientRect(hwnd, &rect);
    if(hdiptr->cxy < MINWIDTH) {
    hdiptr->cxy = MINWIDTH;
    columns[hdnptr->iItem] = MINWIDTH;
    }
    else
    columns[hdnptr->iItem] = hdiptr->cxy;
    rect.top = HeaderHeight;
    InvalidateRect(hwnd, &rect, 1);
    break;
    /* try handling other header control messages here */
    }
    }
    break;
    case WM_PAINT:
    hdc = BeginPaint(hwnd, &ps);

    GetTextMetrics(hdc, &tm);
    linespacing = tm.tmHeight + tm.tmInternalLeading;

    for(entry = 0; entry < NUMENTRIES; entry++) {
    ColStart = 0;
    for(i=0; i<NUMCOLS; i++) {
    switch(i) {
    // case 0: strcpy(str, data[entry].RegKey);
    break;
    case 1: strcpy(str, data[entry].value);
    break;
    case 2: strcpy(str, data[entry].date);
    break;
    // case 3: strcpy(str, data[entry].othervalue);
    // break;
    }

    GetTextExtentPoint32(hdc, str, strlen(str), &size);
    j = 2;
    while((columns[i]-SPACING) < size.cx) {
    chrs = columns[i] / tm.tmAveCharWidth;
    strcpy(&str[chrs-j], "...");
    GetTextExtentPoint32(hdc, str, strlen(str), &size);
    j++;
    }

    TextOut(hdc, ColStart+SPACING,
    HeaderHeight+(entry*linespacing),
    str, strlen(str));

    ColStart += columns[i];
    }
    }
    EndPaint(hwnd, &ps);
    break;
    case WM_DESTROY: /* terminate the program */
    PostQuitMessage(0);
    break;
    default:
    /* Let Windows 98 process any messages not specified in
    the preceding switch statement. */
    return DefWindowProc(hwnd, message, wParam, lParam);
    }
    return 0;
    }

    /* Initialize the header control. */
    HWND InitHeader(HWND hParent)
    {
    HWND hHeadWnd;
    RECT rect;
    HDLAYOUT layout;
    WINDOWPOS winpos;
    HDITEM hditem;

    GetClientRect(hParent, &rect);

    /* create the header control */
    hHeadWnd = CreateWindow(WC_HEADER, NULL,
    WS_CHILD | WS_BORDER,
    CW_USEDEFAULT, CW_USEDEFAULT,
    0, 0, hParent,
    (HMENU) ID_HEADCONTROL,
    hInst, NULL);

    /* get header control layout that will fit client area */
    layout.pwpos = &winpos;
    layout.prc = &rect;
    Header_Layout(hHeadWnd, &layout);

    /* dimension header to fit current size of client area */
    MoveWindow(hHeadWnd, winpos.x, winpos.y,
    winpos.cx, winpos.cy, 0);

    HeaderHeight = winpos.cy; /* save height of header */

    /* insert items into the header */
    hditem.mask = HDI_FORMAT | HDI_WIDTH | HDI_TEXT;
    hditem.pszText = "Registry Key";
    hditem.cchTextMax = strlen(hditem.pszText);
    hditem.cxy = DEFWIDTH;
    hditem.fmt = HDF_STRING | HDF_LEFT;
    Header_InsertItem(hHeadWnd, 0, &hditem);

    hditem.pszText = "Value";
    hditem.cchTextMax = strlen(hditem.pszText);
    Header_InsertItem(hHeadWnd, 1, &hditem);

    hditem.pszText = "Function";
    hditem.cchTextMax = strlen(hditem.pszText);
    Header_InsertItem(hHeadWnd, 2, &hditem);

    // hditem.pszText = "Future Use";
    // hditem.cchTextMax = strlen(hditem.pszText);
    // Header_InsertItem(hHeadWnd, 3, &hditem);

    ShowWindow(hHeadWnd, SW_SHOW); /* display the header control */

    return hHeadWnd;
    }

    /* Put Data. */
    void InitDatabase(void)
    {
    // hRegKey = "test";
    // hRegKey = &Name;
    //hRegKey = &datasize;
    data[0].RegKey = RegType;
    // strcpy(data[0].RegKey, hRegKey);
    // strcpy(data[0].value, "");
    // strcpy(data[0].date, "");
    // strcpy(data[0].othervalue, "");

    // strcpy(data[1].RegKey, ".");
    // strcpy(data[1].value, "");
    // strcpy(data[1].date, "");
    // strcpy(data[1].othervalue, "");


    }

    The only results I get are ZZu'y" (not exact - ' & " should be above the u and y respectively-but best I could do - tried to attach screen shot, but not successful)

    Do you know how to get the values into a form (character I suppose) that can be reported?

  4. #4
    Unregistered
    Guest
    Ok, here is a shorter version of the above
    it is a console application
    Please help.
    As you will see, I don't get very good or much results from the Registry.

    /* RegKeysEnumedCon.c*/

    #include "windows.h"
    #include "commctrl.h"
    #include "string.h"
    #include "stdio.h"

    #define DELAYMAX 999
    #define MSGSIZE 80

    char *HSubKey;

    void InitDatabase(void);
    //long LastWriteTime;
    unsigned long datasize, RegType, RegEnumData, LastWriteTime, result;
    unsigned long datatype;
    char *RegKeyClass, SubKey;
    HKEY hRegKey;
    long nRegKey, server;
    long delay;
    int NUMENTIRES;
    HINSTANCE hInst;
    HWND hHeadWnd;

    int Name;

    int main(void)
    {

    FILE *fptr;
    char name[40];
    fptr = fopen("textfile.txt", "w");
    HDC hdc;
    //
    INITCOMMONCONTROLSEX cc;
    datasize = sizeof(MSGSIZE);
    //
    RegOpenKeyEx(HKEY_CURRENT_USER,
    "Software\\HSPrograms\\Screensaver",
    0,
    KEY_READ,
    &result);
    //
    datasize = sizeof(MSGSIZE);
    RegQueryValueEx(hRegKey,
    "delay",
    NULL,
    &datatype,
    (LPBYTE) &delay,
    &datasize);
    //
    fprintf(fptr, "RegQueryValueExDatatype=%s\n", &datatype);
    fprintf(fptr, "RegQueryValueExdatasize=%s\n", &datasize);
    fprintf(fptr, "RegQueryValueExdelay=%s\n", &delay);//
    //
    RegCloseKey(hRegKey);
    //
    RegOpenKeyEx(HKEY_CURRENT_USER,
    "Software\\HSPrograms\\Screensaver",
    0,
    KEY_READ,
    &result);
    //
    FILETIME LastWriteTime;
    RegEnumKeyEx(HKEY_CURRENT_USER,
    5,
    &SubKey,
    &Name,
    NULL,
    RegKeyClass,
    &datasize,
    &LastWriteTime);
    //
    fprintf(fptr, "RegEnumSubKey = %c\n", &SubKey);
    fprintf(fptr, "RegEnumKeyName = %c\n", &Name);
    fprintf(fptr, "RegEnumKeydatasize = %c\n", &datasize);
    fprintf(fptr, "RegEnumKeyLasWriteTime = %c\n", &LastWriteTime);
    //
    RegEnumValue(HKEY_CURRENT_USER,
    5,
    &SubKey,
    &Name,
    NULL,
    &RegType, //REG_EXPAND_SZ
    &RegEnumData,
    &datasize);
    //
    fprintf(fptr, "RegEnumValueSubKey=%s\n", &SubKey);
    fprintf(fptr, "RegEnumValueName=%s\n", &Name);
    fprintf(fptr, "RegEnumValueRegType=%s\n", &RegType);
    fprintf(fptr, "RegEnumValueData=%s\n", &RegEnumData);
    fprintf(fptr, "RegEnumValuedatasize=%s\n", &datasize);

    //
    RegCloseKey(hRegKey);
    fclose(fptr);
    return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Registry, Regedit
    By franse in forum C++ Programming
    Replies: 21
    Last Post: 01-29-2009, 09:57 AM
  2. Registry HowTo
    By xxxrugby in forum C Programming
    Replies: 2
    Last Post: 04-10-2005, 10:44 AM
  3. Efficient registry use?
    By bennyandthejets in forum Windows Programming
    Replies: 6
    Last Post: 09-28-2003, 06:28 PM
  4. Registry
    By gvector1 in forum C# Programming
    Replies: 0
    Last Post: 07-30-2003, 04:02 PM
  5. Registry Access
    By ExDigit in forum Windows Programming
    Replies: 3
    Last Post: 01-04-2002, 04:02 AM