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.

254 lines
6.6 KiB

  1. #include <iostream>
  2. using namespace std;
  3. #include <windows.h>
  4. #include <stdio.h>
  5. #include <malloc.h>
  6. #include <stdlib.h>
  7. #include <stdarg.h>
  8. #include <setupapi.h>
  9. #include <cdm.h>
  10. //
  11. // Compile #if 0 for LogDriverNotFound test and #if 1 for original code
  12. //
  13. #if 1
  14. //Win 98 DOWNLOADINFO
  15. typedef struct _DOWNLOADINFOWIN98
  16. {
  17. DWORD dwDownloadInfoSize; //size of this structure
  18. LPTSTR lpHardwareIDs; //multi_sz list of Hardware PnP IDs
  19. LPTSTR lpCompatIDs; //multi_sz list of compatible IDs
  20. LPTSTR lpFile; //File name (string)
  21. OSVERSIONINFO OSVersionInfo; //OSVERSIONINFO from GetVersionEx()
  22. DWORD dwFlags; //Flags
  23. DWORD dwClientID; //Client ID
  24. } DOWNLOADINFOWIN98, *PDOWNLOADINFOWIN98;
  25. typedef BOOL (*PFN_DownloadGetUpdatedFiles)(
  26. PDOWNLOADINFOWIN98 pDownLoadInfoWin98,
  27. LPSTR lpDownloadPath,
  28. UINT uSize
  29. );
  30. void DoNT(int argc, char *argv[]);
  31. void Do9x(int argc, char *argv[]);
  32. void usage(bool fNT);
  33. bool GetDriverPackage(PDOWNLOADINFO pinfo, LPWSTR wszPath);
  34. int __cdecl main(int argc, char *argv[])
  35. {
  36. OSVERSIONINFO versionInformation;
  37. versionInformation.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  38. GetVersionEx(&versionInformation);
  39. if (versionInformation.dwPlatformId == VER_PLATFORM_WIN32_NT)
  40. DoNT(argc, argv);
  41. else
  42. Do9x(argc, argv);
  43. return 0;
  44. }
  45. void DoNT(int argc, char *argv[])
  46. {
  47. if ( argc < 3 )
  48. {
  49. usage(true);
  50. return;
  51. }
  52. DOWNLOADINFO info;
  53. ZeroMemory(&info, sizeof(info));
  54. info.dwDownloadInfoSize = sizeof(DOWNLOADINFO);
  55. info.dwArchitecture = PROCESSOR_ARCHITECTURE_UNKNOWN;
  56. bool fHwId = true;
  57. if ( 0 == strcmp(argv[1], "0") )
  58. {
  59. fHwId = false;
  60. }
  61. else if ( 0 == strcmp(argv[1], "1") )
  62. {
  63. if ( 3 < argc )
  64. {
  65. // check 3rd param
  66. if ( 0 == strcmp(argv[3], "PROCESSOR_ARCHITECTURE_INTEL") )
  67. info.dwArchitecture = PROCESSOR_ARCHITECTURE_INTEL;
  68. else if ( 0 == strcmp(argv[3], "PROCESSOR_ARCHITECTURE_ALPHA") )
  69. info.dwArchitecture = PROCESSOR_ARCHITECTURE_ALPHA;
  70. }
  71. }
  72. else
  73. {
  74. usage(true);
  75. return;
  76. }
  77. WCHAR wszParam[2048];
  78. MultiByteToWideChar(CP_ACP, 0, argv[2], -1, wszParam, 2048);
  79. LPCWSTR wszHardwareID = NULL;
  80. LPCWSTR wszDeviceInstanceID = NULL;
  81. if ( fHwId )
  82. info.lpHardwareIDs = wszParam;
  83. else
  84. info.lpDeviceInstanceID = wszParam;
  85. WCHAR wszPath[MAX_PATH];
  86. if ( GetDriverPackage(&info, wszPath) )
  87. {
  88. char szPath[MAX_PATH];
  89. WideCharToMultiByte(CP_ACP, 0, wszPath, -1, szPath, sizeof(szPath), NULL, NULL);
  90. cout << "TESTCDM SUCCESS:Downloaded Driver Files @ " << szPath << endl;
  91. }
  92. else
  93. {
  94. cout << "TESTCDM FAILED:Return value = " << GetLastError() << endl;
  95. }
  96. }
  97. void Do9x(int argc, char *argv[])
  98. {
  99. if ( argc < 2 )
  100. {
  101. usage(false);
  102. return;
  103. }
  104. DOWNLOADINFOWIN98 info;
  105. ZeroMemory(&info, sizeof(info));
  106. info.dwDownloadInfoSize = sizeof(DOWNLOADINFO);
  107. info.lpHardwareIDs = argv[1];
  108. HMODULE hModule = LoadLibrary("cdm.dll");
  109. if ( !hModule )
  110. {
  111. cout << "TESTCDM ERROR:cdm.dll Load Library Failed. Return value = " << GetLastError() << endl;
  112. return;
  113. }
  114. PFN_DownloadGetUpdatedFiles fpDownloadGetUpdatedFiles = (PFN_DownloadGetUpdatedFiles)GetProcAddress(hModule, "DownloadGetUpdatedFiles");
  115. if ( !fpDownloadGetUpdatedFiles )
  116. {
  117. cout << "TESTCDM ERROR:GetProcAddress failed." << endl;
  118. return;
  119. }
  120. char szPath[MAX_PATH];
  121. if ( !fpDownloadGetUpdatedFiles(&info, szPath, sizeof(szPath)) )
  122. {
  123. cout << "TESTCDM ERROR:fpDownloadGetUpdatedFiles failed. Return value = " << GetLastError() << endl;
  124. }
  125. else
  126. {
  127. cout << "TESTCDM SUCCESS:Downloaded Driver Files @ " << szPath << endl;
  128. }
  129. }
  130. void usage(bool fNT)
  131. {
  132. if (fNT)
  133. {
  134. cout << "USAGE on NT: testcdm 0 [DevInst ID]" << endl
  135. << " testcdm 1 [Hardware ID]" << endl
  136. << " testcdm 1 [Hardware ID] PROCESSOR_ARCHITECTURE_INTEL" << endl
  137. << " testcdm 1 [Hardware ID] PROCESSOR_ARCHITECTURE_ALPHA" << endl;
  138. }
  139. else
  140. {
  141. cout << "USAGE on 9x: testcdm [Hardware ID]" << endl;
  142. }
  143. }
  144. bool GetDriverPackage(PDOWNLOADINFO pinfo, LPWSTR wszPath)
  145. {
  146. HMODULE hModule = LoadLibrary("cdm.dll");
  147. if ( !hModule )
  148. {
  149. cout << "TESTCDM ERROR:cdm.dll Load Library Failed. Return value = " << GetLastError() << endl;
  150. return false;
  151. }
  152. CDM_INTERNET_AVAILABLE_PROC fpDownloadIsInternetAvailable = (CDM_INTERNET_AVAILABLE_PROC)GetProcAddress(hModule, "DownloadIsInternetAvailable");
  153. OPEN_CDM_CONTEXT_PROC fpOpenCDMContext = (OPEN_CDM_CONTEXT_PROC)GetProcAddress(hModule, "OpenCDMContext");
  154. DOWNLOAD_UPDATED_FILES_PROC fpDownloadUpdatedFiles = (DOWNLOAD_UPDATED_FILES_PROC)GetProcAddress(hModule, "DownloadUpdatedFiles");
  155. CLOSE_CDM_CONTEXT_PROC fpCloseCDMContext = (CLOSE_CDM_CONTEXT_PROC)GetProcAddress(hModule, "CloseCDMContext");
  156. if ( !fpDownloadIsInternetAvailable || !fpOpenCDMContext || !fpDownloadUpdatedFiles || !fpCloseCDMContext )
  157. {
  158. cout << "TESTCDM ERROR:GetProcAddress failed." << endl;
  159. return false;
  160. }
  161. if (!(*fpDownloadIsInternetAvailable)())
  162. {
  163. cout << "TESTCDM ERROR:GInternat is not available." << endl;
  164. return false;
  165. }
  166. HANDLE handle = (*fpOpenCDMContext)(NULL);
  167. if ( !handle )
  168. {
  169. cout << "TESTCDM ERROR:fpOpenCDMContext failed. GetLastError = "<< GetLastError() << endl;
  170. return false;
  171. }
  172. UINT uRequired;
  173. BOOL bRc = (*fpDownloadUpdatedFiles)(handle, NULL, pinfo, wszPath, MAX_PATH, &uRequired);
  174. (*fpCloseCDMContext)(handle);
  175. FreeLibrary(hModule);
  176. return TRUE == bRc;
  177. }
  178. #else
  179. int __cdecl main(int argc, char * argv[], char * envp[])
  180. {
  181. HMODULE hLib;
  182. INT nTimes = 0;
  183. BOOL fBatchMode = FALSE;
  184. INT nEndFlag = 2; //flush logging to file for every device
  185. if (2 <= argc)
  186. {
  187. nTimes = atoi(argv[1]);
  188. }
  189. if (3 <= argc)
  190. {
  191. fBatchMode = TRUE;
  192. nEndFlag = 0; //hold off flushing
  193. }
  194. if (0 == nTimes)
  195. {
  196. nTimes = 1;
  197. }
  198. hLib = LoadLibrary("cdm.dll");
  199. if (!hLib)
  200. {
  201. cout << "Load lib failed" << endl;
  202. return -1;
  203. }
  204. OPEN_CDM_CONTEXT_EX_PROC fpOpenCDMContextEx = (OPEN_CDM_CONTEXT_EX_PROC)GetProcAddress(hLib, "OpenCDMContextEx");
  205. LOG_DRIVER_NOT_FOUND_PROC fpLogDriverNotFound = (LOG_DRIVER_NOT_FOUND_PROC)GetProcAddress(hLib, "LogDriverNotFound");
  206. CLOSE_CDM_CONTEXT_PROC fpCloseCDMContext = (CLOSE_CDM_CONTEXT_PROC)GetProcAddress(hLib, "CloseCDMContext");
  207. if (!fpOpenCDMContextEx || !fpLogDriverNotFound || !fpCloseCDMContext)
  208. {
  209. cout << "Get proc address failed" << endl;
  210. return -1;
  211. }
  212. HANDLE hCtxt = (*fpOpenCDMContextEx)(false);
  213. for (int i = 0; i < nTimes; i++)
  214. {
  215. (*fpLogDriverNotFound)(hCtxt, L"PCI\\VEN_1011&DEV_0024&SUBSYS_00000000&REV_03\\2&ebb567f&0&78", nEndFlag);
  216. printf("device %d logged, sleep 2 secs\n", i+1);
  217. Sleep(2000);
  218. }
  219. //(*fpLogDriverNotFound)(hCtxt, L"PCI\\VEN_10B7&DEV_9055&SUBSYS_00821028&REV_24\\2&ebb567f&0&88", 0);
  220. (*fpLogDriverNotFound)(hCtxt, L"DISPLAY\\Default_Monitor\\4&2e81f5bd&0&80000000&01&00", 2);
  221. (*fpCloseCDMContext)(hCtxt);
  222. return 0;
  223. }
  224. #endif