Creating a DirectSound Object Using CoCreateInstance

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

1Initialize COM at the start of your application using CoInitialize(NULL).

if (FAILED(CoInitialize(NULL)))

return FALSE;

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

dsrval = CoCreateInstance(&CLSID_DirectSound,

NULL,

CLSCTX_ALL,

&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. lpds is the uninitialized object CoCreateInstance returns.

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

Before closing the application, shut down COM using CoUninitialize, as shown below.

CoUnitialize()