Modified error messages and loop
#define WIN32_DEFAULT_LIBS
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <mmsystem.h>
//#include <mmreg.h>
//#include <stdlib.h>
//#include <math.h>
#include <stdio.h>
#pragma lib "winmm.lib"
#define CHANNEL 1
#define FREQUENCY 44100
#define PITCH 16
WAVEFORMATEX Format;
HWAVEOUT Handle;
WAVEHDR Header;
unsigned short Buffer = 0;
char *WinMMErrorString(MMRESULT res)
{
static char szError[100];
waveOutGetErrorText(res, szError, sizeof(szError));
return szError;
}
void CALLBACK SoundHandler(HWAVEOUT Handle, UINT Message, DWORD Instance, DWORD High, DWORD Low)
{
Buffer = FREQUENCY / 2;
}
int WINAPI WinMain(HINSTANCE Instance, HINSTANCE Previous, LPSTR Command, int Show)
{
int Result = 0;
//memset(&Format, 0, sizeof(Format));
Format.wFormatTag = WAVE_FORMAT_PCM;
Format.nChannels = CHANNEL;
Format.nSamplesPerSec = FREQUENCY;
Format.wBitsPerSample = PITCH;
Format.nBlockAlign = Format.nChannels * Format.wBitsPerSample / 8;
Format.nAvgBytesPerSec = Format.nSamplesPerSec * Format.nBlockAlign;
Format.cbSize = 0;
Result = waveOutOpen(&Handle, WAVE_MAPPER, &Format, (DWORD)SoundHandler, (DWORD)GetModuleHandle(NULL), CALLBACK_FUNCTION);
if (Result != MMSYSERR_NOERROR) {
char Buffer[100];
snprintf(Buffer, sizeof(Buffer), "Error no. %d %s at line %d", Result, WinMMErrorString(Result), __LINE__);
MessageBox(0, Buffer, "Error Message", MB_ICONERROR | MB_OK);
return 1;
}
memset(&Header, 0, sizeof(Header));
Header.lpData = (char *)&Buffer;
Header.dwBufferLength = sizeof(Buffer);
Header.dwFlags = 0;
Result = waveOutPrepareHeader(Handle, &Header, sizeof(Header));
if (Result != MMSYSERR_NOERROR) {
char Buffer[32];
snprintf(Buffer, sizeof(Buffer), "Error no. %d at line %d", Result, __LINE__);
MessageBox(0, Buffer, "Error Message", MB_ICONERROR | MB_OK);
return 2;
}
do {
Result = waveOutWrite(Handle, &Header, sizeof(Header));
if (Result != MMSYSERR_NOERROR && Result != WAVERR_STILLPLAYING) {
char Buffer[100];
snprintf(Buffer, sizeof(Buffer), "Error no. %d %s at line %d", Result, WinMMErrorString(Result), __LINE__);
MessageBox(0, Buffer, "Error Message", MB_ICONERROR | MB_OK);
return 3;
}
Sleep(100);
} while(Result == MMSYSERR_NOERROR);
/*
while (1) {
if ((Header.dwFlags & WHDR_PREPARED) == WHDR_PREPARED || (Header.dwFlags & WHDR_DONE) == WHDR_DONE) {
Result = waveOutWrite(Handle, &Header, sizeof(Header));
if (Result != MMSYSERR_NOERROR) {
char Buffer[100];
snprintf(Buffer, sizeof(Buffer), "Error no. %d %s at line %d", Result, WinMMErrorString(Result), __LINE__);
MessageBox(0, Buffer, "Error Message", MB_ICONERROR | MB_OK);
return 3;
}
}
}
*/
return 0;
}