2013-03-14 79 views
0

,我有以下功能我打電話打印出枚舉音頻端點的端點和友好名稱:的Windows核心音頻錯誤

void EPPrint(void) 
{  
    HRESULT hr; 
    IMMDeviceEnumerator *pEnumerator = NULL; 
    IMMDeviceCollection *pCollection = NULL; 
    IMMDevice *pEp = NULL; 
    IPropertyStore *pProps =NULL; 
    UINT count; 
    PWSTR strID; 
    PROPVARIANT pv; 

    hr = CoCreateInstance(CLSID_MMDeviceEnumerator ,NULL, CLSCTX_ALL, IID_IMMDeviceEnumerator, (void **)&pEnumerator); 
    hr = pEnumerator -> EnumAudioEndpoints(eRender,DEVICE_STATE_ACTIVE,&pCollection); 
    hr = pCollection ->GetCount(&count); 
    if(count==0) 
    { 
    } 
    for (ULONG i =0; i<count; ++i) 
    { 
     hr= pCollection->Item(i,&pEp); 
     hr = pEp ->GetId(&strID); 
     hr = pEp ->OpenPropertyStore(STGM_READ,&pProps); 
     PropVariantInit(&pv); 
     hr = pProps ->GetValue(PKEY_Device_FriendlyName, &pv); 
     printf("Enpoint %d: \"%S\" (%S)\n",i,pv.pwszVal, strID); 
    } 
} 

但是我得到它打破了程序運行時錯誤Unhandled exception at 0x008e104a in EPAudioControl.exe: 0xC0000005: Access violation reading location 0x00000000.

hr = pEnumeartor -> ...行。

該程序是否需要某種特殊的Windows權限才能正常運行?

回答

0

原來pEnumerator是一個NULL指針。 CoCreateInstance調用未正確填充pEnumerator。這是因爲在CoCreateInstance之前必須有一個CoInitialize(NULL)調用。一旦我將CoInitiliaze(NULL)調用放入程序中,錯誤就消失了。