Win32 Registry api

Part of StandardLibraryCategory

Description

Reads int and string from Notepad registry entries. Creates, writes, and reads int and string registry entries for dsource app.

Example

/*
 *  Copyright (C) 2004 by Digital Mars, www.digitalmars.com
 *  Written by Lynn Allan by adapting code by Walter Bright
 *
 *  This software is provided 'as-is', without any express or implied
 *  warranty. In no event will the authors be held liable for any damages
 *  arising from the use of this software.
 *
 *  Permission is granted to anyone to use this software for any purpose,
 *  including commercial applications, and to alter it and redistribute it
 *  freely, subject to the following restrictions:
 *
 *  o  The origin of this software must not be misrepresented; you must not
 *     claim that you wrote the original software. If you use this software
 *     in a product, an acknowledgment in the product documentation would be
 *     appreciated but is not required.
 *  o  Altered source versions must be plainly marked as such, and must not
 *     be misrepresented as being the original software.
 *  o  This notice may not be removed or altered from any source
 *     distribution.
 */

/* Compile with:
 * dmd test.d advapi32.lib 
 */

import std.stdio;
import std.string;
import std.c.windows.windows;

extern (Windows)   // Not defined in windows.d

{
LONG RegQueryValueExA(HKEY hkey, 
                      LPCTSTR keyName, 
                      LPDWORD reservedNull,
                      LPDWORD type, 
                      void*   data,
                      LPDWORD len);
}

void main ()
{
  ReadNotepadKeys();
  WriteAppKeys();
}

void ReadNotepadKeys()
{
  HKEY hkey;
  LONG result;
  DWORD dwType;
  const int BUF_SZ = 40;
  DWORD dwLen = BUF_SZ;
  char[BUF_SZ] regBuf;
  int weight;

  result = RegOpenKeyA(HKEY_CURRENT_USER, 
                       "Software\\Microsoft\\Notepad", &hkey);
  writefln("OpenKey Result: ", result);
  assert(result == std.c.windows.windows.ERROR_SUCCESS);

  result = RegQueryValueExA(hkey, 
                            cast(char*)"lfFaceName", 
                            null, 
                            &dwType, 
                            cast(void*)regBuf, 
                            &dwLen);
  writefln("Query String Result: ", result, "  Len: ", dwLen);
  assert(result == ERROR_SUCCESS);
  writefln("RegBuf: [", regBuf[0..dwLen-1], "]");

  dwLen = 4;
  result = RegQueryValueExA(hkey, 
                            cast(char*)"lfWeight", 
                            null, 
                            &dwType, 
                            cast(void*)&weight, 
                            &dwLen);
  writefln("Query DWORD Result: ", result, "  Weight: [", weight, "]");
  assert(result == ERROR_SUCCESS);

  result = RegCloseKey(hkey);
  writefln("CloseKey Result: ", result);
  assert(result == ERROR_SUCCESS);
}


void WriteAppKeys()
{
  HKEY hkey;
  LONG result;
  DWORD dwType;
  const int BUF_SZ = 4;
  DWORD dwLen = BUF_SZ;
  char[BUF_SZ] regBuf;
  DWORD displosition;
  int param = 0x1001;

  result = RegCreateKeyExA(HKEY_CURRENT_USER, 
                           "Software\\dsource\\Test\\Settings", 
                           cast(uint)null,
                           null,
                           REG_OPTION_NON_VOLATILE,
                           KEY_ALL_ACCESS,
                           null,
                           &hkey,
                           &displosition);
  writefln("CreateKey Result: ", result);
  assert(result == std.c.windows.windows.ERROR_SUCCESS);

  dwLen = 4;
  result = RegSetValueExA(hkey, 
                          cast(char*)"Param", 
                          0, 

                          REG_DWORD, 
                          cast(BYTE*)&param, 
                          dwLen);
  writefln("Set DWORD Result: ", result);
  assert(result == ERROR_SUCCESS);

  char[] regGreeting = "Hello and Goodbye";
  result = RegSetValueExA(hkey, 
                          cast(char*)"Greeting", 
                          0, 

                          REG_SZ, 
                          cast(BYTE*)toStringz(regGreeting), 
                          regGreeting.length);
  writefln("Set DWORD Result: ", result);
  assert(result == ERROR_SUCCESS);

  result = RegCloseKey(hkey);
  writefln("CloseKey Result: ", result);
  assert(result == ERROR_SUCCESS);

  result = RegOpenKeyA(HKEY_CURRENT_USER, 
                       "Software\\dsource\\Test\\Settings", &hkey);
  writefln("OpenKey Result: ", result);
  assert(result == std.c.windows.windows.ERROR_SUCCESS);

  dwLen = 4;
  int check;
  result = RegQueryValueExA(hkey, 
                            cast(char*)"Param", 
                            null, 
                            &dwType, 
                            cast(void*)&check, 
                            &dwLen); 
  writefln("Query DWORD Result: ", result, "  Check: [", check, "]");
  assert(result == ERROR_SUCCESS);
  assert(check == param);

  result = RegCloseKey(hkey);
  writefln("CloseKey Result: ", result);
  assert(result == ERROR_SUCCESS);
}

Source

Posted by Lynn Allan
Date/Time Tue Sep 28, 2004 9:20 pm