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.

1720 lines
49 KiB

  1. //
  2. // Copyright (c) 1999 Microsoft Corporation
  3. //
  4. // HIDCOM.EXE -- exploratory USB Phone Console Application
  5. //
  6. // audio.cpp -- audio magic
  7. //
  8. // Zoltan Szilagyi, July - August, 1999
  9. //
  10. // Prioritized to-do list:
  11. //
  12. // * Convert printfs to debug tracing, and define debug tracing to
  13. // printfs for HidCom.Exe use.
  14. //
  15. // * GetInstanceFromDeviceName should look only for audio devices.
  16. // This should somewhat reduce the 2 sec wave enumeration time.
  17. // Don't forget to remove timing debug output.
  18. //
  19. // * Consider changing FindWaveIdFromHardwareIdString and its helpers to take a
  20. // devinst only and computer the hardware ids on the fly rather than storing
  21. // them in arrays. This would slow it down but make the code simpler.
  22. //
  23. // * Small one-time memory leak: The static arrays of hardware ID
  24. // strings are leaked. That's a few KB per process, no increase over
  25. // time. If we make this a class then we'll just deallocate those
  26. // arrays in the destructor.
  27. // Also, for PNP events that cause us to recompute the mapping, we will
  28. // need to destroy the array at some point if the wave devices change.
  29. // So we need to augment the interface for this.
  30. //
  31. #include <wtypes.h>
  32. #include <stdio.h>
  33. #include "audio.h" // our own prototypes
  34. #include <cfgmgr32.h> // CM_ functions
  35. #include <setupapi.h> // SetupDi functions
  36. #include <mmsystem.h> // wave functions
  37. #include <initguid.h>
  38. #include <devguid.h> // device guids
  39. //
  40. // mmddkp.h -- private winmm header file
  41. // This is in nt\private\inc, but one must ssync and build in
  42. // nt\private\genx\windows\inc before it will show up.
  43. //
  44. #include <mmddkp.h>
  45. #include <crtdbg.h>
  46. #define ASSERT _ASSERTE
  47. #ifdef DBG
  48. #define STATIC
  49. #else
  50. #define STATIC static
  51. #endif
  52. extern "C"
  53. {
  54. #include "mylog.h"
  55. }
  56. //////////////////////////////////////////////////////////////////////////////
  57. //////////////////////////////////////////////////////////////////////////////
  58. //////////////////////////////////////////////////////////////////////////////
  59. //
  60. //
  61. // Private helper functions
  62. //
  63. //
  64. //////////////////////////////////////////////////////////////////////////////
  65. //////////////////////////////////////////////////////////////////////////////
  66. //////////////////////////////////////////////////////////////////////////////
  67. //////////////////////////////////////////////////////////////////////////////
  68. //
  69. // MapConfigRetToWin32
  70. //
  71. // This routine maps some CM error return codes to Win32 return codes, and
  72. // maps everything else to the value specied by dwDefault. This function is
  73. // adapted almost verbatim from the SetupAPI code.
  74. //
  75. // Arguments:
  76. // CmReturnCode - IN - specifies the ConfigMgr return code to be mapped
  77. // dwDefault - IN - specifies the default value to use if no explicit
  78. // mapping applies
  79. //
  80. // Return values:
  81. // Setup API (Win32) error code
  82. //
  83. STATIC DWORD
  84. MapConfigRetToWin32(
  85. IN CONFIGRET CmReturnCode,
  86. IN DWORD dwDefault
  87. )
  88. {
  89. switch(CmReturnCode) {
  90. case CR_SUCCESS :
  91. return NO_ERROR;
  92. case CR_CALL_NOT_IMPLEMENTED :
  93. return ERROR_CALL_NOT_IMPLEMENTED;
  94. case CR_OUT_OF_MEMORY :
  95. return ERROR_NOT_ENOUGH_MEMORY;
  96. case CR_INVALID_POINTER :
  97. return ERROR_INVALID_USER_BUFFER;
  98. case CR_INVALID_DEVINST :
  99. return ERROR_NO_SUCH_DEVINST;
  100. case CR_INVALID_DEVICE_ID :
  101. return ERROR_INVALID_DEVINST_NAME;
  102. case CR_ALREADY_SUCH_DEVINST :
  103. return ERROR_DEVINST_ALREADY_EXISTS;
  104. case CR_INVALID_REFERENCE_STRING :
  105. return ERROR_INVALID_REFERENCE_STRING;
  106. case CR_INVALID_MACHINENAME :
  107. return ERROR_INVALID_MACHINENAME;
  108. case CR_REMOTE_COMM_FAILURE :
  109. return ERROR_REMOTE_COMM_FAILURE;
  110. case CR_MACHINE_UNAVAILABLE :
  111. return ERROR_MACHINE_UNAVAILABLE;
  112. case CR_NO_CM_SERVICES :
  113. return ERROR_NO_CONFIGMGR_SERVICES;
  114. case CR_ACCESS_DENIED :
  115. return ERROR_ACCESS_DENIED;
  116. case CR_NOT_DISABLEABLE:
  117. return ERROR_NOT_DISABLEABLE;
  118. default :
  119. return dwDefault;
  120. }
  121. }
  122. //////////////////////////////////////////////////////////////////////////////
  123. //
  124. // MapConfigRetToHResult
  125. //
  126. // This routine maps some CM error return codes to HRESULT return codes, and
  127. // maps everything else to the HRESULT value E_FAIL.
  128. //
  129. // Arguments:
  130. // CmReturnCode - IN - specifies the ConfigMgr return code to be mapped
  131. //
  132. // Return values:
  133. // HRESULT error code
  134. //
  135. STATIC HRESULT
  136. MapConfigRetToHResult(
  137. IN CONFIGRET CmReturnCode
  138. )
  139. {
  140. DWORD dwWin32Error;
  141. HRESULT hr;
  142. //
  143. // Map configret --> win32
  144. //
  145. dwWin32Error = MapConfigRetToWin32(
  146. CmReturnCode,
  147. E_FAIL
  148. );
  149. //
  150. // Map win32 --> HRESULT
  151. // but don't try to map default E_FAIL, as it is not within the range for
  152. // a normal win32 error code.
  153. //
  154. if ( dwWin32Error == E_FAIL )
  155. {
  156. hr = E_FAIL;
  157. }
  158. else
  159. {
  160. hr = HRESULT_FROM_WIN32( dwWin32Error );
  161. }
  162. return hr;
  163. }
  164. //////////////////////////////////////////////////////////////////////////////
  165. //
  166. // CheckIfAncestor
  167. //
  168. // This function determines if one of the specified devnodes (the "proposed
  169. // ancestor") is an ancestor of the other specified devnode (the "proposed
  170. // descendant").
  171. //
  172. // The devnodes are arranged in a tree. If node A is an ancestor of node
  173. // B, it just means that node A is either equal to node B, or has a child
  174. // that is an ancestor of node B. This can also be stated in reverse --
  175. // node C is a descendant of node D if C is equal to D, or if C's parent
  176. // is a descendant of node D.
  177. //
  178. // The algorithm used here to determine ancestry is a straightforward
  179. // application of the definition, although the recursion is removed.
  180. //
  181. // Arguments:
  182. // dwDevInstProposedAncestor - IN - the proposed ancestor (see above)
  183. // dwDevInstProposedDescendant - IN - the proposed descendant (see above)
  184. // pfIsAncestor - OUT - returns bool value indicating if
  185. // the pa is an ancestor of the pd
  186. //
  187. // Return values:
  188. // S_OK - success
  189. // others - from CM_Get_Parent
  190. //
  191. STATIC HRESULT
  192. CheckIfAncestor(
  193. IN DWORD dwDevInstProposedAnscestor,
  194. IN DWORD dwDevInstProposedDescendant,
  195. OUT BOOL * pfIsAncestor
  196. )
  197. {
  198. ASSERT( ! IsBadWritePtr( pfIsAncestor, sizeof( BOOL ) ) );
  199. DWORD dwCurrNode;
  200. HRESULT hr;
  201. //
  202. // Initially, the current node is the proposed descendant.
  203. //
  204. dwCurrNode = dwDevInstProposedDescendant;
  205. while ( TRUE )
  206. {
  207. //
  208. // Check if this node is the proposed ancestor.
  209. // If so, the proposed ancestor is an ancestor of the
  210. // proposed descendant.
  211. //
  212. if ( dwCurrNode == dwDevInstProposedAnscestor )
  213. {
  214. *pfIsAncestor = TRUE;
  215. hr = S_OK;
  216. break;
  217. }
  218. //
  219. // Replace the current node with the current node's parent.
  220. //
  221. CONFIGRET cr;
  222. DWORD dwDevInstTemp;
  223. cr = CM_Get_Parent(
  224. & dwDevInstTemp, // out: parent's devinst dword
  225. dwCurrNode, // in: child's devinst dword
  226. 0 // in: flags: must be zero
  227. );
  228. if ( cr == CR_NO_SUCH_DEVNODE )
  229. {
  230. //
  231. // This means we've fallen off the top of the PNP tree -- the
  232. // proposed ancestor is not found in the proposed descendant's
  233. // parentage chain.
  234. //
  235. * pfIsAncestor = FALSE;
  236. hr = S_OK;
  237. break;
  238. }
  239. else if ( cr != CR_SUCCESS )
  240. {
  241. //
  242. // Some other error occured.
  243. //
  244. hr = MapConfigRetToHResult( cr );
  245. break;
  246. }
  247. dwCurrNode = dwDevInstTemp;
  248. }
  249. return hr;
  250. }
  251. //////////////////////////////////////////////////////////////////////////////
  252. //
  253. // FindClosestCommonAncestor
  254. //
  255. // Given a pair of devnodes identified by devinst DWORDs, this function
  256. // finds the devinst DWORD for the closest common ancestor of the two
  257. // devnodes.
  258. //
  259. // See CheckIfAncestor for a discussion of the concepts of ancestor and
  260. // descendant. Then, devnode C is a common ancestor of devnodes A and B if C
  261. // is an ancestor of A -AND- C is an ancestor of B. Any pair of devnodes has
  262. // at least one common ancestor, that being the root of the PNP tree. A pair
  263. // of devnodes may have more than one common ancestor. The set of common
  264. // ancestors of A and B has one UNIQUE member, called the closest common
  265. // ancestor, such that no other member of the set is a child of that node.
  266. //
  267. // You can compute the closest common ancestor of two nodes A and B by
  268. // constructing a chain of nodes going from the root of the tree to A through
  269. // all A's ancestors, and also doing the same for B. Comparing these chains
  270. // side by side, they must be the same in at least the first node (the root).
  271. // The closest common ancestor for A and B is the last node that is the same
  272. // for both chains.
  273. //
  274. // The algorithm used here is an alternative, relatively stateless approach
  275. // that can take more CPU time but uses less memory, doesn't involve any
  276. // allocations, and is much easier to write (the last being the overriding
  277. // consideration, as the PNP tree is in always shallow). The code simply walks
  278. // up A's chain of ancestors, checking if each node is an ancestor of B. The
  279. // first node for which this is true is the closest common ancestor of
  280. // A and B.
  281. //
  282. // Arguments:
  283. // dwDevInstOne - IN - the first node ('A' above)
  284. // dwDevInstTwo - IN - the other node ('B' above)
  285. // pdwDevInstResult - OUT - returns the closest common ancestor
  286. //
  287. // Return values:
  288. // S_OK - success
  289. // others - from CM_Get_Parent
  290. //
  291. STATIC HRESULT
  292. FindClosestCommonAncestor(
  293. IN DWORD dwDevInstOne,
  294. IN DWORD dwDevInstTwo,
  295. OUT DWORD * pdwDevInstResult
  296. )
  297. {
  298. ASSERT( ! IsBadWritePtr( pdwDevInstResult, sizeof( DWORD ) ) );
  299. HRESULT hr;
  300. BOOL fIsAncestor;
  301. DWORD dwDevInstCurr;
  302. //
  303. // For each node up the chain of #1's parents, starting from #1 itself...
  304. //
  305. dwDevInstCurr = dwDevInstOne;
  306. while ( TRUE )
  307. {
  308. //
  309. // Check if this node is also in the chain of #2's parents.
  310. //
  311. hr = CheckIfAncestor(
  312. dwDevInstCurr,
  313. dwDevInstTwo,
  314. & fIsAncestor
  315. );
  316. if ( FAILED(hr) )
  317. {
  318. return hr;
  319. }
  320. if ( fIsAncestor )
  321. {
  322. *pdwDevInstResult = dwDevInstCurr;
  323. return S_OK;
  324. }
  325. //
  326. // Get the next node in the chain of #1's parents.
  327. //
  328. CONFIGRET cr;
  329. DWORD dwDevInstTemp;
  330. cr = CM_Get_Parent(
  331. & dwDevInstTemp, // out: parent's devinst dword
  332. dwDevInstCurr, // in: child's devinst dword
  333. 0 // in: flags: must be zero
  334. );
  335. if ( cr != CR_SUCCESS )
  336. {
  337. //
  338. // dwDevInst has no parent, or some other error occured.
  339. //
  340. // This is always an error, because there must always
  341. // be a common parent somewhere up the chain -- the root of the PNP
  342. // tree!
  343. //
  344. return MapConfigRetToHResult( cr );
  345. }
  346. dwDevInstCurr = dwDevInstTemp;
  347. }
  348. }
  349. //////////////////////////////////////////////////////////////////////////////
  350. //
  351. // TrimHardwareIdString
  352. //
  353. // This function strips off extraneous parts of the hardware ID string as
  354. // it is expected to appear for USB devices. The remaining parts of the string
  355. // are those that identify the vendor, product, and product revision, which
  356. // are together used to match devices as belonging to the same composite or
  357. // compound device.
  358. //
  359. // (Actually, for devices A and B, it's not just A and B that are compared,
  360. // it's A and the closest common parent of A and B. This ensures that the case
  361. // of multiple identical phones in the same system is handled correctly. This
  362. // logic of course lives outside of this funtion, though.)
  363. //
  364. // As an example:
  365. // "hid\Vid_04a6&Pid_00b9&Rev_0010&Mi_04&Col01"
  366. // becomes "Vid_04a6&Pid_00b9&Rev_0010"
  367. //
  368. // Note that this function will routinely be applied to strings for non-USB
  369. // devices that will not be in the same format; that's ok, since those strings
  370. // will never match USB-generated strings, be they trimmed or not.
  371. //
  372. // Also, note that the hardware ID string as read from the registry actually
  373. // consists of multiple concatenated null-terminated strings, all terminated
  374. // by two consecutive null characters. This function just ignores strings
  375. // beyond the first, as the first contains all the info we need.
  376. //
  377. // Arguments:
  378. // wszHardwareId - IN - the string to trim (in place)
  379. //
  380. // Return values:
  381. // TRUE - the string looked like a valid USB hardware ID
  382. // FALSE - the string did not look like a valid USB hardware ID
  383. //
  384. STATIC BOOL
  385. TrimHardwareIdString(
  386. IN WCHAR * wszHardwareId
  387. )
  388. {
  389. ASSERT( ! IsBadStringPtrW( wszHardwareId, (DWORD) -1 ) );
  390. //
  391. // "volatile" is needed, otherwise the compiler blatantly ignores the
  392. // recalculation of dwSize after the first pass.
  393. //
  394. volatile DWORD dwSize;
  395. DWORD dwCurrPos;
  396. BOOL fValid = FALSE;
  397. DWORD dwNumSeparators = 0;
  398. //
  399. // Strip off leading characters up to and including the first \ from the
  400. // front. If there is no \ in the string, it is invalid.
  401. //
  402. dwSize = lstrlenW(wszHardwareId);
  403. for ( dwCurrPos = 0; dwCurrPos < dwSize; dwCurrPos ++ )
  404. {
  405. if ( wszHardwareId[ dwCurrPos ] == L'\\' )
  406. {
  407. MoveMemory(
  408. wszHardwareId, // dest
  409. wszHardwareId + dwCurrPos + 1, // source
  410. sizeof(WCHAR) * dwSize - dwCurrPos // size, in bytes
  411. );
  412. fValid = TRUE;
  413. break;
  414. }
  415. }
  416. if ( ! fValid )
  417. {
  418. return FALSE;
  419. }
  420. //
  421. // Strip off trailing characters starting from the third &.
  422. // A string with less than two & is rejected.
  423. //
  424. // Examples:
  425. //
  426. // Vid_04a6&Pid_00b9&Rev_0010&Mi_04&Col01
  427. // becomes Vid_04a6&Pid_00b9&Rev_0010
  428. //
  429. // Vid_04a6&Pid_00b9&Rev_0010&Mi_04
  430. // becomes Vid_04a6&Pid_00b9&Rev_0010
  431. //
  432. // CSC6835_DEV
  433. // is rejected
  434. //
  435. //
  436. // Must recompute size because we changed it above.
  437. // (And note that dwSize is declared as 'volatile'.)
  438. //
  439. dwSize = lstrlenW(wszHardwareId);
  440. for ( dwCurrPos = 0; dwCurrPos < dwSize; dwCurrPos ++ )
  441. {
  442. if ( wszHardwareId[ dwCurrPos ] == L'&' )
  443. {
  444. dwNumSeparators ++;
  445. if ( dwNumSeparators == 3 )
  446. {
  447. wszHardwareId[ dwCurrPos ] = L'\0';
  448. break;
  449. }
  450. }
  451. }
  452. if ( dwNumSeparators < 2 )
  453. {
  454. return FALSE;
  455. }
  456. else
  457. {
  458. return TRUE;
  459. }
  460. }
  461. //////////////////////////////////////////////////////////////////////////////
  462. //
  463. // DevInstGetIdString
  464. //
  465. // This function retrieves an id string or string set for a particular
  466. // devinst dword. The value is obtained from the registry, but the
  467. // Configuration Manager API hides the detail of where in the registry this
  468. // info lives.
  469. //
  470. // Arguments:
  471. // dwDevInst - IN - the devinst dword for which we want info
  472. // dwProperty - IN - the property to retrieve
  473. // pwszIdString - OUT - returns "new"ed Unicode string or string set.
  474. //
  475. // Return values:
  476. // S_OK - success
  477. // E_OUTOFMEMORY - out of memory during string allocation
  478. // E_UNEXPECTED - data type of returned ID is not string or mutli-string
  479. // others - from CM_Get_DevNode_Registry_PropertyW
  480. //
  481. STATIC HRESULT
  482. DevInstGetIdString(
  483. IN DWORD dwDevInst,
  484. IN DWORD dwProperty,
  485. OUT WCHAR ** pwszIdString
  486. )
  487. {
  488. const DWORD INITIAL_STRING_SIZE = 100;
  489. CONFIGRET cr;
  490. DWORD dwBufferSize = INITIAL_STRING_SIZE;
  491. DWORD dwDataType = 0;
  492. ASSERT( ! IsBadWritePtr( pwszIdString, sizeof( WCHAR * ) ) );
  493. do
  494. {
  495. //
  496. // Allocate a buffer to store the returned string.
  497. //
  498. *pwszIdString = new WCHAR[ dwBufferSize + 1 ];
  499. if ( *pwszIdString == NULL )
  500. {
  501. return E_OUTOFMEMORY;
  502. }
  503. //
  504. // Try to get the string in the registry; we may not have enough
  505. // buffer space.
  506. //
  507. cr = CM_Get_DevNode_Registry_PropertyW(
  508. dwDevInst, // IN DEVINST dnDevInst,
  509. dwProperty, // IN ULONG ulProperty,
  510. & dwDataType, // OUT PULONG pulRegDataType, OPT
  511. (void *) *pwszIdString, // OUT PVOID Buffer, OPT
  512. & dwBufferSize, // IN OUT PULONG pulLength,
  513. 0 // IN ULONG ulFlags -- must be zero
  514. );
  515. if ( cr == CR_SUCCESS )
  516. {
  517. if ( ( dwDataType != REG_MULTI_SZ ) && ( dwDataType != REG_SZ ) )
  518. {
  519. //
  520. // Value available, but it is not a string ot multi-string. Ouch!
  521. //
  522. delete ( *pwszIdString );
  523. return E_UNEXPECTED;
  524. }
  525. else
  526. {
  527. return S_OK;
  528. }
  529. }
  530. else if ( cr != CR_BUFFER_SMALL )
  531. {
  532. //
  533. // It's supposed to fail with this error code because we didn't pass in
  534. // a buffer. Failed to get registry value type and length.
  535. //
  536. delete ( *pwszIdString );
  537. return MapConfigRetToHResult( cr );
  538. }
  539. else // cr == CR_BUFFER_SMALL
  540. {
  541. delete ( *pwszIdString );
  542. //
  543. // the call filled in dwBufferSize with the needed value
  544. //
  545. }
  546. }
  547. while ( TRUE );
  548. }
  549. //////////////////////////////////////////////////////////////////////////////
  550. //
  551. // HardwareIdFromDevInst
  552. //
  553. // This function retrieves a trimmed hardware ID string for a particular
  554. // devinst dword. The value is obtained from the helper function
  555. // DevInstGetIdString(), and then trimmed using TrimHardwareIdString.
  556. //
  557. // Arguments:
  558. // dwDevInst - IN - the devinst dword for which we want info
  559. // pwszHardwareId - OUT - returns "new"ed Unicode string set
  560. //
  561. // Return values:
  562. // S_OK - success
  563. // E_FAIL - not a valid string in USB format
  564. // others - from DevInstGetIdString()
  565. //
  566. STATIC HRESULT
  567. HardwareIdFromDevInst(
  568. IN DWORD dwDevInst,
  569. OUT WCHAR ** pwszHardwareId
  570. )
  571. {
  572. ASSERT( ! IsBadWritePtr(pwszHardwareId, sizeof( WCHAR * ) ) );
  573. HRESULT hr;
  574. BOOL fValid;
  575. hr = DevInstGetIdString(
  576. dwDevInst,
  577. CM_DRP_HARDWAREID,
  578. pwszHardwareId
  579. );
  580. if ( FAILED(hr) )
  581. {
  582. return hr;
  583. }
  584. // wprintf(L"*** HardwareIdFromDevInst: devinst 0x%08x, RAW hardwareID %s\n",
  585. // dwDevInst, *pwszHardwareId);
  586. fValid = TrimHardwareIdString( *pwszHardwareId );
  587. if ( ! fValid )
  588. {
  589. delete ( * pwszHardwareId );
  590. return E_FAIL;
  591. }
  592. // wprintf(L"HardwareIdFromDevInst: devinst 0x%08x, hardwareID %s\n",
  593. // dwDevInst, *pwszHardwareId);
  594. return S_OK;
  595. }
  596. //////////////////////////////////////////////////////////////////////////////
  597. //
  598. // MatchHardwareIdInArray
  599. //
  600. // This function takes the devinst and hardware ID for a HID device and
  601. // looks in an array of devinsts and hardware IDs for wave devices to find
  602. // the correct wave id to use with the HID device. The correct wave id is
  603. // the one whose hardware ID matches the HID device's hardware ID, and whose
  604. // hardware ID matches the hardware ID for the closest common ancestor of
  605. // itself and the HID device.
  606. //
  607. // Arguments:
  608. // dwHidDevInst - IN - the devinst dword for the HID device
  609. // wszHidHardwareId - IN - the trimmed hardware ID string for the
  610. // HID device
  611. // dwNumDevices - IN - the size of the arrays -- the number of
  612. // wave ids on the system
  613. // pwszHardwareIds - IN - array of trimmed hardware id strings for
  614. // the wave devices, indexed by wave ids.
  615. // Some entries may be NULL to mark them as
  616. // invalid.
  617. // pdwDevInsts - IN - array of devinsts for wave devices,
  618. // indexed by wave ids. Some entries may be
  619. // (DWORD) -1 to mark them as invalid.
  620. // pdwMatchedWaveId - OUT - the wave id that matches the hid device
  621. //
  622. // Return values:
  623. // S_OK - the devinst was matched
  624. // E_FAIL - the devinst was not matched
  625. //
  626. STATIC HRESULT
  627. MatchHardwareIdInArray(
  628. IN DWORD dwHidDevInst,
  629. IN WCHAR * wszHidHardwareId,
  630. IN DWORD dwNumDevices,
  631. IN WCHAR ** pwszHardwareIds,
  632. IN DWORD * pdwDevInsts,
  633. OUT DWORD * pdwMatchedWaveId
  634. )
  635. {
  636. ASSERT( ! IsBadStringPtrW( wszHidHardwareId, (DWORD) -1 ) );
  637. ASSERT( ! IsBadReadPtr( pwszHardwareIds,
  638. sizeof( WCHAR * ) * dwNumDevices ) );
  639. ASSERT( ! IsBadReadPtr( pdwDevInsts,
  640. sizeof( DWORD ) * dwNumDevices ) );
  641. ASSERT( ! IsBadWritePtr( pdwMatchedWaveId, sizeof(DWORD) ) );
  642. //
  643. // For each available wave id...
  644. //
  645. DWORD dwCurrWaveId;
  646. for ( dwCurrWaveId = 0; dwCurrWaveId < dwNumDevices; dwCurrWaveId++ )
  647. {
  648. //
  649. // If this particular wave device has the same stripped hardware
  650. // ID string as what we are searching for, then we have a match.
  651. // But non-USB devices have non-parsable hardware ID strings, so
  652. // they are stored in the array as NULLs.
  653. //
  654. if ( pwszHardwareIds[ dwCurrWaveId ] != NULL )
  655. {
  656. ASSERT( ! IsBadStringPtrW( pwszHardwareIds[ dwCurrWaveId ], (DWORD) -1 ) );
  657. if ( ! lstrcmpW( pwszHardwareIds[ dwCurrWaveId ], wszHidHardwareId ) )
  658. {
  659. //
  660. // We have a match, but we must still verify if we're on the same
  661. // device, not some other device that has the same hardwareID. This
  662. // is to differentiate between multiple identical phones on the same
  663. // system.
  664. //
  665. // Note: we could make the code more complex, but avoid some work in
  666. // most cases, by only doing this if there is more than one match based
  667. // on hardwareIDs alone.
  668. //
  669. DWORD dwCommonAncestor;
  670. WCHAR * wszAncestorHardwareId;
  671. HRESULT hr;
  672. hr = FindClosestCommonAncestor(
  673. dwHidDevInst,
  674. pdwDevInsts[ dwCurrWaveId ],
  675. & dwCommonAncestor
  676. );
  677. if ( SUCCEEDED(hr) )
  678. {
  679. //
  680. // Get the hardware ID for the closest common ancestor.
  681. //
  682. hr = HardwareIdFromDevInst(
  683. dwCommonAncestor,
  684. & wszAncestorHardwareId
  685. );
  686. if ( SUCCEEDED(hr) )
  687. {
  688. //
  689. // Check if they are the same. The closest common ancestor
  690. // will be some sort of hub if the audio device is from
  691. // some other identical phone other than the one whose HID
  692. // device we are looking at.
  693. //
  694. BOOL fSame;
  695. fSame = ! lstrcmpW( wszAncestorHardwareId,
  696. wszHidHardwareId );
  697. delete wszAncestorHardwareId;
  698. if ( fSame )
  699. {
  700. *pdwMatchedWaveId = dwCurrWaveId;
  701. return S_OK;
  702. }
  703. }
  704. }
  705. }
  706. }
  707. }
  708. //
  709. // No match.
  710. //
  711. return E_FAIL;
  712. }
  713. //////////////////////////////////////////////////////////////////////////////
  714. //
  715. // GetInstanceFromDeviceName
  716. //
  717. // This function retrieves a device instance identifier based on a device
  718. // name string. This works for any device.
  719. //
  720. // Arguments:
  721. // wszName - IN - the device name string
  722. // pdwInstance - OUT - returns instance identifier
  723. //
  724. // Return values:
  725. // S_OK
  726. // various win32 errors from SetupDi fucntions
  727. //
  728. STATIC HRESULT
  729. GetInstanceFromDeviceName(
  730. IN WCHAR * wszName,
  731. OUT DWORD * pdwInstance,
  732. IN HDEVINFO hDevInfo
  733. )
  734. {
  735. ASSERT( ! IsBadStringPtrW( wszName, (DWORD) -1 ) );
  736. ASSERT( ! IsBadWritePtr( pdwInstance, sizeof(DWORD) ) );
  737. //
  738. // Get the interface data for this specific device
  739. // (based on wszName).
  740. //
  741. BOOL fSuccess;
  742. DWORD dwError;
  743. SP_DEVICE_INTERFACE_DATA interfaceData;
  744. interfaceData.cbSize = sizeof( SP_DEVICE_INTERFACE_DATA ); // required
  745. fSuccess = SetupDiOpenDeviceInterfaceW(
  746. hDevInfo, // device info set handle
  747. wszName, // name of the device
  748. 0, // flags, reserved
  749. & interfaceData // OUT: interface data
  750. );
  751. if ( ! fSuccess )
  752. {
  753. LOG((PHONESP_TRACE, "GetInstanceFromDeviceName - SetupDiOpenDeviceInterfaceW failed: %08x", GetLastError()));
  754. //
  755. // Need to clean up, but save the error code first, because
  756. // the cleanup function calls SetLastError().
  757. //
  758. dwError = GetLastError();
  759. return HRESULT_FROM_WIN32( dwError );
  760. }
  761. //
  762. // Get the interface detail data from this interface data. This provides
  763. // more detailed information,including the device instance DWORD that
  764. // we seek.
  765. //
  766. SP_DEVINFO_DATA devinfoData;
  767. devinfoData.cbSize = sizeof( SP_DEVINFO_DATA ); // required
  768. fSuccess = SetupDiGetDeviceInterfaceDetail(
  769. hDevInfo, // device info set handle
  770. & interfaceData, // device interface data structure
  771. NULL, // OPT ptr to dev name struct
  772. 0, // OPT avail size of dev name st
  773. NULL, // OPT actual size of devname st
  774. & devinfoData
  775. );
  776. if ( ! fSuccess )
  777. {
  778. //
  779. // It is normal for the above function to fail with
  780. // ERROR_INSUFFICIENT_BUFFER because we passed in NULL for the
  781. // device interface detail data (device name) structure.
  782. //
  783. dwError = GetLastError();
  784. if ( dwError != ERROR_INSUFFICIENT_BUFFER )
  785. {
  786. LOG((PHONESP_TRACE, "GetInstanceFromDeviceName - SetupDiGetDeviceInterfaceDetail failed: %08x", GetLastError()));
  787. //
  788. // Can't clean this up earlier, because it does SetLastError().
  789. //
  790. return HRESULT_FROM_WIN32( dwError );
  791. }
  792. }
  793. *pdwInstance = devinfoData.DevInst;
  794. //
  795. // Can't clean this up earlier, because it does SetLastError().
  796. //
  797. return S_OK;
  798. }
  799. //////////////////////////////////////////////////////////////////////////////
  800. //
  801. //
  802. //
  803. // This function constructs an array of devinst DWORDs and an array of
  804. // hardware ID strigs, both indexed by wave id, for either wave in or wave
  805. // out devices. The devinsts are retrieved by (1) using undocumented calls
  806. // to winmm to retrieve device name strings for each wave device, and (2)
  807. // using SetupDi calls to retrieve a DevInst DWORD for each device name
  808. // string (helper function GetInstanceFromDeviceName).
  809. //
  810. // The values are saved in an array because this process takes the bulk of the
  811. // time in the HID --> audio mapping process, and therefore finding the mapping
  812. // for several HID devices can be done in not much more time than for one HID
  813. // device, just by reusing the array.
  814. //
  815. // Arguments:
  816. // fRender - IN - if TRUE, look for wave out devices
  817. // pdwNumDevices - OUT - returns number of wave devices found
  818. // ppwszHardwareIds - OUT - returns "new"ed array of trimmed hardware id
  819. // strings. The array is indexed by wave id. If
  820. // a hardware id string cannot be determined for
  821. // a particular wave id, then the string pointer
  822. // in that position is set to NULL. Each string
  823. // is "new"ed separately.
  824. // ppdwDevInsts - OUT - returns "new"ed array of devinst DWORDs. The
  825. // array is indexed by wave id. If a devinst
  826. // cannot be determined for a particular wave id,
  827. // then the DWORD in that position is set to
  828. // (DWORD) -1.
  829. //
  830. // Return values:
  831. // S_OK - success
  832. // E_OUTOFMEMORY - not enough memory to allocate a device name string or
  833. // the return array
  834. //
  835. STATIC HRESULT
  836. ConstructWaveHardwareIdCache(
  837. IN BOOL fRender,
  838. OUT DWORD * pdwNumDevices,
  839. OUT WCHAR *** ppwszHardwareIds,
  840. OUT DWORD ** ppdwDevInsts
  841. )
  842. {
  843. ASSERT( ( fRender == TRUE ) || ( fRender == FALSE ) );
  844. ASSERT( ! IsBadWritePtr( pdwNumDevices, sizeof( DWORD ) ) );
  845. ASSERT( ! IsBadWritePtr( ppwszHardwareIds, sizeof( WCHAR ** ) ) );
  846. ASSERT( ! IsBadWritePtr( ppdwDevInsts, sizeof( DWORD * ) ) );
  847. //
  848. // Get a device info list
  849. //
  850. HDEVINFO hDevInfo;
  851. /*
  852. hDevInfo = SetupDiGetClassDevs(
  853. &GUID_DEVCLASS_MEDIA, // class GUID (which device classes?)
  854. NULL, // optional enumerator to filter
  855. NULL, // HWND (we have none)
  856. ( DIGCF_PRESENT | // only devices that are present
  857. DIGCF_PROFILE ) // only devices in this hw profile
  858. );
  859. */
  860. hDevInfo = SetupDiCreateDeviceInfoList(&GUID_DEVCLASS_MEDIA, NULL);
  861. if ( hDevInfo == NULL )
  862. {
  863. LOG((PHONESP_TRACE, "ConstructWaveHardwareIdCache - SetupDiCreateDeviceInfoList failed: %08x", GetLastError()));
  864. return HRESULT_FROM_WIN32(GetLastError());
  865. }
  866. //
  867. // Find the number of available wave devices.
  868. //
  869. DWORD dwNumDevices;
  870. DWORD dwCurrDevice;
  871. if ( fRender )
  872. {
  873. dwNumDevices = waveOutGetNumDevs();
  874. }
  875. else
  876. {
  877. dwNumDevices = waveInGetNumDevs();
  878. }
  879. //
  880. // Allocate space for the return arrays.
  881. //
  882. *pdwNumDevices = dwNumDevices;
  883. *ppwszHardwareIds = new LPWSTR [ dwNumDevices ];
  884. if ( (*ppwszHardwareIds) == NULL )
  885. {
  886. return E_OUTOFMEMORY;
  887. }
  888. *ppdwDevInsts = new DWORD [ dwNumDevices ];
  889. if ( (*ppdwDevInsts) == NULL )
  890. {
  891. delete *ppwszHardwareIds;
  892. *ppwszHardwareIds = NULL;
  893. return E_OUTOFMEMORY;
  894. }
  895. //
  896. // Loop over the available wave devices.
  897. //
  898. for ( dwCurrDevice = 0; dwCurrDevice < dwNumDevices; dwCurrDevice++ )
  899. {
  900. //
  901. // For failure cases, we will return NULL string and -1 devinst
  902. // for that wave id. Callers should compare against the NULL, not
  903. // the -1.
  904. //
  905. (*ppwszHardwareIds) [ dwCurrDevice ] = NULL;
  906. (*ppdwDevInsts) [ dwCurrDevice ] = -1;
  907. //
  908. // Get the size of the device path string.
  909. //
  910. MMRESULT mmresult;
  911. ULONG ulSize;
  912. if ( fRender )
  913. {
  914. mmresult = waveOutMessage( (HWAVEOUT) IntToPtr(dwCurrDevice),
  915. DRV_QUERYDEVICEINTERFACESIZE,
  916. (DWORD_PTR) & ulSize,
  917. 0
  918. );
  919. }
  920. else
  921. {
  922. mmresult = waveInMessage( (HWAVEIN) IntToPtr(dwCurrDevice),
  923. DRV_QUERYDEVICEINTERFACESIZE,
  924. (DWORD_PTR) & ulSize,
  925. 0
  926. );
  927. }
  928. if ( mmresult != MMSYSERR_NOERROR )
  929. {
  930. LOG((PHONESP_TRACE, "ConstructWaveHardwareIdCache - Could not get device string size for device %d; "
  931. "error = %d", dwCurrDevice, mmresult));
  932. }
  933. else if ( ulSize == 0 )
  934. {
  935. LOG((PHONESP_TRACE, "ConstructWaveHardwareIdCache - Got zero device string size for device %d",
  936. dwCurrDevice));
  937. }
  938. else
  939. {
  940. //
  941. // Allocate space for the device path string.
  942. //
  943. WCHAR * wszDeviceName;
  944. wszDeviceName = new WCHAR[ (ulSize / 2) + 1 ];
  945. if ( wszDeviceName == NULL )
  946. {
  947. LOG((PHONESP_TRACE, "ConstructWaveHardwareIdCache - Out of memory in device string alloc for device %d;"
  948. " requested size is %d\n", dwCurrDevice, ulSize));
  949. delete *ppwszHardwareIds;
  950. *ppwszHardwareIds = NULL;
  951. delete *ppdwDevInsts;
  952. *ppdwDevInsts = NULL;
  953. return E_OUTOFMEMORY;
  954. }
  955. //
  956. // Get the device path string from winmm.
  957. //
  958. if ( fRender )
  959. {
  960. mmresult = waveOutMessage( (HWAVEOUT) IntToPtr(dwCurrDevice),
  961. DRV_QUERYDEVICEINTERFACE,
  962. (DWORD_PTR) wszDeviceName,
  963. (DWORD_PTR) ulSize
  964. );
  965. }
  966. else
  967. {
  968. mmresult = waveInMessage( (HWAVEIN) IntToPtr(dwCurrDevice),
  969. DRV_QUERYDEVICEINTERFACE,
  970. (DWORD_PTR) wszDeviceName,
  971. (DWORD_PTR) ulSize
  972. );
  973. }
  974. if ( mmresult == MMSYSERR_NOERROR )
  975. {
  976. //
  977. // Got the string. Now retrieve a devinst dword based on the
  978. // string.
  979. //
  980. // wprintf(L"\tDevice name string for device %d is:\n"
  981. // L"\t\t%ws\n",
  982. // dwCurrDevice, wszDeviceName);
  983. HRESULT hr;
  984. DWORD dwInstance;
  985. hr = GetInstanceFromDeviceName(
  986. wszDeviceName,
  987. & dwInstance,
  988. hDevInfo
  989. );
  990. delete wszDeviceName;
  991. if ( FAILED(hr) )
  992. {
  993. LOG((PHONESP_TRACE, "ConstructWaveHardwareIdCache - Can't get instance DWORD for device %d; "
  994. "error 0x%08x\n",
  995. dwCurrDevice, hr));
  996. }
  997. else
  998. {
  999. //
  1000. // Based on the devinst dword, retrieve a trimmed
  1001. // hardware id string.
  1002. //
  1003. // printf("\tInstance DWORD for device %d is "
  1004. // "0x%08x\n",
  1005. // dwCurrDevice, dwInstance);
  1006. WCHAR * wszHardwareId;
  1007. hr = HardwareIdFromDevInst(
  1008. dwInstance,
  1009. & wszHardwareId
  1010. );
  1011. if ( SUCCEEDED(hr) )
  1012. {
  1013. (*ppwszHardwareIds) [ dwCurrDevice ] = wszHardwareId;
  1014. (*ppdwDevInsts) [ dwCurrDevice ] = dwInstance;
  1015. }
  1016. }
  1017. }
  1018. }
  1019. }
  1020. SetupDiDestroyDeviceInfoList( hDevInfo );
  1021. return S_OK;
  1022. }
  1023. //////////////////////////////////////////////////////////////////////////////
  1024. //
  1025. // FindWaveIdFromHardwareIdString
  1026. //
  1027. // This function finds the wave id for a device whose devinst and hardware id
  1028. // string are known.
  1029. //
  1030. // Constructing the mapping from waveid to devinst and hardwave id string
  1031. // takes some time, so the mapping is only constructed once for each
  1032. // direction (render/capture), via the helper function
  1033. // ConstructWaveHardwareIdCache().
  1034. //
  1035. // Thereafter, the helper function MatchHardwareIdInArray() is used to run
  1036. // the matching algorithm based on the already-computed arrays. See that
  1037. // function for a description of how the matching is done.
  1038. //
  1039. // Arguments:
  1040. // dwHidDevInst - IN - the devinst dword to match to a wave id
  1041. // wszHardwareId - IN - the hardware id string for the devinst
  1042. // fRender - IN - TRUE for wave out, FALSE for wave in
  1043. // pdwMatchedWaveId - OUT - the wave id associated with the devinst
  1044. //
  1045. // Return values:
  1046. // S_OK - success
  1047. // various errors from ConstructWaveHardwareIdCache() and
  1048. // MatchHardwareIdInArray() helper functions
  1049. //
  1050. STATIC HRESULT
  1051. FindWaveIdFromHardwareIdString(
  1052. IN DWORD dwHidDevInst,
  1053. IN WCHAR * wszHardwareId,
  1054. IN BOOL fRender,
  1055. OUT DWORD * pdwMatchedWaveId
  1056. )
  1057. {
  1058. ASSERT( ! IsBadStringPtrW( wszHardwareId, (DWORD) -1 ) );
  1059. ASSERT( ( fRender == TRUE ) || ( fRender == FALSE ) );
  1060. ASSERT( ! IsBadWritePtr(pdwMatchedWaveId, sizeof(DWORD) ) );
  1061. DWORD dwNumDevices = 0;
  1062. WCHAR ** pwszHardwareIds = NULL;
  1063. DWORD * pdwDevInsts = NULL;
  1064. HRESULT hr;
  1065. //
  1066. // Need to construct cache of render device hardware IDs.
  1067. //
  1068. hr = ConstructWaveHardwareIdCache(
  1069. fRender,
  1070. & dwNumDevices,
  1071. & pwszHardwareIds,
  1072. & pdwDevInsts
  1073. );
  1074. if ( FAILED(hr) )
  1075. {
  1076. return hr;
  1077. }
  1078. //
  1079. // The cache is ready; use it to perform the rest of the matching
  1080. // algorithm.
  1081. //
  1082. hr = MatchHardwareIdInArray(
  1083. dwHidDevInst,
  1084. wszHardwareId,
  1085. dwNumDevices,
  1086. pwszHardwareIds,
  1087. pdwDevInsts,
  1088. pdwMatchedWaveId
  1089. );
  1090. delete pwszHardwareIds;
  1091. delete pdwDevInsts;
  1092. return hr;
  1093. }
  1094. //////////////////////////////////////////////////////////////////////////////
  1095. //
  1096. // OutputDeviceInfo
  1097. //
  1098. // This function is for diagnostic purposes only.
  1099. //
  1100. // Given a devinst DWORD, this function prints the DeviceDesc string as well
  1101. // as the entire (untrimmed) hardware ID string set for the device. Example:
  1102. //
  1103. //
  1104. //
  1105. // Arguments:
  1106. // dwDesiredDevInst - IN - the devinst dword for which we want info
  1107. //
  1108. // Return values:
  1109. // none
  1110. //
  1111. STATIC void
  1112. OutputDeviceInfo(
  1113. DWORD dwDesiredDevInst
  1114. )
  1115. {
  1116. //
  1117. // Get and print the device description string.
  1118. //
  1119. HRESULT hr;
  1120. WCHAR * wszDeviceDesc;
  1121. WCHAR * wszHardwareId;
  1122. hr = DevInstGetIdString(
  1123. dwDesiredDevInst,
  1124. CM_DRP_DEVICEDESC,
  1125. & wszDeviceDesc
  1126. );
  1127. if ( FAILED(hr) )
  1128. {
  1129. LOG((PHONESP_TRACE, "OutputDeviceInfo - [can't get device description string - 0x%08x]", hr));
  1130. }
  1131. else
  1132. {
  1133. LOG((PHONESP_TRACE, "OutputDeviceInfo - [DeviceDesc: %ws]", wszDeviceDesc));
  1134. delete wszDeviceDesc;
  1135. }
  1136. //
  1137. // Get and print hardware ID string set.
  1138. //
  1139. hr = DevInstGetIdString(
  1140. dwDesiredDevInst,
  1141. CM_DRP_HARDWAREID,
  1142. & wszHardwareId
  1143. );
  1144. if ( FAILED(hr) )
  1145. {
  1146. LOG((PHONESP_TRACE, "OutputDeviceInfo - [can't get hardware id - 0x%08x]", hr));
  1147. }
  1148. else
  1149. {
  1150. //
  1151. // Print out all the values in the mutli-string.
  1152. //
  1153. WCHAR * wszCurr = wszHardwareId;
  1154. while ( wszCurr[0] != L'\0' )
  1155. {
  1156. LOG((PHONESP_TRACE, "OutputDeviceInfo - [HardwareId: %ws]", wszCurr));
  1157. wszCurr += lstrlenW(wszCurr) + 1;
  1158. }
  1159. delete wszHardwareId;
  1160. }
  1161. }
  1162. //////////////////////////////////////////////////////////////////////////////
  1163. //////////////////////////////////////////////////////////////////////////////
  1164. //////////////////////////////////////////////////////////////////////////////
  1165. //
  1166. //
  1167. // Externally-callable functions
  1168. //
  1169. //
  1170. //////////////////////////////////////////////////////////////////////////////
  1171. //////////////////////////////////////////////////////////////////////////////
  1172. //////////////////////////////////////////////////////////////////////////////
  1173. //////////////////////////////////////////////////////////////////////////////
  1174. //
  1175. // ExamineWaveDevices
  1176. //
  1177. // This function is for debugging purposes only. It enumerates audio devices
  1178. // using the Wave API and prints the device path string as well as the
  1179. // device instance DWORD for each render or capture device.
  1180. //
  1181. // Arguments:
  1182. // fRender - IN - true means examine wave out devices; false = wave in
  1183. //
  1184. // Return values:
  1185. // E_OUTOFMEMORY
  1186. // S_OK
  1187. //
  1188. HRESULT
  1189. ExamineWaveDevices(
  1190. IN BOOL fRender
  1191. )
  1192. {
  1193. ASSERT( ( fRender == TRUE ) || ( fRender == FALSE ) );
  1194. DWORD dwNumDevices;
  1195. DWORD dwCurrDevice;
  1196. //
  1197. // Get a device info list
  1198. //
  1199. HDEVINFO hDevInfo;
  1200. /*
  1201. hDevInfo = SetupDiGetClassDevs(
  1202. &GUID_DEVCLASS_MEDIA, // class GUID (which device classes?)
  1203. NULL, // optional enumerator to filter
  1204. NULL, // HWND (we have none)
  1205. ( DIGCF_PRESENT | // only devices that are present
  1206. DIGCF_PROFILE ) // only devices in this hw profile
  1207. );
  1208. */
  1209. hDevInfo = SetupDiCreateDeviceInfoList(&GUID_DEVCLASS_MEDIA, NULL);
  1210. if ( hDevInfo == NULL )
  1211. {
  1212. LOG((PHONESP_TRACE, "ExamineWaveDevices - SetupDiCreateDeviceInfoList failed: %08x", GetLastError()));
  1213. return HRESULT_FROM_WIN32(GetLastError());
  1214. }
  1215. //
  1216. // Loop over the available wave devices.
  1217. //
  1218. if ( fRender )
  1219. {
  1220. dwNumDevices = waveOutGetNumDevs();
  1221. }
  1222. else
  1223. {
  1224. dwNumDevices = waveInGetNumDevs();
  1225. }
  1226. LOG((PHONESP_TRACE, "ExamineWaveDevices - Found %d audio %s devices.",
  1227. dwNumDevices,
  1228. fRender ? "render" : "capture"));
  1229. for ( dwCurrDevice = 0; dwCurrDevice < dwNumDevices; dwCurrDevice++ )
  1230. {
  1231. MMRESULT mmresult;
  1232. ULONG ulSize;
  1233. //
  1234. // Get the size of the device path string.
  1235. //
  1236. if ( fRender )
  1237. {
  1238. mmresult = waveOutMessage( (HWAVEOUT) IntToPtr(dwCurrDevice),
  1239. DRV_QUERYDEVICEINTERFACESIZE,
  1240. (DWORD_PTR) & ulSize,
  1241. 0
  1242. );
  1243. }
  1244. else
  1245. {
  1246. mmresult = waveInMessage( (HWAVEIN) IntToPtr(dwCurrDevice),
  1247. DRV_QUERYDEVICEINTERFACESIZE,
  1248. (DWORD_PTR) & ulSize,
  1249. 0
  1250. );
  1251. }
  1252. if ( mmresult != MMSYSERR_NOERROR )
  1253. {
  1254. LOG((PHONESP_TRACE, "ExamineWaveDevices - Could not get device string size for device %d; "
  1255. "error = %d\n", dwCurrDevice, mmresult));
  1256. }
  1257. else if ( ulSize == 0 )
  1258. {
  1259. LOG((PHONESP_TRACE, "ExamineWaveDevices - Got zero device string size for device %d\n",
  1260. dwCurrDevice));
  1261. }
  1262. else
  1263. {
  1264. //
  1265. // Allocate space for the device path string.
  1266. //
  1267. WCHAR * wszDeviceName;
  1268. wszDeviceName = new WCHAR[ (ulSize / 2) + 1 ];
  1269. if ( wszDeviceName == NULL )
  1270. {
  1271. LOG((PHONESP_TRACE, "ExamineWaveDevices - Out of memory in device string alloc for device %d;"
  1272. " requested size is %d\n", dwCurrDevice, ulSize));
  1273. return E_OUTOFMEMORY;
  1274. }
  1275. //
  1276. // Get the device path string from winmm.
  1277. //
  1278. if ( fRender )
  1279. {
  1280. mmresult = waveOutMessage( (HWAVEOUT) IntToPtr(dwCurrDevice),
  1281. DRV_QUERYDEVICEINTERFACE,
  1282. (DWORD_PTR) wszDeviceName,
  1283. ulSize
  1284. );
  1285. }
  1286. else
  1287. {
  1288. mmresult = waveInMessage( (HWAVEIN) IntToPtr(dwCurrDevice),
  1289. DRV_QUERYDEVICEINTERFACE,
  1290. (DWORD_PTR) wszDeviceName,
  1291. ulSize
  1292. );
  1293. }
  1294. if ( mmresult == MMSYSERR_NOERROR )
  1295. {
  1296. //
  1297. // Got the string; print it and convert it to a
  1298. // devinst DWORD.
  1299. //
  1300. LOG((PHONESP_TRACE, "ExamineWaveDevices - Device name string for device %d is: %ws",
  1301. dwCurrDevice, wszDeviceName));
  1302. HRESULT hr;
  1303. DWORD dwInstance;
  1304. hr = GetInstanceFromDeviceName(
  1305. wszDeviceName,
  1306. & dwInstance,
  1307. hDevInfo
  1308. );
  1309. if ( FAILED(hr) )
  1310. {
  1311. LOG((PHONESP_TRACE, "ExamineWaveDevices - Can't get instance DWORD for device %d; "
  1312. "error 0x%08x",
  1313. dwCurrDevice, hr));
  1314. }
  1315. else
  1316. {
  1317. LOG((PHONESP_TRACE, "ExamineWaveDevices - Instance DWORD for device %d is "
  1318. "0x%08x",
  1319. dwCurrDevice, dwInstance));
  1320. //
  1321. // Print various other info about this device.
  1322. //
  1323. OutputDeviceInfo( dwInstance );
  1324. WCHAR * wszHardwareId;
  1325. hr = HardwareIdFromDevInst(
  1326. dwInstance,
  1327. & wszHardwareId
  1328. );
  1329. if ( FAILED(hr) )
  1330. {
  1331. LOG((PHONESP_TRACE, "ExamineWaveDevices - Can't get hardware id string for device %d; "
  1332. "error 0x%08x",
  1333. dwCurrDevice, hr));
  1334. }
  1335. else
  1336. {
  1337. LOG((PHONESP_TRACE, "ExamineWaveDevices - Hardware ID for device %d is %ws\n",
  1338. dwCurrDevice, wszHardwareId));
  1339. delete wszHardwareId;
  1340. }
  1341. }
  1342. delete wszDeviceName;
  1343. }
  1344. }
  1345. }
  1346. SetupDiDestroyDeviceInfoList( hDevInfo );
  1347. return S_OK;
  1348. }
  1349. //////////////////////////////////////////////////////////////////////////////
  1350. //
  1351. // DiscoverAssociatedWaveId
  1352. //
  1353. // This function searches for a wave device to match the HID device in the
  1354. // PNP tree location specified in the passed in SP_DEVICE_INTERFACE_DATA
  1355. // structure, obtained from the SetupKi API. It returns the wave id for
  1356. // the matched device.
  1357. //
  1358. // It uses the helper function FindWaveIdFromHardwareIdString() to search for
  1359. // the wave device based on a devinst DWORD and a hardware ID string. First,
  1360. // it must obtain the devinst for the device; it does this by calling a SetupDi
  1361. // function and looking up the devinst in a resulting structure. The hardware
  1362. // ID string is then retrieved from the registry and trimmed, using the helper
  1363. // function HardwareIdFromDevinst().
  1364. //
  1365. // See FindWaveIdFromHardwareIdString() for further comments on the search
  1366. // algorithm.
  1367. //
  1368. // Arguments:
  1369. // dwDevInst - IN - Device Instance of the HID device
  1370. // fRender - IN - TRUE for wave out, FALSE for wave in
  1371. // pdwWaveId - OUT - the wave id associated with this HID device
  1372. //
  1373. // Return values:
  1374. // S_OK - succeeded and matched wave id
  1375. // other from helper functions FindWaveIdFromHardwareIdString() or
  1376. // or HardwareIdFromDevinst()
  1377. //
  1378. HRESULT
  1379. DiscoverAssociatedWaveId(
  1380. IN DWORD dwDevInst,
  1381. IN BOOL fRender,
  1382. OUT DWORD * pdwWaveId
  1383. )
  1384. {
  1385. ASSERT( ! IsBadWritePtr(pdwWaveId, sizeof(DWORD) ) );
  1386. ASSERT( ( fRender == TRUE ) || ( fRender == FALSE ) );
  1387. //
  1388. // We've got the device instance DWORD for the HID device.
  1389. // Use it to get the trimmed hardware ID string, which tells
  1390. // us the vendor, product, and revision numbers.
  1391. //
  1392. HRESULT hr;
  1393. WCHAR * wszHardwareId;
  1394. hr = HardwareIdFromDevInst(
  1395. dwDevInst,
  1396. & wszHardwareId
  1397. );
  1398. if ( FAILED(hr) )
  1399. {
  1400. return hr;
  1401. }
  1402. //
  1403. // Finally, use this information to choose a wave id.
  1404. //
  1405. hr = FindWaveIdFromHardwareIdString(
  1406. dwDevInst,
  1407. wszHardwareId,
  1408. fRender,
  1409. pdwWaveId
  1410. );
  1411. delete wszHardwareId;
  1412. if ( FAILED(hr) )
  1413. {
  1414. return hr;
  1415. }
  1416. return S_OK;
  1417. }
  1418. //
  1419. // eof
  1420. //