⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 registry.c

📁 ReactOS是一些高手根据Windows XP的内核编写出的类XP。内核实现机理和API函数调用几乎相同。甚至可以兼容XP的程序。喜欢研究系统内核的人可以看一看。
💻 C
📖 第 1 页 / 共 2 页
字号:
/*
 *  FreeLoader
 *
 *  Copyright (C) 2001, 2002  Eric Kohl
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <freeldr.h>
#include <debug.h>

static FRLDRHKEY RootKey;


VOID
RegInitializeRegistry (VOID)
{
#if 0
  FRLDRHKEY TestKey;
  WCHAR szTestString[] = L"TestString";
#endif

  /* Create root key */
  RootKey = (FRLDRHKEY) MmAllocateMemory (sizeof(KEY));

  InitializeListHead (&RootKey->SubKeyList);
  InitializeListHead (&RootKey->ValueList);
  InitializeListHead (&RootKey->KeyList);

  RootKey->SubKeyCount = 0;
  RootKey->ValueCount = 0;

  RootKey->NameSize = 4;
  RootKey->Name = MmAllocateMemory (4);
  wcscpy (RootKey->Name, L"\\");

  RootKey->DataType = 0;
  RootKey->DataSize = 0;
  RootKey->Data = NULL;

  /* Create 'SYSTEM' key */
  RegCreateKey (RootKey,
		L"Registry\\Machine\\SYSTEM",
		NULL);

  /* Create 'HARDWARE' key */
  RegCreateKey (RootKey,
		L"Registry\\Machine\\HARDWARE",
		NULL);

  /* Create 'HARDWARE\DESCRIPTION' key */
  RegCreateKey (RootKey,
		L"Registry\\Machine\\HARDWARE\\DESCRIPTION",
		NULL);

  /* Create 'HARDWARE\DEVICEMAP' key */
  RegCreateKey (RootKey,
		L"Registry\\Machine\\HARDWARE\\DEVICEMAP",
		NULL);

  /* Create 'HARDWARE\RESOURCEMAP' key */
  RegCreateKey (RootKey,
		L"Registry\\Machine\\HARDWARE\\RESOURCEMAP",
		NULL);

/* Testcode */
#if 0
  RegCreateKey (RootKey,
		L"Registry\\Machine\\HARDWARE\\DESCRIPTION\\TestKey",
		&TestKey);

  RegSetValue (TestKey,
	       L"TestValue",
	       REG_SZ,
	       szTestString,
	       sizeof(szTestString));
#endif
}


LONG
RegInitCurrentControlSet(BOOLEAN LastKnownGood)
{
  WCHAR ControlSetKeyName[80];
  FRLDRHKEY SelectKey;
  FRLDRHKEY SystemKey;
  FRLDRHKEY ControlSetKey;
  FRLDRHKEY LinkKey;
  ULONG CurrentSet = 0;
  ULONG DefaultSet = 0;
  ULONG LastKnownGoodSet = 0;
  ULONG DataSize;
  LONG Error;

  Error = RegOpenKey(NULL,
		     L"\\Registry\\Machine\\SYSTEM\\Select",
		     &SelectKey);
  if (Error != ERROR_SUCCESS)
    {
      DbgPrint((DPRINT_REGISTRY, "RegOpenKey() failed (Error %u)\n", (int)Error));
      return(Error);
    }

  DataSize = sizeof(ULONG);
  Error = RegQueryValue(SelectKey,
			L"Default",
			NULL,
			(PUCHAR)&DefaultSet,
			&DataSize);
  if (Error != ERROR_SUCCESS)
    {
      DbgPrint((DPRINT_REGISTRY, "RegQueryValue('Default') failed (Error %u)\n", (int)Error));
      return(Error);
    }

  DataSize = sizeof(ULONG);
  Error = RegQueryValue(SelectKey,
			L"LastKnownGood",
			NULL,
			(PUCHAR)&LastKnownGoodSet,
			&DataSize);
  if (Error != ERROR_SUCCESS)
    {
      DbgPrint((DPRINT_REGISTRY, "RegQueryValue('Default') failed (Error %u)\n", (int)Error));
      return(Error);
    }

  CurrentSet = (LastKnownGood == TRUE) ? LastKnownGoodSet : DefaultSet;
  wcscpy(ControlSetKeyName, L"ControlSet");
  switch(CurrentSet)
    {
      case 1:
	wcscat(ControlSetKeyName, L"001");
	break;
      case 2:
	wcscat(ControlSetKeyName, L"002");
	break;
      case 3:
	wcscat(ControlSetKeyName, L"003");
	break;
      case 4:
	wcscat(ControlSetKeyName, L"004");
	break;
      case 5:
	wcscat(ControlSetKeyName, L"005");
	break;
    }

  Error = RegOpenKey(NULL,
		     L"\\Registry\\Machine\\SYSTEM",
		     &SystemKey);
  if (Error != ERROR_SUCCESS)
    {
      DbgPrint((DPRINT_REGISTRY, "RegOpenKey(SystemKey) failed (Error %u)\n", (int)Error));
      return(Error);
    }

  Error = RegOpenKey(SystemKey,
		     ControlSetKeyName,
		     &ControlSetKey);
  if (Error != ERROR_SUCCESS)
    {
      DbgPrint((DPRINT_REGISTRY, "RegOpenKey(ControlSetKey) failed (Error %u)\n", (int)Error));
      return(Error);
    }

  Error = RegCreateKey(SystemKey,
		       L"CurrentControlSet",
		       &LinkKey);
  if (Error != ERROR_SUCCESS)
    {
      DbgPrint((DPRINT_REGISTRY, "RegCreateKey(LinkKey) failed (Error %u)\n", (int)Error));
      return(Error);
    }

  Error = RegSetValue(LinkKey,
		      NULL,
		      REG_LINK,
		      (PCHAR)&ControlSetKey,
		      sizeof(PVOID));
  if (Error != ERROR_SUCCESS)
    {
      DbgPrint((DPRINT_REGISTRY, "RegSetValue(LinkKey) failed (Error %u)\n", (int)Error));
      return(Error);
    }

  return(ERROR_SUCCESS);
}


LONG
RegCreateKey(FRLDRHKEY ParentKey,
	     PCWSTR KeyName,
	     PFRLDRHKEY Key)
{
  PLIST_ENTRY Ptr;
  FRLDRHKEY SearchKey = NULL;
  FRLDRHKEY CurrentKey;
  FRLDRHKEY NewKey;
  PWCHAR p;
  PCWSTR name;
  int subkeyLength;
  int stringLength;
  ULONG NameSize;
  int CmpResult;

  DbgPrint((DPRINT_REGISTRY, "KeyName '%S'\n", KeyName));

  if (*KeyName == L'\\')
    {
      KeyName++;
      CurrentKey = RootKey;
    }
  else if (ParentKey == NULL)
    {
      CurrentKey = RootKey;
    }
  else
    {
      CurrentKey = ParentKey;
    }

  /* Check whether current key is a link */
  if (CurrentKey->DataType == REG_LINK)
    {
      CurrentKey = (FRLDRHKEY)CurrentKey->Data;
    }

  while (*KeyName != 0)
    {
      DbgPrint((DPRINT_REGISTRY, "KeyName '%S'\n", KeyName));

      if (*KeyName == L'\\')
	KeyName++;
      p = wcschr(KeyName, L'\\');
      if ((p != NULL) && (p != KeyName))
	{
	  subkeyLength = p - KeyName;
	  stringLength = subkeyLength + 1;
	  name = KeyName;
	}
      else
	{
	  subkeyLength = wcslen(KeyName);
	  stringLength = subkeyLength;
	  name = KeyName;
	}
      NameSize = (subkeyLength + 1) * sizeof(WCHAR);
      
      Ptr = CurrentKey->SubKeyList.Flink;
      CmpResult = 1;
      while (Ptr != &CurrentKey->SubKeyList)
	{
	  DbgPrint((DPRINT_REGISTRY, "Ptr 0x%x\n", Ptr));

	  SearchKey = CONTAINING_RECORD(Ptr,
					KEY,
					KeyList);
	  DbgPrint((DPRINT_REGISTRY, "SearchKey 0x%x\n", SearchKey));
	  DbgPrint((DPRINT_REGISTRY, "Searching '%S'\n", SearchKey->Name));
	  CmpResult = _wcsnicmp(SearchKey->Name, name, subkeyLength);
	  if (CmpResult == 0 && SearchKey->NameSize == NameSize)	  
	    break;
	  else if (CmpResult == -1)
	    break;

	  Ptr = Ptr->Flink;
	}

      if (CmpResult != 0)
	{
	  /* no key found -> create new subkey */
	  NewKey = (FRLDRHKEY)MmAllocateMemory(sizeof(KEY));
	  if (NewKey == NULL)
	    return(ERROR_OUTOFMEMORY);

	  InitializeListHead(&NewKey->SubKeyList);
	  InitializeListHead(&NewKey->ValueList);

	  NewKey->SubKeyCount = 0;
	  NewKey->ValueCount = 0;

	  NewKey->DataType = 0;
	  NewKey->DataSize = 0;
	  NewKey->Data = NULL;

	  InsertTailList(Ptr, &NewKey->KeyList);
	  CurrentKey->SubKeyCount++;

	  NewKey->NameSize = NameSize;
	  NewKey->Name = (PWCHAR)MmAllocateMemory(NewKey->NameSize);
	  if (NewKey->Name == NULL)
	    return(ERROR_OUTOFMEMORY);
	  memcpy(NewKey->Name, name, NewKey->NameSize - sizeof(WCHAR));
	  NewKey->Name[subkeyLength] = 0;

	  DbgPrint((DPRINT_REGISTRY, "NewKey 0x%x\n", NewKey));
	  DbgPrint((DPRINT_REGISTRY, "NewKey '%S'  Length %d\n", NewKey->Name, NewKey->NameSize));

	  CurrentKey = NewKey;
	}
      else
	{
	  CurrentKey = SearchKey;

	  /* Check whether current key is a link */
	  if (CurrentKey->DataType == REG_LINK)
	    {
	      CurrentKey = (FRLDRHKEY)CurrentKey->Data;
	    }
	}

      KeyName = KeyName + stringLength;
    }

  if (Key != NULL)
    *Key = CurrentKey;

  return(ERROR_SUCCESS);
}


LONG
RegDeleteKey(FRLDRHKEY Key,
	     PCWSTR Name)
{


  if (wcschr(Name, L'\\') != NULL)
    return(ERROR_INVALID_PARAMETER);



  return(ERROR_SUCCESS);
}


LONG
RegEnumKey(FRLDRHKEY Key,
	   ULONG Index,
	   PWCHAR Name,
	   ULONG* NameSize)
{
  PLIST_ENTRY Ptr;
  FRLDRHKEY SearchKey;
  ULONG Count = 0;
  ULONG Size;

  Ptr = Key->SubKeyList.Flink;
  while (Ptr != &Key->SubKeyList)
    {
      if (Index == Count)
	break;

      Count++;
      Ptr = Ptr->Flink;
    }

  if (Ptr == &Key->SubKeyList)
    return(ERROR_NO_MORE_ITEMS);

  SearchKey = CONTAINING_RECORD(Ptr,
				KEY,
				KeyList);

  DbgPrint((DPRINT_REGISTRY, "Name '%S'  Length %d\n", SearchKey->Name, SearchKey->NameSize));

  Size = min(SearchKey->NameSize, *NameSize);
  *NameSize = Size;
  memcpy(Name, SearchKey->Name, Size);

  return(ERROR_SUCCESS);
}


LONG
RegOpenKey(FRLDRHKEY ParentKey,
	   PCWSTR KeyName,
	   PFRLDRHKEY Key)
{
  PLIST_ENTRY Ptr;
  FRLDRHKEY SearchKey = NULL;
  FRLDRHKEY CurrentKey;
  PWCHAR p;
  PCWSTR name;
  int subkeyLength;
  int stringLength;
  ULONG NameSize;

  DbgPrint((DPRINT_REGISTRY, "KeyName '%S'\n", KeyName));

  *Key = NULL;

  if (*KeyName == L'\\')
    {
      KeyName++;
      CurrentKey = RootKey;
    }
  else if (ParentKey == NULL)
    {
      CurrentKey = RootKey;
    }
  else
    {
      CurrentKey = ParentKey;
    }

  /* Check whether current key is a link */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -