Function Overview
DirectSoundCreate creates a DirectSound object used for playing and capturing audio.
HRESULT DirectSoundCreate(
LPCGUID pcGuidDevice,
LPDIRECTSOUND *ppDS,
LPUNKNOWN pUnkOuter
);
Available Since: DirectX 3.0
Parameters
| Parameter | Type | Description |
|---|---|---|
| pcGuidDevice | LPCGUID | Pointer to GUID of the sound device. Use NULL for default device. |
| ppDS | LPDIRECTSOUND * | Address of a pointer that receives the DirectSound interface pointer. |
| pUnkOuter | LPUNKNOWN | Must be NULL (DirectSound does not support aggregation). |
Return Value
Returns an HRESULT indicating success or failure.
DS_OK– Success.DSERR_ALLOCATED– Another application already has exclusive access.DSERR_INVALIDPARAM– Invalid pointer.DSERR_OUTOFMEMORY– Memory allocation failed.- Other DirectSound error codes.
Remarks
Before calling any DirectSound methods, you must call DirectSoundCreate to obtain a DirectSound object. After creation, use IDirectSound::SetCooperativeLevel to set the priority.
For multi‑device environments, retrieve device GUIDs via DirectSoundEnumerate.
Example
#include <dsound.h>
#pragma comment(lib, "dsound.lib")
int main() {
LPDIRECTSOUND pDS = nullptr;
HRESULT hr = DirectSoundCreate(NULL, &pDS, NULL);
if (FAILED(hr)) {
// handle error
return -1;
}
// Set cooperative level
hr = pDS->SetCooperativeLevel(GetConsoleWindow(), DSSCL_PRIORITY);
// ... use DirectSound
pDS->Release();
return 0;
}