/* PLAY.C - Play a .WAV File */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include "getopt.h"

#include <windows.h>

LPSTR lpszFile = "FILE.WAV"; 

static void
PrintErr(
    char    *why
){
    LPVOID lpMsgBuf;
    char buf[1024];
 
    FormatMessage( 
        FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
        NULL,
        GetLastError(),
        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
        (LPTSTR) &lpMsgBuf,
        0,
        NULL 
    );
    /* Note: lpMsgBug terminated with \n! */
    sprintf(buf,"%s: Error %lu %s", why, GetLastError(), lpMsgBuf);
    MessageBox((HWND)0, buf, why, MB_OK | MB_ICONSTOP);
}

HINSTANCE hinst;  
HWND hwndMain; 

WNDPROC WndProc(
    WNDPROC lpPrevWndFunc,	// pointer to previous procedure
    HWND hWnd,	// handle to window
    UINT Msg,	// message
    WPARAM wParam,	// first message parameter
    LPARAM lParam 	// second message parameter
) {
    static int bPlayed = FALSE;

    if (!bPlayed) {
        sndPlaySound(lpszFile, SND_SYNC | SND_NODEFAULT);
        ///PrintErr(lpszFile);
        bPlayed = TRUE;
    }

    SendMessage (hWnd, WM_CLOSE, 0, 0L) ;
    return 0 ;
}
 
int APIENTRY WinMain(hInstance, hPrevInstance, lpszCmdLine, 
    nCmdShow) 
HINSTANCE hInstance; 
HINSTANCE hPrevInstance; 
LPSTR lpszCmdLine; 
int nCmdShow; 
{ 
    MSG msg; 
    WNDCLASS wc; 
    UNREFERENCED_PARAMETER(lpszCmdLine); 
 
    if (lpszCmdLine && *lpszCmdLine) lpszFile = lpszCmdLine; 

    /* Register the window class for the main window. */ 
 
    if (!hPrevInstance) { 
        wc.style = 0; 
        wc.lpfnWndProc = (WNDPROC) WndProc; 
        wc.cbClsExtra = 0; 
        wc.cbWndExtra = 0; 
        wc.hInstance = hInstance; 
        wc.hIcon = LoadIcon((HINSTANCE) NULL, 
            IDI_APPLICATION); 
        wc.hCursor = LoadCursor((HINSTANCE) NULL, 
            IDC_ARROW); 
        wc.hbrBackground = GetStockObject(WHITE_BRUSH); 
        wc.lpszMenuName =  "MainMenu"; 
        wc.lpszClassName = "MainWndClass"; 
 
        if (!RegisterClass(&wc)) 
            return FALSE; 
    } 
 
    hinst = hInstance;  /* save instance handle */ 
 
    /* Create the main window. */ 
 
    hwndMain = CreateWindow("MainWndClass", "Sample", 
        WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 
        CW_USEDEFAULT, CW_USEDEFAULT, (HWND) NULL, 
        (HMENU) NULL, hinst, (LPVOID) NULL); 
 
    /* 
     * If the main window cannot be created, terminate 
     * the application. 
     */ 
 
    if (!hwndMain) 
        return FALSE; 

    /* Show the window and paint its contents. */ 
 
    ShowWindow(hwndMain, nCmdShow); 
    UpdateWindow(hwndMain); 
 
    return FALSE;
 
    /* Start the message loop. */ 
    while (GetMessage(&msg, (HWND) NULL, 0, 0)) { 
        TranslateMessage(&msg); 
        DispatchMessage(&msg); 
    } 
 
    /* Return the exit code to Windows. */ 
 
    return msg.wParam; 
} 
