控制台应用程序与 Win32 应用程序 - DirectSound 捕获设备枚举给出不同的结果

Console app vs Win32 app - DirectSound capture-device enumeration gives different results

我正在寻找一种可靠的方法来将 DirectShow 捕获设备 GUID 映射到其对应的 waveID 值。

我通过 Chris_P 找到了以下 project

该解决方案效果很好,它依赖于一个相当晦涩的 IKsPropertySet 接口来检索映射。

不幸的是,如果我尝试从 C++/CLI 库中使用相同的技术,代码将失败并显示 E_NOTIMPL(此行为已在 this 问题中进行了描述,- 请参阅 弗拉基米尔·赫梅利奥夫)

所以,我想我可以编写一个简单的基于控制台的辅助应用程序来检索映射并打印它们。然后我的图书馆可以启动这个辅助应用程序并解析重定向的输出以获得映射。

控制台程序运行正常,但是,传递给枚举回调的 GUID 与 Chris_P 的解决方案传递的完全不同。

事实上它们都具有相同的基本结构:

lpGuid = 0x02ad0808 {BDF35A00-B9AC-11D0-A619-00AA00A7C000}

唯一的变化出现在 GUID 的最后几位,巧合的是,它们与映射的 waveId 值匹配。

另一个奇怪的事情是捕获设备描述被截断为 31 个字符,就好像枚举是使用 WaveIn APIs!

似乎有些 DirectSound 门面现在正在包装 WaveIn API。

任何关于可能发生的事情的指示?我可以禁用此行为并枚举 WIN32 应用程序正在枚举的相同 GUID 吗?

这是控制台应用程序的代码:

#include "stdafx.h"
#include <mmreg.h>
#include <initguid.h>
#include <Dsound.h>
#include <dsconf.h>


static BOOL CALLBACK DSEnumCallback(
   LPGUID  lpGuid,
   LPCTSTR  lpcstrDescription,
   LPCTSTR  lpcstrModule,
   LPVOID  lpContext
);

static BOOL GetInfoFromDSoundGUID(GUID i_sGUID, DWORD &dwWaveID);
static HRESULT DirectSoundPrivateCreate(OUT LPKSPROPERTYSET * ppKsPropertySet);

typedef WINUSERAPI HRESULT(WINAPI *LPFNDLLGETCLASSOBJECT) (const CLSID &, const IID &, void **);



BOOL GetInfoFromDSoundGUID(GUID i_sGUID, DWORD &dwWaveID) {
   LPKSPROPERTYSET         pKsPropertySet = NULL;
   HRESULT                 hr;
   BOOL                 retval = FALSE;

   PDSPROPERTY_DIRECTSOUNDDEVICE_DESCRIPTION_DATA psDirectSoundDeviceDescription = NULL;
   DSPROPERTY_DIRECTSOUNDDEVICE_DESCRIPTION_DATA sDirectSoundDeviceDescription;

   memset(&sDirectSoundDeviceDescription, 0, sizeof(sDirectSoundDeviceDescription));
   hr = DirectSoundPrivateCreate(&pKsPropertySet);
   if( SUCCEEDED(hr) ) {
      ULONG ulBytesReturned = 0;
      sDirectSoundDeviceDescription.DeviceId = i_sGUID;

      // On the first call the final size is unknown so pass the size of the struct in order to receive
      // "Type" and "DataFlow" values, ulBytesReturned will be populated with bytes required for struct+strings.
      hr = pKsPropertySet->Get(DSPROPSETID_DirectSoundDevice,
                               DSPROPERTY_DIRECTSOUNDDEVICE_DESCRIPTION,
                               NULL,
                               0,
                               &sDirectSoundDeviceDescription,
                               sizeof(sDirectSoundDeviceDescription),
                               &ulBytesReturned
      );

      if( ulBytesReturned ) {
         // On the first call it notifies us of the required amount of memory in order to receive the strings.
         // Allocate the required memory, the strings will be pointed to the memory space directly after the struct.
         psDirectSoundDeviceDescription = (PDSPROPERTY_DIRECTSOUNDDEVICE_DESCRIPTION_DATA)new BYTE[ulBytesReturned];
         *psDirectSoundDeviceDescription = sDirectSoundDeviceDescription;

         hr = pKsPropertySet->Get(DSPROPSETID_DirectSoundDevice,
                                  DSPROPERTY_DIRECTSOUNDDEVICE_DESCRIPTION,
                                  NULL,
                                  0,
                                  psDirectSoundDeviceDescription,
                                  ulBytesReturned,
                                  &ulBytesReturned
         );

         dwWaveID = psDirectSoundDeviceDescription->WaveDeviceId;
         delete[] psDirectSoundDeviceDescription;
         retval = TRUE;
      }

      pKsPropertySet->Release();
   }

   return retval;
}



HRESULT DirectSoundPrivateCreate(OUT LPKSPROPERTYSET * ppKsPropertySet) {
   HMODULE                 hLibDsound = NULL;
   LPFNDLLGETCLASSOBJECT   pfnDllGetClassObject = NULL;
   LPCLASSFACTORY          pClassFactory = NULL;
   LPKSPROPERTYSET         pKsPropertySet = NULL;
   HRESULT                 hr = DS_OK;

   // Load dsound.dll 
   hLibDsound = LoadLibrary(TEXT("dsound.dll"));

   if( !hLibDsound ) {
      hr = DSERR_GENERIC;
   }

   // Find DllGetClassObject 
   if( SUCCEEDED(hr) ) {
      pfnDllGetClassObject =
         (LPFNDLLGETCLASSOBJECT)GetProcAddress(hLibDsound, "DllGetClassObject");


      if( !pfnDllGetClassObject ) {
         hr = DSERR_GENERIC;
      }
   }

   // Create a class factory object     
   if( SUCCEEDED(hr) ) {
      hr = pfnDllGetClassObject(CLSID_DirectSoundPrivate, IID_IClassFactory, (LPVOID *)&pClassFactory);
   }

   // Create the DirectSoundPrivate object and query for an IKsPropertySet 
   // interface 
   if( SUCCEEDED(hr) ) {
      hr = pClassFactory->CreateInstance(NULL, IID_IKsPropertySet, (LPVOID *)&pKsPropertySet);
   }

   // Release the class factory 
   if( pClassFactory ) {
      pClassFactory->Release();
   }

   // Handle final success or failure 
   if( SUCCEEDED(hr) ) {
      *ppKsPropertySet = pKsPropertySet;
   } else if( pKsPropertySet ) {
      pKsPropertySet->Release();
   }

   FreeLibrary(hLibDsound);

   return hr;
}




BOOL CALLBACK DSEnumCallback(
   LPGUID  lpGuid,
   LPCTSTR  lpcstrDescription,
   LPCTSTR  lpcstrModule,
   LPVOID  lpContext
) {



   LPWSTR psz = NULL;
   StringFromCLSID(*lpGuid, &psz);
   DWORD WaveID = 0xFFFFFFFF;

   if( lpGuid ) {
      GUID i_guid = *lpGuid;
      GetInfoFromDSoundGUID(i_guid, WaveID);
   }

   if( WaveID != 0xFFFFFFFF ) 
      wprintf(_T("%d %s\r\n"), WaveID, psz);

   if( psz ) {
      CoTaskMemFree(psz);
   }

   return TRUE;
}


int main()
{
    DirectSoundCaptureEnumerate(DSEnumCallback, NULL);
    Sleep(10000);
    return 0;
}

原来我没有初始化 COM

我在 main() 过程的开头添加了以下代码片段,程序检索到了预期的 GUID:

   HRESULT hr = NULL;
   hr = CoInitialize(NULL);
   if( FAILED(hr) ) {
      printf("Failed to initialize COM");
      return -1;
   }

所以我猜如果 COM 没有初始化,DirectSound 引擎会回退到 WaveIn API(围绕它创建一个 DirectShow 外观)。