Leaked source code of windows server 2003
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
7.9 KiB

  1. /******************************************************************************
  2. *
  3. * (C) COPYRIGHT MICROSOFT CORP., 2000
  4. *
  5. * TITLE: PTP.cpp
  6. *
  7. * VERSION: 1.0
  8. *
  9. * AUTHOR: KeisukeT
  10. *
  11. * DATE: 23 Apr, 2002
  12. *
  13. * DESCRIPTION:
  14. * Utility function for PTP device access and coinstaller entry.
  15. *
  16. * NOTE:
  17. *
  18. *******************************************************************************/
  19. //
  20. // Precompiled header
  21. //
  22. #include "precomp.h"
  23. #include <strsafe.h>
  24. #include <cfgmgr32.h>
  25. #include "sti_ci.h"
  26. #include "cistr.h"
  27. #include "debug.h"
  28. #pragma hdrstop
  29. //
  30. // Include
  31. //
  32. //
  33. // Define
  34. //
  35. #define MAX_STRING_BUFFER 256
  36. #define PTPUSD_DLL L"ptpusd.dll"
  37. #define FUNTION_GETDEVICENAME "GetDeviceName"
  38. //
  39. // Typedef
  40. //
  41. typedef HRESULT (WINAPI* GETDEVICENAME)(LPCWSTR pwszPortName,
  42. WCHAR *pwszManufacturer,
  43. DWORD cchManufacturer,
  44. WCHAR *pwszModelName,
  45. DWORD cchModelName
  46. );
  47. //
  48. // Prototype
  49. //
  50. //
  51. // Global
  52. //
  53. //
  54. // Function
  55. //
  56. extern "C"
  57. DWORD
  58. APIENTRY
  59. PTPCoinstallerEntry(
  60. IN DI_FUNCTION diFunction,
  61. IN HDEVINFO hDevInfo,
  62. IN PSP_DEVINFO_DATA pDevInfoData,
  63. IN OUT PCOINSTALLER_CONTEXT_DATA pCoinstallerContext
  64. )
  65. {
  66. DWORD dwReturn;
  67. DWORD dwWaitResult;
  68. WCHAR wszMfg[MAX_STRING_BUFFER];
  69. WCHAR wszModel[MAX_STRING_BUFFER];
  70. CString csSymbolicLink;
  71. CString csMfg;
  72. CString csModel;
  73. CString csDeviceID;
  74. HKEY hDevRegKey;
  75. HRESULT hr;
  76. HANDLE hThread;
  77. HMODULE hDll;
  78. GETDEVICENAME pfnGetDeviceName;
  79. DebugTrace(TRACE_PROC_ENTER,(("PTPCoinstallerEntry: Enter... \r\n")));
  80. //
  81. // Initialize local.
  82. //
  83. dwReturn = NO_ERROR;
  84. dwWaitResult = 0;
  85. hDevRegKey = NULL;
  86. hr = S_OK;
  87. hThread = NULL;
  88. pfnGetDeviceName = NULL;
  89. hDll = NULL;
  90. memset(wszMfg, 0, sizeof(wszMfg));
  91. memset(wszModel, 0, sizeof(wszModel));
  92. switch(diFunction){
  93. case DIF_INSTALLDEVICE:
  94. {
  95. if(pCoinstallerContext->PostProcessing){
  96. //
  97. // Open device regkey.
  98. //
  99. hDevRegKey = SetupDiOpenDevRegKey(hDevInfo,
  100. pDevInfoData,
  101. DICS_FLAG_GLOBAL,
  102. 0,
  103. DIREG_DRV,
  104. KEY_READ | KEY_WRITE);
  105. if(!IS_VALID_HANDLE(hDevRegKey)){
  106. DebugTrace(TRACE_STATUS,(("PTPCoinstallerEntry: Unable to open driver key for isntalling device. Err=0x%x.\r\n"), GetLastError()));
  107. goto PTPCoinstallerEntry_return;
  108. } // if(!IS_VALID_HANDLE(hDevRegKey))
  109. //
  110. // Get symbolic link of the installing device.
  111. //
  112. csSymbolicLink.Load(hDevRegKey, CREATEFILENAME);
  113. if(csSymbolicLink.IsEmpty()){
  114. DebugTrace(TRACE_ERROR,(("PTPCoinstallerEntry: ERROR!! Unable to get symbolic link. Err=0x%x.\r\n"), GetLastError()));
  115. goto PTPCoinstallerEntry_return;
  116. } // if(csSymbolicLink.IsEmpty())
  117. DebugTrace(TRACE_STATUS,(("PTPCoinstallerEntry: CreateFileName=%ws.\r\n"), (LPWSTR)csSymbolicLink));
  118. //
  119. // Load ptpusd.dll..
  120. //
  121. hDll = LoadLibrary(PTPUSD_DLL);
  122. if(!IS_VALID_HANDLE(hDll)){
  123. DebugTrace(TRACE_ERROR,(("PTPCoinstallerEntry: ERROR!! Unable to load %ws. Err=0x%x.\r\n"), PTPUSD_DLL, GetLastError()));
  124. goto PTPCoinstallerEntry_return;
  125. } // if(!IS_VALID_HANDLE(hDll))
  126. //
  127. // Get proc address of GetDeviceName from ptpusd.dll.
  128. //
  129. pfnGetDeviceName = (GETDEVICENAME)GetProcAddress(hDll, FUNTION_GETDEVICENAME);
  130. if(NULL == pfnGetDeviceName){
  131. DebugTrace(TRACE_ERROR,(("PTPCoinstallerEntry: ERROR!! Unable to get proc address. Err=0x%x.\r\n"), GetLastError()));
  132. goto PTPCoinstallerEntry_return;
  133. } // if(NULL == pfnGetDeviceName)
  134. //
  135. // Call the function to get the device info.
  136. //
  137. _try {
  138. hr = pfnGetDeviceName(csSymbolicLink,
  139. wszMfg,
  140. ARRAYSIZE(wszMfg),
  141. wszModel,
  142. ARRAYSIZE(wszModel));
  143. }
  144. _except(EXCEPTION_EXECUTE_HANDLER) {
  145. DebugTrace(TRACE_ERROR,(("PTPCoinstallerEntry: ERROR!! excpetion in ptpusd.dll.\r\n")));
  146. goto PTPCoinstallerEntry_return;
  147. } // _except(EXCEPTION_EXECUTE_HANDLER)
  148. if(S_OK != hr){
  149. DebugTrace(TRACE_ERROR,(("PTPCoinstallerEntry: ERROR!! Unable to get device info from device. hr=0x%x.\r\n"), hr));
  150. goto PTPCoinstallerEntry_return;
  151. } // if(S_OK != hr)
  152. DebugTrace(TRACE_STATUS,(("PTPCoinstallerEntry: Manufacturer name=%ws.\r\n"), wszMfg));
  153. DebugTrace(TRACE_STATUS,(("PTPCoinstallerEntry: Model name=%ws.\r\n"), wszModel));
  154. //
  155. // We will need to generate unique FriendlyName.
  156. //
  157. //
  158. // Store Vendor, FriendlyName and DriverDesc.
  159. //
  160. csMfg = wszMfg;
  161. csModel = wszModel;
  162. csMfg.Store(hDevRegKey, VENDOR);
  163. csModel.Store(hDevRegKey, FRIENDLYNAME);
  164. csModel.Store(hDevRegKey, DRIVERDESC);
  165. CM_Set_DevNode_Registry_Property(pDevInfoData->DevInst,
  166. CM_DRP_FRIENDLYNAME,
  167. (LPTSTR)csModel,
  168. (lstrlen(csModel) + 1) * sizeof(TCHAR),
  169. 0);
  170. } else { // if(pCoinstallerContext->PostProcessing)
  171. dwReturn = ERROR_DI_POSTPROCESSING_REQUIRED;
  172. } // else(pCoinstallerContext->PostProcessing)
  173. break;
  174. } // case DIF_INSTALLDEVICE:
  175. } // switch(diFunction)
  176. PTPCoinstallerEntry_return:
  177. //
  178. // Clean up.
  179. //
  180. if(IS_VALID_HANDLE(hDevRegKey)){
  181. RegCloseKey(hDevRegKey);
  182. hDevRegKey = (HKEY)INVALID_HANDLE_VALUE;
  183. } // if(IS_VALID_HANDLE(hDevRegKey))
  184. if( (DIF_INSTALLDEVICE == diFunction)
  185. && (pCoinstallerContext->PostProcessing) )
  186. {
  187. if(IS_VALID_HANDLE(hDll)){
  188. FreeLibrary(hDll);
  189. hDll = NULL;
  190. } // if(IS_VALID_HANDLE(hDll))
  191. } // if(DIF_DESTROYPRIVATEDATA == diFunction)
  192. DebugTrace(TRACE_PROC_LEAVE,(("PTPCoinstallerEntry: Leaving... Ret=0x%x.\r\n"), dwReturn));
  193. return dwReturn;
  194. } // PTPCoinstallerEntry