1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
|
#include <windows.h>
#include <tchar.h>
#include <conio.h> // for _tcprintf
#pragma comment(lib, "Winmm.lib")
bool FileExists (TCHAR* szFileName)
{
DWORD result = 0L;
WIN32_FIND_DATA fd;
HANDLE hFile;
hFile = FindFirstFile (szFileName, &fd);
if (hFile == INVALID_HANDLE_VALUE)
{
return false;
}
FindClose (hFile);
return true;
}
void ShowWinError ()
{
TCHAR szBuf[1024];
TCHAR *lpMsgBuf;
DWORD err_code = GetLastError ();
FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
0, err_code, MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
(TCHAR*)&lpMsgBuf, 0, 0);
_tcprintf (szBuf, _T("Error: %d: %s"), err_code, lpMsgBuf);
MessageBox (NULL, szBuf, _T("Error"), MB_OK);
LocalFree (lpMsgBuf);
}
int main ()
{
TCHAR szFileName[] = _T ("C:\\Windows\\Media\\Calm\\nuke.wav");
if (!FileExists (szFileName))
{
::MessageBox (0, _T ("File does not exist"), _T ("MISSING FILE"), MB_OK | MB_ICONERROR);
return EXIT_FAILURE;
}
if (!PlaySound (szFileName, 0, SND_FILENAME | SND_NODEFAULT))
{
ShowWinError ();
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
|