Source code of Windows XP (NT5)
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.

243 lines
5.1 KiB

  1. #include <windows.h>
  2. #include <stdio.h>
  3. #include <io.h>
  4. /*
  5. Usage:
  6. sublocal -winini <section> <key> <value>
  7. IE,
  8. sublocal -winini fonts "MS Sans Serif (8,10,12,18,24 (VGA res)" sserifeg.fon
  9. sublocal -userdef
  10. Return codes:
  11. 0 - success
  12. 1 - invalid command line args
  13. 2 - failure (-winini case)
  14. 3 - failure (-userdef case)
  15. */
  16. typedef enum {
  17. ResultSuccess,
  18. ResultInvalidArgs,
  19. ResultWinIniFailure,
  20. ResultUserDefFailure,
  21. ResultTurkishKeyboardFailure
  22. } RESULTCODE;
  23. BOOL
  24. EnablePrivilege(
  25. IN PTSTR PrivilegeName
  26. )
  27. {
  28. HANDLE Token;
  29. BOOL b;
  30. TOKEN_PRIVILEGES NewPrivileges;
  31. LUID Luid;
  32. if(!OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES,&Token)) {
  33. return(FALSE);
  34. }
  35. if(!LookupPrivilegeValue(NULL,PrivilegeName,&Luid)) {
  36. CloseHandle(Token);
  37. return(FALSE);
  38. }
  39. NewPrivileges.PrivilegeCount = 1;
  40. NewPrivileges.Privileges[0].Luid = Luid;
  41. NewPrivileges.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  42. b = AdjustTokenPrivileges(
  43. Token,
  44. FALSE,
  45. &NewPrivileges,
  46. 0,
  47. NULL,
  48. NULL
  49. );
  50. CloseHandle(Token);
  51. return(b);
  52. }
  53. RESULTCODE
  54. DoNewUserDef(
  55. VOID
  56. )
  57. {
  58. TCHAR UserDefName[MAX_PATH];
  59. TCHAR TempName[MAX_PATH];
  60. BOOL Renamed;
  61. ULONG u;
  62. //
  63. // Form the full pathname of the userdef hive
  64. // and a temporary filename.
  65. //
  66. GetSystemDirectory(UserDefName,MAX_PATH);
  67. lstrcat(UserDefName,TEXT("\\CONFIG"));
  68. GetTempFileName(UserDefName,TEXT("HIVE"),0,TempName);
  69. lstrcat(UserDefName,TEXT("\\USERDEF"));
  70. //
  71. // Rename the existing userdef to the temp file name.
  72. // If this fails, assume there is no existing userdef hive (!!!)
  73. // and continue.
  74. //
  75. // Note that GetTempFileName creates the temp file and leaves it
  76. // there so we have to use MoveFileEx to replace it.
  77. //
  78. Renamed = MoveFileEx(UserDefName,TempName,MOVEFILE_REPLACE_EXISTING);
  79. //
  80. // Save the current user to userdef.
  81. //
  82. EnablePrivilege(SE_BACKUP_NAME);
  83. u = RegSaveKey(HKEY_CURRENT_USER,UserDefName,NULL);
  84. if(u != NO_ERROR) {
  85. //
  86. // Save failed. Clean up and return.
  87. //
  88. DeleteFile(UserDefName);
  89. if(Renamed) {
  90. MoveFile(TempName,UserDefName);
  91. }
  92. return(ResultUserDefFailure);
  93. }
  94. //
  95. // Save worked. Get rid of the original userdef.
  96. //
  97. SetFileAttributes(TempName,FILE_ATTRIBUTE_NORMAL);
  98. DeleteFile(TempName);
  99. return(ResultSuccess);
  100. }
  101. static TCHAR szKey[] = TEXT("System\\CurrentControlSet\\Control\\Keyboard Layout\\DosKeybIDs");
  102. RESULTCODE
  103. PatchTurkishDosKeybIDs(
  104. VOID
  105. )
  106. {
  107. DWORD dwSize, dwType;
  108. int rc;
  109. HANDLE hkey;
  110. TCHAR szX0000041F[40];
  111. TCHAR szX0001041F[40];
  112. if (!RegOpenKeyEx(HKEY_LOCAL_MACHINE, szKey, 0L, KEY_WRITE, (PHKEY)&hkey)) {
  113. // dwSize = sizeof(szX0000041F);
  114. // rc = RegQueryValueEx (hkey, TEXT("0000041F"), NULL, &dwType,
  115. // (LPBYTE)szX0000041F, &dwSize);
  116. rc = RegSetValueEx (hkey, TEXT("0000041F"), 0L, REG_SZ,
  117. (LPBYTE)TEXT("179"), (lstrlen(TEXT("179")) + 1)*sizeof(TCHAR));
  118. if (rc != 0) {
  119. RegCloseKey (hkey);
  120. printf("Error = %d\n", GetLastError());
  121. return ResultTurkishKeyboardFailure;
  122. }
  123. // dwSize = sizeof(szX0001041F);
  124. // rc = RegQueryValueEx (hkey, TEXT("0001041F"), NULL, &dwType,
  125. // (LPBYTE)szX0001041F, &dwSize);
  126. rc = RegSetValueEx (hkey, TEXT("0001041F"), 0L, REG_SZ,
  127. (LPBYTE)TEXT("440"), (lstrlen(TEXT("440")) + 1)*sizeof(TCHAR));
  128. RegCloseKey (hkey);
  129. if (rc != 0) {
  130. printf("Error = %d\n", GetLastError());
  131. return ResultTurkishKeyboardFailure;
  132. }
  133. // printf("0000041F=%s, 0001041F=%s\n", szX0000041F, szX0001041F);
  134. }
  135. else {
  136. printf("Error = %d\n", GetLastError());
  137. return ResultTurkishKeyboardFailure;
  138. }
  139. return rc;
  140. }
  141. int
  142. tmain(
  143. IN int argc,
  144. IN PTSTR argv[]
  145. )
  146. {
  147. RESULTCODE result;
  148. //
  149. // Check arguments.
  150. //
  151. if((argc < 2)
  152. || ((argv[1][0] != TEXT('-')) && (argv[1][0] != TEXT('/')))) {
  153. return(ResultInvalidArgs);
  154. }
  155. //
  156. // See whether we are supposed to set a win.ini value.
  157. //
  158. if(!lstrcmpi(argv[1]+1,TEXT("winini"))) {
  159. result = (argc == 5)
  160. ? (WriteProfileString(argv[2],argv[3],argv[4]) ? ResultSuccess
  161. : ResultWinIniFailure)
  162. : ResultInvalidArgs;
  163. //
  164. // See whether we are supposed to create a new userdef hive.
  165. //
  166. }
  167. else if(!lstrcmpi(argv[1]+1,TEXT("userdef"))) {
  168. result = DoNewUserDef();
  169. }
  170. else if(!lstrcmpi(argv[1]+1,TEXT("PatchTurkishKeyb"))) {
  171. result = PatchTurkishDosKeybIDs();
  172. }
  173. else {
  174. //
  175. // Unknown operation.
  176. //
  177. result = ResultInvalidArgs;
  178. }
  179. return(result);
  180. }
  181. int
  182. __cdecl
  183. main(
  184. IN int argc,
  185. IN char *argv[]
  186. )
  187. {
  188. return(tmain(argc,argv));
  189. }