DirectSound linker error please help!

5657c3c757943b66905353f82ffb4cbc
0
helpthisProgrammer 101 Apr 15, 2008 at 02:15

Hi all, this is my first post here so please don’t be angry if this has already been covered or if it’s under the wrong forum. Thanks \^.\^

Anyways I found the following code from another thread of this forum for creating primary and secondary buffers in DirectSound, but it has two linker errors that I’m not sure how to fix…
I have the DirectX SDK installed…

Here are the errors

DirectSound.obj : error LNK2028: unresolved token (0A00028A) “extern “C” long __stdcall DirectSoundCreate8(struct _GUID const *,struct IDirectSound8 * *,struct IUnknown *)” (?DirectSoundCreate8@@$$J212YGJPBU_GUID@@PAPAUIDirectSound8@@PAUIUnknown@@@Z) referenced in function “int __cdecl main(int,char * * const)” (?main@@$$HYAHHQAPAD@Z)
DirectSound.obj : error LNK2019: unresolved external symbol “extern “C” long __stdcall DirectSoundCreate8(struct _GUID const *,struct IDirectSound8 * *,struct IUnknown *)” (?DirectSoundCreate8@@$$J212YGJPBU_GUID@@PAPAUIDirectSound8@@PAUIUnknown@@@Z) referenced in function “int __cdecl main(int,char * * const)” (?main@@$$HYAHHQAPAD@Z)

Code:

//#include "stdafx.h"
#include <dsound.h>
#define _USE_MATH_DEFINES
#define _WIN32_DCOM
#include <cmath>
#include <windows.h>
#include <string>
#include <iostream>


using namespace std;

LPDIRECTSOUND8 lpds;
LPDIRECTSOUNDBUFFER lpdsbuffer;
WAVEFORMATEX wfx;
DSBUFFERDESC dsbdesc;
HWND hwnd;
short* buffer = new short[44100 * 2];
bool playing = false;

HWND GetConsoleHwnd(void){
       HWND cWindowHandle;         
       LPCSTR newtitle = (LPCSTR) L"Audio Programming - Lesson 1";
       SetConsoleTitle(newtitle);
       Sleep(40);
       cWindowHandle=FindWindow(NULL, newtitle);
       return(cWindowHandle);
}

HRESULT createSoundObject(void){
    HRESULT hr;
    hr = DirectSoundCreate8(NULL,&lpds,NULL);
    hr = CoInitializeEx(NULL, 0);
    hr = lpds->SetCooperativeLevel(hwnd,DSSCL_NORMAL);
    return hr;
}

WAVEFORMATEX setWaveFormat(void){
    WAVEFORMATEX wfx; 
    memset(&wfx, 0, sizeof(WAVEFORMATEX)); 
    wfx.wFormatTag = WAVE_FORMAT_PCM; 
    wfx.nChannels = 2; 
    wfx.wBitsPerSample = 16;
    wfx.nSamplesPerSec = 44100; 
    wfx.nBlockAlign = 4;
    wfx.nAvgBytesPerSec = 44100 * 4; 
    return wfx;
}

DSBUFFERDESC setBufferDescription(){
    DSBUFFERDESC dsbdesc;
    memset(&dsbdesc, 0, sizeof(DSBUFFERDESC)); 
    dsbdesc.dwSize = sizeof(DSBUFFERDESC); 
    dsbdesc.dwFlags = DSBCAPS_CTRLPOSITIONNOTIFY | DSBCAPS_GETCURRENTPOSITION2;
    dsbdesc.dwBufferBytes = 1024;
    dsbdesc.lpwfxFormat = &wfx;
    return dsbdesc;
}

HRESULT createSecondarySoundBuffer(void){
    HRESULT hr = lpds->CreateSoundBuffer(&dsbdesc,&lpdsbuffer,NULL);
    return hr;
}

HRESULT fillSoundBuffer(bool first){
    bool firsthalf = first;
    LPVOID lpvWrite;
    DWORD  dwLength;
    HRESULT hr = lpdsbuffer->Lock(0,dsbdesc.dwBufferBytes,&lpvWrite,&dwLength,NULL,NULL,DSBLOCK_ENTIREBUFFER);
    if(SUCCEEDED(hr)){
        memcpy(lpvWrite, buffer, dwLength);
        hr = lpdsbuffer->Unlock(lpvWrite,dwLength,NULL,NULL);
    }
    return hr;
}

BOOL fillBufferWithWaveForm(string form){
    
    if(form == "sine"){
            short* ptr = buffer;
            unsigned short i;
            float amp = 1.0f;
            float tmp;
            short output;
            for(i=0;i<44100;i++){
                tmp = amp * sinf(1000.0f * 2.0f * M_PI * ((float)i/44100.0f));
                output = (short)(tmp*32767);
                ptr[0] = output;
                ptr[1] = output;
                ptr += 2;
            }
            return true;
    }
}


int _tmain(int argc, char* argv[])
{
    HRESULT hr;
    BOOL formFound = false;
    string waveform = "sine";

      
    // Rename the ConsoleWindow and retrieve its 
    // handle by looking up its new name in Windows
    hwnd = GetConsoleHwnd();

    // Create the Basic LPDIRECTSOUND8 object
    hr = createSoundObject();

    // Set the default wave format (hard coded)
    wfx = setWaveFormat();

    // Set the default buffer description (hard coded)
    dsbdesc = setBufferDescription();

    // Create the secondary sound buffer
    hr = createSecondarySoundBuffer();

    // Generate a waveform and put it into the playback array
    formFound = fillBufferWithWaveForm(waveform);

    // fill the secondary sound buffer entirely with the playback array data
    hr = fillSoundBuffer(true);

    // Start playback at position 0 and loop this sound until explicitely stopped
    lpdsbuffer->SetCurrentPosition(0);
    
    // Create Notification Handles
    HANDLE NotifyEvent[2];
    NotifyEvent[0] = CreateEvent(NULL,TRUE,FALSE,NULL);
    NotifyEvent[1] = CreateEvent(NULL,TRUE,FALSE,NULL);
    
    LPDIRECTSOUNDNOTIFY8 lpDsNotify;
    DSBPOSITIONNOTIFY PositionNotify[2];
    
    if (hr = lpdsbuffer->QueryInterface(IID_IDirectSoundNotify8,(LPVOID*)&lpDsNotify) == DS_OK){ 
        PositionNotify[0].dwOffset = 512;
        PositionNotify[0].hEventNotify = NotifyEvent[0];
        PositionNotify[1].dwOffset = 1024;
        PositionNotify[1].hEventNotify = NotifyEvent[1];
        if(hr = lpDsNotify->SetNotificationPositions(2, PositionNotify) != DS_OK){
            cout << hr << endl;
            cout << "Error while setting up Notification Positions!" << endl;
        }
        else{
            lpDsNotify->Release();
        }
    }
    else{
        cout << "Notification settings failed!" << endl;
    }
    
    
    hr = lpdsbuffer->Play(0,0,DSBPLAY_LOOPING);
    printf("Buffer set up successfully! Playing back sound... \n \n");
    
    playing = true;
    while(playing){
        hr = WaitForMultipleObjects(2,NotifyEvent,FALSE,INFINITE);
        //cout << hr << endl;
        if(hr-WAIT_OBJECT_0 == 0){
            cout << "Notify bei 512!" << endl;
            ResetEvent(NotifyEvent[0]);
        }
        else if(hr-WAIT_OBJECT_0 == 1){
            cout << "Notify bei 1024!" << endl;
            ResetEvent(NotifyEvent[1]);
        }
    }
    
    system("PAUSE");
    lpdsbuffer->Stop();
    lpdsbuffer->Release();
    lpds->Release();
    return 0;
}

I tired creating another directSound object and set its cooperative level in a separate solution but got the same error. Any help is greatly appreciated

Thanks.

5 Replies

Please log in or register to post a reply.

6837d514b487de395be51432d9cdd078
0
TheNut 179 Apr 15, 2008 at 03:29

In your project or compiler settings, make sure to link your executable with:
dxerr.lib
dsound.lib
dxguid.lib
winmm.lib

You need these four in order to successfully link with DirectSound.

A8433b04cb41dd57113740b779f61acb
0
Reedbeta 167 Apr 15, 2008 at 04:16

Also, for future reference please use the …[/code[b][/b]] tags when posting code in the forum. [code]…[/code**] tags when posting code in the forum.

5657c3c757943b66905353f82ffb4cbc
0
helpthisProgrammer 101 Apr 15, 2008 at 06:27

Thanks for the help so far… but now I’ve got this linker error…

LINK : fatal error LNK1181: cannot open input file ‘dxerr.lib’

6837d514b487de395be51432d9cdd078
0
TheNut 179 Apr 15, 2008 at 11:18

Check the lib folder where you installed your DirectX SDK to and verify the existence of all the libs I mentioned. Perhaps Microsoft renamed it.

5657c3c757943b66905353f82ffb4cbc
0
helpthisProgrammer 101 Apr 16, 2008 at 16:18

Ok so now I’ve got the library problems sorted out but now it’s led me right back to the same linker error…

directSoundtry2.obj : error LNK2019: unresolved external symbol _DirectSoundCreate@12 referenced in function “int __cdecl Sound_Initialization(void)” (?Sound_Initialization@@YAHXZ)

all the additional directories under Options -> VC++ directories have been included

i included the libs like this

#pragma comment [lib, "dxerr.lib"]
#pragma comment [lib, "dsound.lib"]
#pragma comment [lib, "dxguid.lib"]
#pragma comment [lib, "winmm.lib"]