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.

237 lines
7.5 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 <objbase.h>
  10. #include <shlwapi.h>
  11. #include <wustl.h>
  12. #include <download.h>
  13. #include "..\cdm.h"
  14. void usage();
  15. void SimulateAU();
  16. void FindUpdate(PDOWNLOADINFO pinfo);
  17. void DownloadUpdate(PDOWNLOADINFO pinfo);
  18. void __cdecl main(int argc, char *argv[])
  19. {
  20. DOWNLOADINFO info;
  21. ZeroMemory(&info, sizeof(info));
  22. info.dwDownloadInfoSize = sizeof(DOWNLOADINFO);
  23. info.dwArchitecture = PROCESSOR_ARCHITECTURE_UNKNOWN;
  24. if ( 2 == argc && 0 == lstrcmpi(argv[1], "SimulateAU") )
  25. {
  26. SimulateAU();
  27. }
  28. else if ( 4 == argc && 0 == lstrcmpi(argv[1], "FindUpdate") && 0 == lstrcmpi(argv[2], "devinst") )
  29. {
  30. WCHAR wszParam[2048];
  31. MultiByteToWideChar(CP_ACP, 0, argv[3], -1, wszParam, 2048);
  32. info.lpDeviceInstanceID = wszParam;
  33. FindUpdate(&info);
  34. }
  35. else if ( 4 == argc && 0 == lstrcmpi(argv[1], "FindUpdate") && 0 == lstrcmpi(argv[2], "hwid") )
  36. {
  37. WCHAR wszParam[2048];
  38. MultiByteToWideChar(CP_ACP, 0, argv[3], -1, wszParam, 2048);
  39. info.lpHardwareIDs = wszParam;
  40. FindUpdate(&info);
  41. }
  42. else if ( 4 == argc && 0 == lstrcmpi(argv[1], "DownloadUpdate") && 0 == lstrcmpi(argv[2], "devinst") )
  43. {
  44. WCHAR wszParam[2048];
  45. MultiByteToWideChar(CP_ACP, 0, argv[3], -1, wszParam, 2048);
  46. info.lpDeviceInstanceID = wszParam;
  47. DownloadUpdate(&info);
  48. }
  49. else if ( 4 == argc && 0 == lstrcmpi(argv[1], "DownloadUpdate") && 0 == lstrcmpi(argv[2], "hwid") )
  50. {
  51. WCHAR wszParam[2048];
  52. MultiByteToWideChar(CP_ACP, 0, argv[3], -1, wszParam, 2048);
  53. info.lpHardwareIDs = wszParam;
  54. DownloadUpdate(&info);
  55. }
  56. else
  57. {
  58. usage();
  59. }
  60. }
  61. void usage()
  62. {
  63. cout << "usage:" << endl
  64. << " CdmAu SimulateAU" << endl
  65. << " CdmAu FindUpdate devinst [DevInst ID]" << endl
  66. << " CdmAu FindUpdate hwid [Hardware ID]" << endl
  67. << " CdmAu DownloadUpdate devinst [DevInst ID]" << endl
  68. << " CdmAu DownloadUpdate hwid [Hardware ID]" << endl;
  69. }
  70. void QueryDetectionFilesCallback( void* pCallbackParam, LPCSTR pszURL, LPCSTR pszLocalFile)
  71. {
  72. printf(" %s - %s\n", pszURL, pszLocalFile);
  73. // download file
  74. CDownload download;
  75. char szURL[INTERNET_MAX_PATH_LENGTH];
  76. strcpy(szURL, pszURL);
  77. char* pszServerFile = strrchr(szURL, '/');
  78. *pszServerFile = 0;
  79. pszServerFile ++;
  80. char szDir[MAX_PATH];
  81. strcpy(szDir, pszLocalFile);
  82. *strrchr(szDir, '\\') = 0;
  83. char szDirCabs[MAX_PATH];
  84. strcpy(szDirCabs, szDir);
  85. *strrchr(szDirCabs, '\\') = 0;
  86. CreateDirectory(szDirCabs, NULL);
  87. CreateDirectory(szDir, NULL);
  88. if(!download.Connect(szURL))
  89. {
  90. printf(" ERROR - Connect(%s) fails\n", szURL);
  91. return;
  92. }
  93. if(!download.Copy(pszServerFile, pszLocalFile))
  94. {
  95. printf(" ERROR - Copy(%s, %s) fails\n", pszServerFile, pszLocalFile);
  96. return;
  97. }
  98. }
  99. void SimulateAU()
  100. {
  101. cout << "--- SimulateAU ---" << endl;
  102. auto_hlib hlib = LoadLibrary("cdm.dll");
  103. if ( !hlib.valid() )
  104. {
  105. cout << " ERROR: cdm.dll Load Library Failed. Return value = " << GetLastError() << endl;
  106. return;
  107. }
  108. OPEN_CDM_CONTEXT_EX_PROC fpOpenCDMContextEx = (OPEN_CDM_CONTEXT_EX_PROC)GetProcAddress(hlib, "OpenCDMContextEx");
  109. QUERY_DETECTION_FILES_PROC fpQueryDetectionFiles = (QUERY_DETECTION_FILES_PROC)GetProcAddress(hlib, "QueryDetectionFiles");
  110. DET_FILES_DOWNLOADED_PROC fpDetFilesDownloaded = (DET_FILES_DOWNLOADED_PROC)GetProcAddress(hlib, "DetFilesDownloaded");
  111. CLOSE_CDM_CONTEXT_PROC fpCloseCDMContext = (CLOSE_CDM_CONTEXT_PROC)GetProcAddress(hlib, "CloseCDMContext");
  112. if ( !fpOpenCDMContextEx || !fpQueryDetectionFiles || !fpDetFilesDownloaded || !fpCloseCDMContext )
  113. {
  114. cout << " ERROR: GetProcAddress failed." << endl;
  115. return;
  116. }
  117. HANDLE handle = (fpOpenCDMContextEx)(TRUE);
  118. if ( !handle )
  119. {
  120. cout << " ERROR: OpenCDMContextEx failed. GetLastError = "<< GetLastError() << endl;
  121. return;
  122. }
  123. int nCount = (*fpQueryDetectionFiles)(handle, 0, QueryDetectionFilesCallback);
  124. (fpDetFilesDownloaded)(handle);
  125. (fpCloseCDMContext)(handle);
  126. if (-1 == nCount)
  127. cout << " ERROR: QueryDetectionFiles failed. GetLastError = "<< GetLastError() << endl;
  128. else
  129. cout << " SUCCESS: " << nCount << " files downloaded, ready for offline detection" << endl;
  130. }
  131. void FindUpdate(PDOWNLOADINFO pinfo)
  132. {
  133. cout << "--- FindUpdate ---" << endl;
  134. auto_hlib hlib = LoadLibrary("cdm.dll");
  135. if ( !hlib.valid() )
  136. {
  137. cout << " ERROR: cdm.dll Load Library Failed. Return value = " << GetLastError() << endl;
  138. return;
  139. }
  140. OPEN_CDM_CONTEXT_EX_PROC fpOpenCDMContextEx = (OPEN_CDM_CONTEXT_EX_PROC)GetProcAddress(hlib, "OpenCDMContextEx");
  141. FIND_MATCHING_DRIVER_PROC fpFindMatchingDriver = (FIND_MATCHING_DRIVER_PROC)GetProcAddress(hlib, "FindMatchingDriver");
  142. CLOSE_CDM_CONTEXT_PROC fpCloseCDMContext = (CLOSE_CDM_CONTEXT_PROC)GetProcAddress(hlib, "CloseCDMContext");
  143. if ( !fpOpenCDMContextEx|| !fpFindMatchingDriver || !fpCloseCDMContext )
  144. {
  145. cout << " ERROR: GetProcAddress failed." << endl;
  146. return;
  147. }
  148. HANDLE handle = (fpOpenCDMContextEx)(FALSE);
  149. if ( !handle )
  150. {
  151. cout << " ERROR: OpenCDMContextEx failed. GetLastError = "<< GetLastError() << endl;
  152. return;
  153. }
  154. WUDRIVERINFO wudrvinfo;
  155. BOOL fRC = (*fpFindMatchingDriver)(handle, pinfo, &wudrvinfo);
  156. DWORD dwError = GetLastError();
  157. (fpCloseCDMContext)(handle);
  158. if (!fRC)
  159. {
  160. if (dwError == ERROR_INVALID_FUNCTION)
  161. cout << " Not ready for offline detection " << endl;
  162. else
  163. cout << " No update is found. GetLastError = "<< dwError << endl;
  164. return;
  165. }
  166. cout << " SUCCESS: CDM has driver" << endl;
  167. char szTmp[HWID_LEN];
  168. WideCharToMultiByte(CP_ACP, 0, wudrvinfo.wszHardwareID, -1, szTmp, sizeof(szTmp), NULL, NULL);
  169. cout << " HardwareID = " << szTmp << endl;
  170. WideCharToMultiByte(CP_ACP, 0, wudrvinfo.wszDescription, -1, szTmp, sizeof(szTmp), NULL, NULL);
  171. cout << " Description = " << szTmp << endl;
  172. WideCharToMultiByte(CP_ACP, 0, wudrvinfo.wszMfgName, -1, szTmp, sizeof(szTmp), NULL, NULL);
  173. cout << " MfgName = " << szTmp << endl;
  174. WideCharToMultiByte(CP_ACP, 0, wudrvinfo.wszProviderName, -1, szTmp, sizeof(szTmp), NULL, NULL);
  175. cout << " ProviderName = " << szTmp << endl;
  176. WideCharToMultiByte(CP_ACP, 0, wudrvinfo.wszDriverVer, -1, szTmp, sizeof(szTmp), NULL, NULL);
  177. cout << " DriverVer = " << szTmp << endl;
  178. }
  179. void DownloadUpdate(PDOWNLOADINFO pinfo)
  180. {
  181. cout << "--- DownloadUpdate ---" << endl;
  182. auto_hlib hlib = LoadLibrary("cdm.dll");
  183. if ( !hlib.valid() )
  184. {
  185. cout << " ERROR: cdm.dll Load Library Failed. Return value = " << GetLastError() << endl;
  186. return;
  187. }
  188. OPEN_CDM_CONTEXT_EX_PROC fpOpenCDMContextEx = (OPEN_CDM_CONTEXT_EX_PROC)GetProcAddress(hlib, "OpenCDMContextEx");
  189. DOWNLOAD_UPDATED_FILES_PROC fpDownloadUpdatedFiles = (DOWNLOAD_UPDATED_FILES_PROC)GetProcAddress(hlib, "DownloadUpdatedFiles");
  190. CLOSE_CDM_CONTEXT_PROC fpCloseCDMContext = (CLOSE_CDM_CONTEXT_PROC)GetProcAddress(hlib, "CloseCDMContext");
  191. if ( !fpOpenCDMContextEx|| !fpDownloadUpdatedFiles || !fpCloseCDMContext )
  192. {
  193. cout << " ERROR: GetProcAddress failed." << endl;
  194. return;
  195. }
  196. HANDLE handle = (fpOpenCDMContextEx)(TRUE);
  197. if ( !handle )
  198. {
  199. cout << " ERROR: OpenCDMContextEx failed. GetLastError = "<< GetLastError() << endl;
  200. return;
  201. }
  202. UINT uRequired;
  203. WCHAR wszPath[MAX_PATH];
  204. BOOL fRC = (fpDownloadUpdatedFiles)(handle, NULL, pinfo, wszPath, MAX_PATH, &uRequired);
  205. DWORD dwError = GetLastError();
  206. (fpCloseCDMContext)(handle);
  207. if (!fRC)
  208. {
  209. cout << " ERROR: DownloadUpdatedFiles failed. GetLastError = "<< dwError << endl;
  210. return;
  211. }
  212. char szPath[MAX_PATH];
  213. WideCharToMultiByte(CP_ACP, 0, wszPath, -1, szPath, sizeof(szPath), NULL, NULL);
  214. cout << "SUCCESS:Downloaded Driver Files @ " << szPath << endl;
  215. }