wizard.c

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

C
1,867
字号
    }

  SetupData->TimeZoneListTail = NULL;
}

#if 0
static BOOL
GetTimeZoneListIndex(LPDWORD lpIndex)
{
  TCHAR szLanguageIdString[9];
  HKEY hKey;
  DWORD dwValueSize;
  DWORD Length;
  LPTSTR Buffer;
  LPTSTR Ptr;
  LPTSTR End;
  BOOL bFound;

  if (RegOpenKeyEx(HKEY_LOCAL_MACHINE,
		   _T("SYSTEM\\CurrentControlSet\\Control\\NLS\\Language"),
		   0,
		   KEY_ALL_ACCESS,
		   &hKey))
    return FALSE;

  dwValueSize = 9 * sizeof(TCHAR);
  if (RegQueryValueEx(hKey,
		      _T("Default"),
		      NULL,
		      NULL,
		      (LPBYTE)szLanguageIdString,
		      &dwValueSize))
    {
      RegCloseKey(hKey);
      return FALSE;
    }

  RegCloseKey(hKey);

  if (RegOpenKeyEx(HKEY_LOCAL_MACHINE,
		   _T("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones"),
		   0,
		   KEY_ALL_ACCESS,
		   &hKey))
    return FALSE;

  dwValueSize = 0;
  if (RegQueryValueEx(hKey,
		      _T("IndexMapping"),
		      NULL,
		      NULL,
		      NULL,
		      &dwValueSize))
    {
      RegCloseKey(hKey);
      return FALSE;
    }

  Buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwValueSize);
  if (Buffer == NULL)
    {
      RegCloseKey(hKey);
      return FALSE;
    }

  if (RegQueryValueEx(hKey,
		      _T("IndexMapping"),
		      NULL,
		      NULL,
		      (LPBYTE)Buffer,
		      &dwValueSize))
    {
      HeapFree(GetProcessHeap(), 0, Buffer);
      RegCloseKey(hKey);
      return FALSE;
    }

  RegCloseKey(hKey);

  Ptr = Buffer;
  while (*Ptr != 0)
    {
      Length = _tcslen(Ptr);
      if (_tcsicmp(Ptr, szLanguageIdString) == 0)
        bFound = TRUE;

      Ptr = Ptr + Length + 1;
      if (*Ptr == 0)
        break;

      Length = _tcslen(Ptr);

      if (bFound)
        {
          *lpIndex = _tcstoul(Ptr, &End, 10);
          HeapFree(GetProcessHeap(), 0, Buffer);
          return FALSE;
        }

      Ptr = Ptr + Length + 1;
    }

  HeapFree(GetProcessHeap(), 0, Buffer);

  return FALSE;
}
#endif


static VOID
ShowTimeZoneList(HWND hwnd, PSETUPDATA SetupData)
{
  PTIMEZONE_ENTRY Entry;
  DWORD dwIndex = 0;
  DWORD dwEntryIndex = 0;
  DWORD dwCount;

#if 0
  GetTimeZoneListIndex(&dwEntryIndex);
#endif
  dwEntryIndex = 85; /* GMT time zone */

  Entry = SetupData->TimeZoneListHead;
  while (Entry != NULL)
    {
      dwCount = SendMessage(hwnd,
			    CB_ADDSTRING,
			    0,
			    (LPARAM)Entry->Description);

      if (dwEntryIndex != 0 && dwEntryIndex == Entry->Index)
        dwIndex = dwCount;

      Entry = Entry->Next;
    }

  SendMessage(hwnd,
	      CB_SETCURSEL,
	      (WPARAM)dwIndex,
	      0);
}


static VOID
SetLocalTimeZone(HWND hwnd, PSETUPDATA SetupData)
{
  TIME_ZONE_INFORMATION TimeZoneInformation;
  PTIMEZONE_ENTRY Entry;
  DWORD dwIndex;
  DWORD i;

  dwIndex = SendMessage(hwnd,
			CB_GETCURSEL,
			0,
			0);

  i = 0;
  Entry = SetupData->TimeZoneListHead;
  while (i < dwIndex)
    {
      if (Entry == NULL)
	return;

      i++;
      Entry = Entry->Next;
    }

  _tcscpy(TimeZoneInformation.StandardName,
	  Entry->StandardName);
  _tcscpy(TimeZoneInformation.DaylightName,
	  Entry->DaylightName);

  TimeZoneInformation.Bias = Entry->TimezoneInfo.Bias;
  TimeZoneInformation.StandardBias = Entry->TimezoneInfo.StandardBias;
  TimeZoneInformation.DaylightBias = Entry->TimezoneInfo.DaylightBias;

  memcpy(&TimeZoneInformation.StandardDate,
	 &Entry->TimezoneInfo.StandardDate,
	 sizeof(SYSTEMTIME));
  memcpy(&TimeZoneInformation.DaylightDate,
	 &Entry->TimezoneInfo.DaylightDate,
	 sizeof(SYSTEMTIME));

  /* Set time zone information */
  SetTimeZoneInformation(&TimeZoneInformation);
}


static BOOL
GetLocalSystemTime(HWND hwnd, PSETUPDATA SetupData)
{
  SYSTEMTIME Date;
  SYSTEMTIME Time;

  if (DateTime_GetSystemTime(GetDlgItem(hwnd, IDC_DATEPICKER), &Date) != GDT_VALID)
    {
      return FALSE;
    }

  if (DateTime_GetSystemTime(GetDlgItem(hwnd, IDC_TIMEPICKER), &Time) != GDT_VALID)
    {
      return FALSE;
    }

  SetupData->SystemTime.wYear = Date.wYear;
  SetupData->SystemTime.wMonth = Date.wMonth;
  SetupData->SystemTime.wDayOfWeek = Date.wDayOfWeek;
  SetupData->SystemTime.wDay = Date.wDay;
  SetupData->SystemTime.wHour = Time.wHour;
  SetupData->SystemTime.wMinute = Time.wMinute;
  SetupData->SystemTime.wSecond = Time.wSecond;
  SetupData->SystemTime.wMilliseconds = Time.wMilliseconds;

  return TRUE;
}


static VOID
SetAutoDaylightInfo(HWND hwnd)
{
  HKEY hKey;
  DWORD dwValue = 1;

  if (SendMessage(hwnd, BM_GETCHECK, 0, 0) == BST_UNCHECKED)
    {
      if (RegOpenKeyEx(HKEY_LOCAL_MACHINE,
		       _T("SYSTEM\\CurrentControlSet\\Control\\TimeZoneInformation"),
		       0,
		       KEY_SET_VALUE,
		       &hKey))
	  return;

      RegSetValueEx(hKey,
		    _T("DisableAutoDaylightTimeSet"),
		    0,
		    REG_DWORD,
		    (LPBYTE)&dwValue,
		    sizeof(DWORD));
      RegCloseKey(hKey);
    }
}


static BOOL
SetSystemLocalTime(HWND hwnd, PSETUPDATA SetupData)
{
  HANDLE hToken;
  DWORD PrevSize;
  TOKEN_PRIVILEGES priv, previouspriv;
  BOOL Ret = FALSE;

  /*
   * enable the SeSystemtimePrivilege privilege
   */

  if(OpenProcessToken(GetCurrentProcess(),
                      TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
                      &hToken))
  {
    priv.PrivilegeCount = 1;
    priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;

    if(LookupPrivilegeValue(NULL,
                            SE_SYSTEMTIME_NAME,
                            &priv.Privileges[0].Luid))
    {
      if(AdjustTokenPrivileges(hToken,
                               FALSE,
                               &priv,
                               sizeof(previouspriv),
                               &previouspriv,
                               &PrevSize) &&
         GetLastError() == ERROR_SUCCESS)
      {
        /*
         * We successfully enabled it, we're permitted to change the system time
         * Call SetLocalTime twice to ensure correct results
         */
        Ret = SetLocalTime(&SetupData->SystemTime) &&
              SetLocalTime(&SetupData->SystemTime);

        /*
         * for the sake of security, restore the previous status again
         */
        if(previouspriv.PrivilegeCount > 0)
        {
          AdjustTokenPrivileges(hToken,
                                FALSE,
                                &previouspriv,
                                0,
                                NULL,
                                0);
        }
      }
    }
    CloseHandle(hToken);
  }

  return Ret;
}


static INT_PTR CALLBACK
DateTimePageDlgProc(HWND hwndDlg,
                    UINT uMsg,
                    WPARAM wParam,
                    LPARAM lParam)
{
  PSETUPDATA SetupData;

  /* Retrieve pointer to the global setup data */
  SetupData = (PSETUPDATA)GetWindowLongPtr (hwndDlg, GWL_USERDATA);

  switch (uMsg)
    {
      case WM_INITDIALOG:
        {
          /* Save pointer to the global setup data */
          SetupData = (PSETUPDATA)((LPPROPSHEETPAGE)lParam)->lParam;
          SetWindowLongPtr(hwndDlg, GWL_USERDATA, (DWORD_PTR)SetupData);

          CreateTimeZoneList(SetupData);

          ShowTimeZoneList(GetDlgItem(hwndDlg, IDC_TIMEZONELIST),
                           SetupData);

          SendDlgItemMessage(hwndDlg, IDC_AUTODAYLIGHT, BM_SETCHECK, (WPARAM)BST_CHECKED, 0);
        }
        break;


      case WM_NOTIFY:
        {
          LPNMHDR lpnm = (LPNMHDR)lParam;

          switch (lpnm->code)
            {
              case PSN_SETACTIVE:
                /* Enable the Back and Next buttons */
                PropSheet_SetWizButtons(GetParent(hwndDlg), PSWIZB_BACK | PSWIZB_NEXT);
                break;

              case PSN_WIZNEXT:
                {
                  GetLocalSystemTime(hwndDlg, SetupData);
                  SetLocalTimeZone(GetDlgItem(hwndDlg, IDC_TIMEZONELIST),
                                   SetupData);
                  SetAutoDaylightInfo(GetDlgItem(hwndDlg, IDC_AUTODAYLIGHT));
                  if(!SetSystemLocalTime(hwndDlg, SetupData))
                  {
                    MessageBox(hwndDlg,
                               _T("Setup was unable to set the local time."),
                               _T("ReactOS Setup"),
                               MB_ICONWARNING | MB_OK);
                  }
                }
                break;

              default:
                break;
            }
        }
        break;

      case WM_DESTROY:
        DestroyTimeZoneList(SetupData);
        break;

      default:
        break;
    }

  return FALSE;
}


static UINT CALLBACK
RegistrationNotificationProc(PVOID Context,
                             UINT Notification,
                             UINT_PTR Param1,
                             UINT_PTR Param2)
{
  PREGISTRATIONDATA RegistrationData;
  REGISTRATIONNOTIFY RegistrationNotify;
  PSP_REGISTER_CONTROL_STATUSW StatusInfo;
  UINT MessageID;
  WCHAR ErrorMessage[128];

  RegistrationData = (PREGISTRATIONDATA) Context;

  if (SPFILENOTIFY_STARTREGISTRATION == Notification ||
      SPFILENOTIFY_ENDREGISTRATION == Notification)
    {
      StatusInfo = (PSP_REGISTER_CONTROL_STATUSW) Param1;
      RegistrationNotify.CurrentItem = wcsrchr(StatusInfo->FileName, L'\\');
      if (NULL == RegistrationNotify.CurrentItem)
        {
          RegistrationNotify.CurrentItem = StatusInfo->FileName;
        }
      else
        {
          RegistrationNotify.CurrentItem++;
        }

      if (SPFILENOTIFY_STARTREGISTRATION == Notification)
        {
          DPRINT("Received SPFILENOTIFY_STARTREGISTRATION notification for %S\n",
                 StatusInfo->FileName);
          RegistrationNotify.ErrorMessage = NULL;
          RegistrationNotify.Progress = RegistrationData->Registered;
        }
      else
        {
          DPRINT("Received SPFILENOTIFY_ENDREGISTRATION notification for %S\n",
                 StatusInfo->FileName);
          DPRINT("Win32Error %u FailureCode %u\n", StatusInfo->Win32Error,
                 StatusInfo->FailureCode);
          if (SPREG_SUCCESS != StatusInfo->FailureCode)
            {
              switch(StatusInfo->FailureCode)
                {
                case SPREG_LOADLIBRARY:
                  MessageID = IDS_LOADLIBRARY_FAILED;
                  break;
                case SPREG_GETPROCADDR:
                  MessageID = IDS_GETPROCADDR_FAILED;
                  break;
                case SPREG_REGSVR:
                  MessageID = IDS_REGSVR_FAILED;
                  break;
                case SPREG_DLLINSTALL:
                  MessageID = IDS_DLLINSTALL_FAILED;
                  break;
                case SPREG_TIMEOUT:
                  MessageID = IDS_TIMEOUT;
                  break;
                default:
                  MessageID = IDS_REASON_UNKNOWN;
                  break;
                }
              if (0 == LoadStringW(hDllInstance, MessageID,
                                   ErrorMessage,
                                   sizeof(ErrorMessage) /
                                   sizeof(ErrorMessage[0])))
                {
                  ErrorMessage[0] = L'\0';
                }
              if (SPREG_TIMEOUT != StatusInfo->FailureCode)
                {
                  FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM, NULL,
                                 StatusInfo->Win32Error, 0, 
                                 ErrorMessage + wcslen(ErrorMessage),
                                 sizeof(ErrorMessage) / sizeof(ErrorMessage[0]) -
                                 wcslen(ErrorMessage), NULL);
                }
              RegistrationNotify.ErrorMessage = ErrorMessage;
            }
          else
            {
              RegistrationNotify.ErrorMessage = NULL;
            }
          if (RegistrationData->Registered < RegistrationData->DllCount)
            {
              RegistrationData->Registered++;
            }
        }

⌨️ 快捷键说明

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