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.

207 lines
5.3 KiB

  1. #include <windows.h>
  2. #include <setupapi.h>
  3. #include <spapip.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <ole2.h>
  8. #include <excppkg.h>
  9. #define MYPACKAGE_GUID L"{0e25c565-2fcc-4dcb-8e3e-4378a024c50e}"
  10. #define PACKAGE_DIRECTORY L"%windir%\\RegisteredPackages\\"
  11. #define PACKAGE_CAT L"exception.cat"
  12. #define PACKAGE_INF L"exception.inf"
  13. #define PACKAGE_CAB L"exception.cab"
  14. BOOL
  15. CALLBACK
  16. pComponentLister(
  17. IN const PSETUP_OS_COMPONENT_DATA SetupOsComponentData,
  18. IN const PSETUP_OS_EXCEPTION_DATA SetupOsExceptionData,
  19. IN OUT DWORD_PTR Context
  20. )
  21. {
  22. PDWORD Count = (PDWORD) Context;
  23. PWSTR GuidString;
  24. StringFromIID(&SetupOsComponentData->ComponentGuid, &GuidString);
  25. wprintf( L"Component Data\n\tName: %ws\n\tGuid: %ws\n\tVersionMajor: %d\n\tVersionMinor: %d\n",
  26. SetupOsComponentData->FriendlyName,
  27. GuidString,
  28. SetupOsComponentData->VersionMajor,
  29. SetupOsComponentData->VersionMinor);
  30. wprintf( L"ExceptionData\n\tInf: %ws\n\tCatalog: %ws\n",
  31. SetupOsExceptionData->ExceptionInfName,
  32. SetupOsExceptionData->CatalogFileName);
  33. *Count += 1;
  34. CoTaskMemFree( GuidString );
  35. return(TRUE);
  36. }
  37. int
  38. __cdecl
  39. main(
  40. IN int argc,
  41. IN char *argv[]
  42. )
  43. {
  44. WCHAR Path[MAX_PATH];
  45. SETUP_OS_COMPONENT_DATA ComponentData,cd;
  46. SETUP_OS_EXCEPTION_DATA ExceptionData,ed;
  47. PWSTR s,t;
  48. GUID MyGuid;
  49. PWSTR GuidString;
  50. WCHAR SourcePath[MAX_PATH];
  51. PCWSTR FileList[] = {PACKAGE_INF,PACKAGE_CAT,PACKAGE_CAB};
  52. #define FileListCount (sizeof(FileList)/sizeof(PCWSTR))
  53. DWORD i;
  54. //
  55. // 1. Make sure my package isn't already installed.
  56. //
  57. ComponentData.SizeOfStruct = sizeof(SETUP_OS_COMPONENT_DATA);
  58. ExceptionData.SizeOfStruct = sizeof(SETUP_OS_EXCEPTION_DATA);
  59. IIDFromString( MYPACKAGE_GUID, &MyGuid);
  60. if (SetupQueryRegisteredOsComponent(
  61. &MyGuid,
  62. &ComponentData,
  63. &ExceptionData)) {
  64. wprintf(L"My component is already registered with the OS, removing it!\n");
  65. if (!SetupUnRegisterOsComponent(&MyGuid)) {
  66. wprintf(L"couldn't remove my component, ec = %d\n", GetLastError());
  67. return 1;
  68. }
  69. }
  70. //
  71. // 2. unregister any packages that are superceded by my package
  72. //
  73. //
  74. // 3. Install my package
  75. //
  76. //
  77. // 3a. copy my exception package to the appropriate location
  78. //
  79. //
  80. // 3a.1 make sure the main package directory exists
  81. //
  82. ExpandEnvironmentStrings(
  83. PACKAGE_DIRECTORY,
  84. Path,
  85. sizeof(Path)/sizeof(WCHAR));
  86. CreateDirectory( Path, NULL );
  87. //
  88. // 3a.2 now create my package directory
  89. //
  90. wcscat( Path, MYPACKAGE_GUID );
  91. CreateDirectory( Path, NULL );
  92. //
  93. // 3a.3 now copy the bits to this location
  94. //
  95. wcscat( Path, L"\\" );
  96. t = wcsrchr( Path, L'\\' );
  97. t += 1;
  98. ExpandEnvironmentStrings(
  99. L"%temp%\\mypackagesource\\",
  100. SourcePath,
  101. sizeof(SourcePath)/sizeof(WCHAR));
  102. s = wcsrchr( SourcePath, L'\\' );
  103. s += 1;
  104. for (i = 0; i < FileListCount; i++) {
  105. *s = '\0';
  106. *t = '\0';
  107. wcscat(s,FileList[i]);
  108. wcscat(t,FileList[i]);
  109. CopyFile(SourcePath, Path ,FALSE);
  110. }
  111. //
  112. // 3b. register the package
  113. //
  114. ComponentData.VersionMajor = 2;
  115. ComponentData.VersionMinor = 5;
  116. RtlMoveMemory(&ComponentData.ComponentGuid, &MyGuid,sizeof(GUID));
  117. wcscpy(ComponentData.FriendlyName, L"My Exception Package");
  118. wcscpy( Path, PACKAGE_DIRECTORY );
  119. wcscat( Path, MYPACKAGE_GUID );
  120. wcscat( Path, L"\\" );
  121. t = wcsrchr( Path, L'\\' );
  122. t += 1;
  123. *t = '\0';
  124. wcscat( t, PACKAGE_INF );
  125. wcscpy(ExceptionData.ExceptionInfName, Path);
  126. *t = '\0';
  127. wcscat( t, PACKAGE_CAT );
  128. wcscpy(ExceptionData.CatalogFileName, Path);
  129. if (!SetupRegisterOsComponent(&ComponentData, &ExceptionData)) {
  130. wprintf( L"Failed to register component, ec = %d\n", GetLastError() );
  131. return 1;
  132. }
  133. //
  134. // 4. retrieve my package
  135. //
  136. cd.SizeOfStruct = sizeof(SETUP_OS_COMPONENT_DATA);
  137. ed.SizeOfStruct = sizeof(SETUP_OS_EXCEPTION_DATA);
  138. if (!SetupQueryRegisteredOsComponent(
  139. &MyGuid,
  140. &cd,
  141. &ed)) {
  142. wprintf( L"Failed to register component, ec = %d\n", GetLastError() );
  143. return 1;
  144. }
  145. StringFromIID(&cd.ComponentGuid, &GuidString);
  146. wprintf( L"Component Data\n\tName: %ws\n\tGuid: %ws\n\tVersionMajor: %d\n\tVersionMinor: %d\n",
  147. cd.FriendlyName,GuidString,cd.VersionMajor,cd.VersionMinor);
  148. wprintf( L"ExceptionData\n\tInf: %ws\n\tCatalog: %ws\n",
  149. ed.ExceptionInfName,ed.CatalogFileName);
  150. CoTaskMemFree( GuidString );
  151. //
  152. // enumerate packages
  153. //
  154. i = 0;
  155. if (!SetupEnumerateRegisteredOsComponents( pComponentLister, (DWORD_PTR)&i)) {
  156. wprintf( L"Failed to enumerate components, ec = %d\n", GetLastError() );
  157. return 1;
  158. }
  159. wprintf( L"Done (%d enumerated components)!!!\n", i );
  160. return 0;
  161. }
  162.