settings.c

来自「一个类似windows」· C语言 代码 · 共 816 行 · 第 1/2 页

C
816
字号
	{
	  if (!InfGetDataField(Context, 0, &KeyName))
	    {
	      /* FIXME: Handle error! */
	      DPRINT("InfGetDataField() failed\n");
	      return NULL;
	    }

	  DPRINT("Display key: %S\n", KeyName);
	  wcscpy(DisplayKey, KeyName);
	}
    }
  while (InfFindNextLine(Context, Context));
  InfFreeContext(Context);


  List = CreateGenericList();
  if (List == NULL)
    return NULL;

  if (!InfFindFirstLine (InfFile, L"Display", NULL, &Context))
    {
      DestroyGenericList(List, FALSE);
      return NULL;
    }

  do
    {
      if (!InfGetDataField(Context, 0, &KeyName))
	{
	  DPRINT1("InfGetDataField() failed\n");
	  break;
	}

      if (!InfGetDataField(Context, 1, &KeyValue))
	{
	  DPRINT1("InfGetDataField() failed\n");
	  break;
	}

      UserData = RtlAllocateHeap(ProcessHeap,
				 0,
				 (wcslen(KeyName) + 1) * sizeof(WCHAR));
      if (UserData == NULL)
	{
	  DPRINT1("RtlAllocateHeap() failed\n");
	  DestroyGenericList(List, TRUE);
	  return NULL;
	}

      wcscpy(UserData, KeyName);

      sprintf(Buffer, "%S", KeyValue);
      AppendGenericListEntry(List,
			     Buffer,
			     UserData,
			     _wcsicmp(KeyName, DisplayKey) ? FALSE : TRUE);
    }
  while (InfFindNextLine(Context, Context));
  InfFreeContext(Context);

#if 0
  AppendGenericListEntry(List, "Other display driver", NULL, TRUE);
#endif

  return List;
}

BOOLEAN
ProcessComputerFiles(HINF InfFile, PGENERIC_LIST List, PWCHAR* AdditionalSectionName)
{
	PGENERIC_LIST_ENTRY Entry;
	static WCHAR SectionName[128];

	DPRINT("ProcessComputerFiles() called\n");

	Entry = GetGenericListEntry(List);
	if (Entry == NULL)
	{
		DPRINT("GetGenericListEntry() failed\n");
		return FALSE;
	}

	wcscpy(SectionName, L"Files.");
	wcscat(SectionName, Entry->UserData);
	*AdditionalSectionName = SectionName;

	return TRUE;
}


BOOLEAN
ProcessDisplayRegistry(HINF InfFile, PGENERIC_LIST List)
{
  PGENERIC_LIST_ENTRY Entry;
  PINFCONTEXT Context;
  PWCHAR ServiceName;
  ULONG StartValue;
  NTSTATUS Status;
  WCHAR RegPath [255];
  PWCHAR Buffer;
  ULONG Width, Hight, Bpp;

  DPRINT("ProcessDisplayRegistry() called\n");

  Entry = GetGenericListEntry(List);
  if (Entry == NULL)
    {
      DPRINT("GetGenericListEntry() failed\n");
      return FALSE;
    }

  if (!InfFindFirstLine(InfFile, L"Display", Entry->UserData, &Context))
    {
      DPRINT("InfFindFirstLine() failed\n");
      return FALSE;
    }

  /* Enable the right driver */
  if (!InfGetDataField(Context, 3, &ServiceName))
    {
      DPRINT("InfGetDataField() failed\n");
      return FALSE;
    }

  ASSERT(wcslen(ServiceName) < 10);
  DPRINT("Service name: %S\n", ServiceName);

  StartValue = 1;
  Status = RtlWriteRegistryValue(RTL_REGISTRY_SERVICES,
				 ServiceName,
				 L"Start",
				 REG_DWORD,
				 &StartValue,
				 sizeof(ULONG));

  if (!NT_SUCCESS(Status))
    {
      DPRINT("RtlWriteRegistryValue() failed (Status %lx)\n", Status);
      return FALSE;
    }

  /* Set the resolution */
  swprintf(RegPath, L"\\Registry\\Machine\\System\\CurrentControlSet\\Hardware Profiles\\Current\\System\\CurrentControlSet\\Services\\%s\\Device0", ServiceName);

  if (!InfGetDataField(Context, 4, &Buffer))
    {
      DPRINT("InfGetDataField() failed\n");
      return FALSE;
    }
  Width = wcstoul(Buffer, NULL, 10);
  Status = RtlWriteRegistryValue(RTL_REGISTRY_ABSOLUTE,
				 RegPath, 
				 L"DefaultSettings.XResolution",
				 REG_DWORD,
				 &Width,
				 sizeof(ULONG));
  if (!NT_SUCCESS(Status))
    {
      DPRINT("RtlWriteRegistryValue() failed (Status %lx)\n", Status);
      return FALSE;
    }

  
  if (!InfGetDataField(Context, 5, &Buffer))
    {
      DPRINT("InfGetDataField() failed\n");
      return FALSE;
    }
  Hight = wcstoul(Buffer, 0, 0);
  Status = RtlWriteRegistryValue(RTL_REGISTRY_ABSOLUTE,
				 RegPath,
				 L"DefaultSettings.YResolution",
				 REG_DWORD,
				 &Hight,
				 sizeof(ULONG));
  if (!NT_SUCCESS(Status))
    {
      DPRINT("RtlWriteRegistryValue() failed (Status %lx)\n", Status);
      return FALSE;
    }

  if (!InfGetDataField(Context, 6, &Buffer))
    {
      DPRINT("InfGetDataField() failed\n");
      return FALSE;
    }
  Bpp = wcstoul(Buffer, 0, 0);
  Status = RtlWriteRegistryValue(RTL_REGISTRY_ABSOLUTE,
				 RegPath,
				 L"DefaultSettings.BitsPerPel",
				 REG_DWORD,
				 &Bpp,
				 sizeof(ULONG));
  if (!NT_SUCCESS(Status))
    {
      DPRINT("RtlWriteRegistryValue() failed (Status %lx)\n", Status);
      return FALSE;
    }

  InfFreeContext(Context);

  DPRINT("ProcessDisplayRegistry() done\n");

  return TRUE;
}


PGENERIC_LIST
CreateKeyboardDriverList(HINF InfFile)
{
  CHAR Buffer[128];
  PGENERIC_LIST List;
  PINFCONTEXT Context;
  PWCHAR KeyName;
  PWCHAR KeyValue;
  PWCHAR UserData;

  List = CreateGenericList();
  if (List == NULL)
    return NULL;

  if (!InfFindFirstLine (InfFile, L"Keyboard", NULL, &Context))
    {
      DestroyGenericList(List, FALSE);
      return NULL;
    }

  do
    {
      if (!InfGetData (Context, &KeyName, &KeyValue))
	{
	  /* FIXME: Handle error! */
	  DPRINT("InfGetData() failed\n");
	  break;
	}

      UserData = RtlAllocateHeap(ProcessHeap,
				 0,
				 (wcslen(KeyName) + 1) * sizeof(WCHAR));
      if (UserData == NULL)
	{
	  /* FIXME: Handle error! */
	}

      wcscpy(UserData, KeyName);

      sprintf(Buffer, "%S", KeyValue);
      AppendGenericListEntry(List, Buffer, UserData, FALSE);
    }
  while (InfFindNextLine(Context, Context));
  InfFreeContext(Context);

  return List;
}


PGENERIC_LIST
CreateKeyboardLayoutList(HINF InfFile)
{
  CHAR Buffer[128];
  PGENERIC_LIST List;
  PINFCONTEXT Context;
  PWCHAR KeyName;
  PWCHAR KeyValue;
  PWCHAR UserData;
  WCHAR DefaultLayout[20];

  /* Get default layout id */
  if (!InfFindFirstLine (InfFile, L"NLS", L"DefaultLayout", &Context))
    return NULL;

  if (!InfGetData (Context, NULL, &KeyValue))
    {
      InfFreeContext(Context);
      return NULL;
    }

  wcscpy(DefaultLayout, KeyValue);
  InfFreeContext(Context);

  List = CreateGenericList();
  if (List == NULL)
    return NULL;

  if (!InfFindFirstLine (InfFile, L"KeyboardLayout", NULL, &Context))
    {
      DestroyGenericList(List, FALSE);
      InfFreeContext(Context);
      return NULL;
    }

  do
    {
      if (!InfGetData (Context, &KeyName, &KeyValue))
	{
	  /* FIXME: Handle error! */
	  DPRINT("InfGetData() failed\n");
	  break;
	}

      UserData = RtlAllocateHeap(ProcessHeap,
				 0,
				 (wcslen(KeyName) + 1) * sizeof(WCHAR));
      if (UserData == NULL)
	{
	  /* FIXME: Handle error! */
	}

      wcscpy(UserData, KeyName);

      sprintf(Buffer, "%S", KeyValue);
      AppendGenericListEntry(List,
			     Buffer,
			     UserData,
			     _wcsicmp(KeyName, DefaultLayout) ? FALSE : TRUE);
    }
  while (InfFindNextLine(Context, Context));
  InfFreeContext(Context);

  return List;
}


BOOLEAN
ProcessKeyboardLayoutRegistry(PGENERIC_LIST List)
{
  PGENERIC_LIST_ENTRY Entry;
  PWCHAR LanguageId;
  OBJECT_ATTRIBUTES ObjectAttributes;
  UNICODE_STRING KeyName;
  UNICODE_STRING ValueName;
  HANDLE KeyHandle;
  NTSTATUS Status;

  Entry = GetGenericListEntry(List);
  if (Entry == NULL)
    return FALSE;

  LanguageId = (PWCHAR)Entry->UserData;
  if (LanguageId == NULL)
    return FALSE;

  /* Open the nls language key */
  RtlInitUnicodeString(&KeyName,
		       L"\\Registry\\Machine\\SYSTEM\\CurrentControlSet\\Control\\NLS\\Language");
  InitializeObjectAttributes(&ObjectAttributes,
			     &KeyName,
			     OBJ_CASE_INSENSITIVE,
			     NULL,
			     NULL);
  Status =  NtOpenKey(&KeyHandle,
		      KEY_ALL_ACCESS,
		      &ObjectAttributes);
  if (!NT_SUCCESS(Status))
    {
      DPRINT1("NtOpenKey() failed (Status %lx)\n", Status);
      return FALSE;
    }

  /* Set default language */
  RtlInitUnicodeString(&ValueName,
		       L"Default");
  Status = NtSetValueKey (KeyHandle,
			  &ValueName,
			  0,
			  REG_SZ,
			  (PVOID)(LanguageId + 4),
			  8);
  if (!NT_SUCCESS(Status))
    {
      DPRINT1("NtSetValueKey() failed (Status %lx)\n", Status);
      NtClose(KeyHandle);
      return FALSE;
    }

  /* Set install language */
  RtlInitUnicodeString(&ValueName,
		       L"InstallLanguage");
  Status = NtSetValueKey (KeyHandle,
			  &ValueName,
			  0,
			  REG_SZ,
			  (PVOID)(LanguageId + 4),
			  8);
  if (!NT_SUCCESS(Status))
    {
      DPRINT1("NtSetValueKey() failed (Status %lx)\n", Status);
      NtClose(KeyHandle);
      return FALSE;
    }

  NtClose(KeyHandle);

  return TRUE;
}


#if 0
BOOLEAN
ProcessKeyboardLayoutFiles(PGENERIC_LIST List)
{
  return TRUE;
}
#endif

/* EOF */

⌨️ 快捷键说明

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