void
CEnumCodecDlg::EnumCodec()

...
{
::CoInitialize(NULL);
//定义一个ProfileManager;
IWMProfileManager * pProfileMgr = NULL;
//HRESULT hr = WMCreateProfileManager()
// Create Profile Manager;
HRESULT hr = WMCreateProfileManager(&pProfileMgr);
if (FAILED(hr))

...{
printf("Failed to Create a Profile Manager. ");
}
//定义一个编码器信息结构变量
IWMCodecInfo3 * pCodecInfo = NULL;

//获取编码器信息接口指针。
hr = pProfileMgr->QueryInterface(IID_IWMCodecInfo3,(void**) &pCodecInfo);
//得到IWMCodecInfo3指针后安全释放PROFILEMANAGER的指针。
SAFE_RELEASE(pProfileMgr);
//枚举VIdeo的编码器
GetCodecNames(pCodecInfo,WMMEDIATYPE_Video);
GetCodecNames(pCodecInfo, WMMEDIATYPE_Audio);
//释放IWMCodecInfo3指针。
SAFE_RELEASE(pCodecInfo);
::CoUninitialize();
}

HRESULT CEnumCodecDlg::GetCodecNames( IWMCodecInfo3
*
pCodecInfo, REFGUID inType )

...
{
if (inType == WMMEDIATYPE_Audio)

...{
printf(" All audio codecs are listed as the following... ");
}
else

...{
printf(" All video codecs are listed as the following... ");
}

DWORD cCodecs = 0;
WCHAR* pwszCodecName = NULL;
DWORD cchCodecName = 0;
// 获取编码器数量
HRESULT hr = pCodecInfo->GetCodecInfoCount(inType, &cCodecs);
if (SUCCEEDED(hr))

...{
printf("Count: %d ", cCodecs);
}
else

...{
printf("Could not get the count of codecs. ");
return hr;
}

// Loop through all the audio codecs.
for (DWORD dwCodecIndex = 0; dwCodecIndex < cCodecs; dwCodecIndex++)

...{
// Get the codec name:
// First, get the size of the name.
//获取编码器字符长度
hr = pCodecInfo->GetCodecName(inType, dwCodecIndex,
NULL, &cchCodecName);
if (FAILED(hr))

...{
printf("Could not get the size of the codec name. ");
return hr;
}

// 给字符串分配空间
pwszCodecName = new WCHAR[cchCodecName];
if (pwszCodecName == NULL)

...{
printf("Could not allocate memory. ");
return E_OUTOFMEMORY;
}
// 获取编码器名称
hr = pCodecInfo->GetCodecName(inType, dwCodecIndex,
pwszCodecName, &cchCodecName);
if (FAILED(hr))

...{
delete[] pwszCodecName;
printf("Could not get the codec name. ");
return hr;
}

// Print the codec name.
//printf("%d %ws ", dwCodecIndex, pwszCodecName);
char* pszCodecName = new char[cchCodecName];
WideCharToMultiByte ( CP_ACP, // ANSI 代码页
0, // 检查重音字符
pwszCodecName, // 原Unicode 串
-1, // -1 意思是串以0x00结尾
pszCodecName, // 目的char字符串
cchCodecName, // 缓冲大小
NULL, // 肥缺省字符串
NULL ); // 忽略这个参数
//添加到列表。

m_ListBox.AddString(pszCodecName);


// Clean up for the next iteration.
delete[] pwszCodecName;
pwszCodecName = NULL;
cchCodecName = 0;
}

return FALSE;
}
以上的代码是枚举当前的VIDEO和AUDIO的编码器的名称。从上边我们可以看到我们首先建立一个PROFILEMANAGER的指针对象,然后通过他来得到WMCODECINFO的指针,通过该指针我们实现了枚举过程。