CComActivator::DOCreateInstance throws access violation in 32-bit code.

Norman Viljoen 1 Reputation point
2022-05-12T18:11:33.903+00:00

Hi All,

I am seeing an exception running 32-bit code using CComActivator::DOCreateInstance. This call causes an access violation when invoking CoCreateInstance to instantiate a device enumerator object in a 32-bit DLL. The same code compiled 64-bit invoking the same enumerator in a 64-bit DLL works perfectly.

IMMDeviceEnumerator* pDeviceEnumerator = NULL;
CoCreateInstance(__uuidof(MMDeviceEnunerator), NULL, CLCTX_INPROC_SERVER, __uuidof(IMMDeviceEnumerator), (LPVOID*)&pDeviceEnumerator);

push 0
push eax
push 1
push 0
push dword ptr [ebp+10h]
mov ecx, ebx
call combase!CComActivator::DOCreateInstance
(3bc.1cf4): Access violation - code c0000005 (first chance)

BTW. The calling convention used in the DLL for all the enumerator methods is STDMETHODCALLTYPE, which is the required calling convention through class inheritance. What am I missing here? Is it a calling convention problem? Any ideas?

Sincerely,
Norman

Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,415 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Norman Viljoen 1 Reputation point
    2022-05-17T10:56:03.91+00:00

    I found the solution to this. The 32-bit call is expected to use the __stdcall calling convention. But using this convention causes the CoCreateInstance to not find the GetTSAudioEndpointEnumeratorForSession. To fix that I used a pragma:
    #pragma comment(linker, "/export:GetTSAudioEndpointEnumeratorForSession=_GetTSAudioEndpointEnumeratorForSession@8")
    Now the 32-bit code works as well as the 64-bit code.