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.

204 lines
7.4 KiB

  1. //==========================================================================
  2. //
  3. // Copyright (C) 2000 Microsoft Corporation. All Rights Reserved.
  4. //
  5. // File: fixbtn.c
  6. // Content: Code to overwrite the OEMData for SideWinder USB devices
  7. // with values containing the correct number of buttons.
  8. //
  9. //
  10. //==========================================================================
  11. #ifndef WIN32_LEAN_AND_MEAN
  12. #define WIN32_LEAN_AND_MEAN
  13. #endif
  14. #include <windows.h>
  15. #include <regstr.h>
  16. #include <aclapi.h>
  17. #define TYPE_COUNT 6
  18. struct
  19. {
  20. TCHAR tszKeyName[18];
  21. BYTE rgbOEMData[8];
  22. }
  23. c_Types[TYPE_COUNT] = {
  24. // Type key name OEMData to be set
  25. TEXT("VID_045E&PID_001A"), { 0x00,0x00,0x08,0x10,0x08,0x00,0x00,0x00 }, // Precision Wheel
  26. TEXT("VID_045E&PID_001B"), { 0x03,0x00,0x08,0x10,0x08,0x00,0x00,0x00 }, // FF 2
  27. TEXT("VID_045E&PID_0026"), { 0x20,0x00,0x00,0x10,0x09,0x00,0x00,0x00 }, // Gamepad Pro
  28. TEXT("VID_045E&PID_0034"), { 0x00,0x00,0x08,0x10,0x08,0x00,0x00,0x00 }, // FF Wheel 2
  29. TEXT("VID_045E&PID_0038"), { 0x03,0x00,0x08,0x10,0x08,0x00,0x00,0x00 }, // Precision 2
  30. TEXT("VID_045E&PID_0008"), { 0x03,0x00,0x08,0x10,0x0A,0x00,0x00,0x00 } // Precision Pro
  31. };
  32. int WINAPI WinMain
  33. (
  34. HINSTANCE hInstance, // handle to current instance
  35. HINSTANCE hPrevInstance, // handle to previous instance
  36. LPSTR lpCmdLine, // pointer to command line
  37. int nCmdShow // window show state
  38. )
  39. {
  40. LONG lRc;
  41. HKEY hkOEM;
  42. int KeysUnwritten = TYPE_COUNT;
  43. DWORD dwDisposition;
  44. SECURITY_DESCRIPTOR SecurityDesc;
  45. SID_IDENTIFIER_AUTHORITY authority = SECURITY_WORLD_SID_AUTHORITY;
  46. EXPLICIT_ACCESS ExplicitAccess;
  47. SECURITY_ATTRIBUTES sa;
  48. PSECURITY_ATTRIBUTES pSA = NULL;
  49. PSID pSid = NULL;
  50. PACL pACL = NULL;
  51. // If we're on any form of NT, set up all the things necessary to get
  52. // everyone access to these keys.
  53. // Also don't update the Precision Pro
  54. if( ((int)GetVersion()) >= 0 )
  55. {
  56. HMODULE hAdvApiDLL = LoadLibrary( TEXT("ADVAPI32.DLL") );
  57. if( hAdvApiDLL )
  58. {
  59. typedef VOID (WINAPI * DYNAMICBUILDTRUSTEEWITHSIDA) (PTRUSTEE_A pTrustee, PSID pSid);
  60. typedef DWORD (WINAPI * DYNAMICSETENTRIESINACLA)
  61. (ULONG cCountOfExplicitEntries, PEXPLICIT_ACCESS_A pListOfExplicitEntries, PACL OldAcl, PACL * NewAcl );
  62. DYNAMICBUILDTRUSTEEWITHSIDA DynamicBuildTrusteeWithSidA;
  63. DYNAMICSETENTRIESINACLA DynamicSetEntriesInAclA;
  64. DynamicBuildTrusteeWithSidA = (DYNAMICBUILDTRUSTEEWITHSIDA)GetProcAddress( hAdvApiDLL, TEXT("BuildTrusteeWithSidA") );
  65. DynamicSetEntriesInAclA = (DYNAMICSETENTRIESINACLA)GetProcAddress( hAdvApiDLL, TEXT("SetEntriesInAclA") );
  66. if( DynamicBuildTrusteeWithSidA && DynamicSetEntriesInAclA )
  67. {
  68. DWORD dwErr;
  69. // Describe the access we want to create the key with
  70. ZeroMemory (&ExplicitAccess, sizeof(ExplicitAccess) );
  71. ExplicitAccess.grfAccessPermissions = KEY_QUERY_VALUE | KEY_SET_VALUE
  72. | KEY_CREATE_SUB_KEY | KEY_ENUMERATE_SUB_KEYS
  73. | KEY_NOTIFY | KEY_CREATE_LINK
  74. | DELETE | READ_CONTROL
  75. | WRITE_DAC | WRITE_OWNER;
  76. ExplicitAccess.grfAccessMode = SET_ACCESS; // discard any existing AC info
  77. ExplicitAccess.grfInheritance = SUB_CONTAINERS_AND_OBJECTS_INHERIT;
  78. if (AllocateAndInitializeSid(
  79. &authority,
  80. 1,
  81. SECURITY_WORLD_RID, 0, 0, 0, 0, 0, 0, 0,
  82. &pSid
  83. ))
  84. {
  85. DynamicBuildTrusteeWithSidA(&(ExplicitAccess.Trustee), pSid );
  86. dwErr = DynamicSetEntriesInAclA( 1, &ExplicitAccess, NULL, &pACL );
  87. if( dwErr == ERROR_SUCCESS )
  88. {
  89. if( InitializeSecurityDescriptor( &SecurityDesc, SECURITY_DESCRIPTOR_REVISION ) )
  90. {
  91. if( SetSecurityDescriptorDacl( &SecurityDesc, TRUE, pACL, FALSE ) )
  92. {
  93. // Initialize a security attributes structure.
  94. sa.nLength = sizeof (SECURITY_ATTRIBUTES);
  95. sa.lpSecurityDescriptor = &SecurityDesc;
  96. sa.bInheritHandle = TRUE;// Use the security attributes to create a key.
  97. pSA = &sa;
  98. }
  99. }
  100. }
  101. }
  102. }
  103. FreeLibrary( hAdvApiDLL );
  104. }
  105. // and don't write the Precision Pro key.
  106. KeysUnwritten--;
  107. }
  108. lRc = RegCreateKeyEx(
  109. HKEY_LOCAL_MACHINE, // handle of an open key
  110. REGSTR_PATH_JOYOEM, // address of subkey name
  111. 0, // reserved
  112. NULL, // address of class string
  113. REG_OPTION_NON_VOLATILE, // special options flag
  114. KEY_WRITE, // desired security access
  115. pSA, // address of key security structure
  116. &hkOEM, // address of buffer for opened handle
  117. &dwDisposition // address of disposition value buffer
  118. );
  119. if( lRc == ERROR_SUCCESS )
  120. {
  121. int Idx;
  122. HKEY hkType;
  123. for( Idx = KeysUnwritten-1; Idx >= 0; Idx-- )
  124. {
  125. lRc = RegCreateKeyEx(
  126. hkOEM, // handle of an open key
  127. c_Types[Idx].tszKeyName, // address of subkey name
  128. 0, // reserved
  129. NULL, // address of class string
  130. REG_OPTION_NON_VOLATILE, // special options flag
  131. KEY_WRITE, // desired security access
  132. pSA, // address of key security structure
  133. &hkType, // address of buffer for opened handle
  134. &dwDisposition // address of disposition value buffer
  135. );
  136. if( lRc == ERROR_SUCCESS )
  137. {
  138. lRc = RegSetValueEx( hkType, REGSTR_VAL_JOYOEMDATA, 0,
  139. REG_BINARY, c_Types[Idx].rgbOEMData, sizeof( c_Types[0].rgbOEMData ) );
  140. if( lRc == ERROR_SUCCESS )
  141. {
  142. KeysUnwritten--;
  143. }
  144. RegCloseKey( hkType );
  145. }
  146. }
  147. RegCloseKey( hkOEM );
  148. }
  149. //Cleanup pACL
  150. if( pACL != NULL )
  151. {
  152. LocalFree( pACL );
  153. }
  154. //Cleanup pSid
  155. if( pSid != NULL )
  156. {
  157. FreeSid( pSid );
  158. }
  159. if( KeysUnwritten )
  160. {
  161. MessageBox( 0, TEXT( "Update incomplete" ), TEXT( "Button fix" ), MB_ICONEXCLAMATION );
  162. }
  163. else
  164. {
  165. MessageBox( 0, TEXT( "Update OK" ), TEXT( "Button fix" ), 0 );
  166. }
  167. return KeysUnwritten;
  168. };