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

时间:2018-06-07 23:01:32

标签: visual-studio-2017 directshow directsound

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

我通过 Chris_P 找到了以下project

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

不幸的是,如果我从C ++ / CLI库中尝试相同的技术,代码将失败并显示E_NOTIMPLthis问题已对此行为进行了描述, - 请参阅 Vladimir的回答Hmelyoff

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

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

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

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

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

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

几乎看起来某些DirectSound外观正在包装WaveIn API。

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

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

#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;
}

1 个答案:

答案 0 :(得分: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外观)。