Creating a DirectSound Object by Using CoCreateInstance

Use the following steps to create an instance of a DirectSound object by using CoCreateInstance:

1Initialize COM at the start of your application by calling CoInitialize and specifying NULL.

if (FAILED(CoInitialize(NULL)))

return FALSE;

2Create your DirectSound object by using CoCreateInstance and the IDirectSound::Initialize method, rather than the DirectSoundCreate function.

dsrval = CoCreateInstance(&CLSID_DirectSound,

NULL, &IID_IDirectSound, &lpds);

if(!FAILED(dsrval))

dsrval = IDirectSound_Initialize(lpds, NULL);

CLSID_DirectSound is the class identifier of the DirectSound driver object class and IID_IDirectSound is the DirectSound interface that you should use. The lpds parameter is the uninitialized object CoCreateInstance returns.

Before you use the DirectSound object, you must call IDirectSound::Initialize. This method takes the driver GUID parameter that DirectSoundCreate typically uses (NULL in this case). After the DirectSound object is initialized, you can use and release the DirectSound object as if it had been created by using DirectSoundCreate.

Before you close the application, shut down COM by using CoUninitialize, as follows:

CoUnitialize();