Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

156 lines
3.2 KiB

  1. /*++
  2. Copyright (c) 1989-2001 Microsoft Corporation
  3. Module Name:
  4. setloc.cpp
  5. Abstract:
  6. Sets the system default locale ID
  7. Author:
  8. Vijay Jayaseelan (vijayj@microsoft.com) 05'November'2001
  9. Revision History:
  10. --*/
  11. #include <nt.h>
  12. #include <ntrtl.h>
  13. #include <nturtl.h>
  14. #include <iostream>
  15. #include <string>
  16. #include <exception>
  17. #include <windows.h>
  18. #include <stdlib.h>
  19. using namespace std;
  20. //
  21. // global data
  22. //
  23. const string Usage = "Usage: setloc.exe [/lcid <locale-id>]\n";
  24. const int MinimumArgs = 2;
  25. const string ShowHelp1 = "/?";
  26. const string ShowHelp2 = "-h";
  27. //
  28. // Helper dump operators
  29. //
  30. std::ostream& operator<<(std::ostream &os, const std::wstring &str) {
  31. FILE *OutStream = (&os == &std::cerr) ? stderr : stdout;
  32. fwprintf(OutStream, (PWSTR)str.c_str());
  33. return os;
  34. }
  35. //
  36. // Helper dump operators
  37. //
  38. std::ostream& operator<<(std::ostream &os, WCHAR *Str) {
  39. std::wstring WStr = Str;
  40. os << WStr;
  41. return os;
  42. }
  43. //
  44. // Exceptions
  45. //
  46. struct ProgramException : public std::exception {
  47. virtual void Dump(std::ostream &os) = 0;
  48. };
  49. //
  50. // Abstracts a Win32 error
  51. //
  52. struct W32Error : public ProgramException {
  53. DWORD ErrorCode;
  54. W32Error(DWORD ErrCode = GetLastError()) : ErrorCode(ErrCode){}
  55. void Dump(std::ostream &os) {
  56. WCHAR MsgBuffer[4096];
  57. MsgBuffer[0] = UNICODE_NULL;
  58. DWORD CharCount = FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM,
  59. NULL,
  60. ErrorCode,
  61. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  62. MsgBuffer,
  63. sizeof(MsgBuffer)/sizeof(WCHAR),
  64. NULL);
  65. if (CharCount) {
  66. std::wstring Msg(MsgBuffer);
  67. os << Msg;
  68. } else {
  69. os << std::hex << ErrorCode;
  70. }
  71. }
  72. };
  73. //
  74. // Abstracts usage exception
  75. //
  76. struct UsageException : public ProgramException {
  77. void Dump(std::ostream &os) {
  78. os << Usage;
  79. }
  80. };
  81. /*
  82. /* main() entry point
  83. */
  84. int
  85. __cdecl
  86. main(
  87. int Argc,
  88. char *Argv[]
  89. )
  90. {
  91. int Result = 1;
  92. try {
  93. if (Argc >= MinimumArgs) {
  94. string Arg1(Argv[1]);
  95. if ((Arg1 == ShowHelp1) || (Arg1 == ShowHelp2) ||
  96. (Arg1 != "/lcid") || (Argc != 3)) {
  97. throw new UsageException();
  98. } else {
  99. char *EndPtr = 0;
  100. DWORD LcId = strtoul(Argv[2], &EndPtr, 0);
  101. NTSTATUS Status = NtSetDefaultLocale(FALSE, LcId);
  102. if (!NT_SUCCESS(Status)) {
  103. throw new W32Error();
  104. }
  105. }
  106. } else {
  107. LCID SystemDefault = GetSystemDefaultLCID();
  108. cout << "System default LCID = 0x" << hex << SystemDefault << endl;
  109. }
  110. }
  111. catch(ProgramException *Exp) {
  112. if (Exp) {
  113. Exp->Dump(cout);
  114. delete Exp;
  115. }
  116. }
  117. return Result;
  118. }