Newsgroup: it.comp.lang.c++
Titolo: Aiuto!! Controllo porte

Sono poco esperto di c++, ma devo costruire un modulo per
il vontrollo di un evento su una porta (seriale o
parellela), monitorando i segnali che arrivano sui vari pin
della stessa, ma non so da che parte cominciare....
aiutatemi!!!
* Sent from AltaVista 
http://www.altavista.com
 Where you can also find related Web Pages, Images, Audios, Videos, News, and Shopping.  Smart is Beautiful

Alessandro <afalasca...@hotmail.com.invalid> wrote in message
002e299c...@usw-ex0109-068.remarq.com...

#define STRICT
#define _MT
#define rxport1 0x379  // data 0x37A
#define txport1 0x378
#define rxport2 0x279
#define txport2 0x278
#define rxport3 0x379
#define txport3 0x378
#define WM_DONE (WM_USER + 1);
#include <process.h>            //
#include <conio.h>
#include <windows.h>
#include <windowsx.h>
#include <commdlg.h>            // common dialog box (for file names)
#include <mmsystem.h>           // multimedia definitions
#include <stdlib.h>             // _MAX_* constants
#include "showwave.h"           // program variables and functions
#include "rsrc.h"               // program resource ID definitions
/*--------------------------------------------------------------------
        PROTOTYPES FOR PRIVATE FUNCTIONS
  --------------------------------------------------------------------*/
static void ShowWave_OnSize ( HWND hDlg, int i, WPARAM wParam, LPARAM
lParam );
static BOOL ShowWave_OnInitDialog( HWND hDlg, HWND hWndFocus, LPARAM
lParam );
static HBRUSH ShowWave_OnCtlColor( HWND hDlg, HDC hDC, HWND hCtl, int
iType );
static void ShowWave_OnCommand( HWND hDlg, int iCmd, HWND hCtl, UINT
uCode );
static void ShowWave_OnHScroll( HWND hDlg, HWND hCtl, UINT uCode, int
iPos );
static BOOL ShowWave_OnQueryEndSession( HWND hDlg );
static void ShowWave_OnDestroy( HWND hDlg );
static void PaintWaveData( HWND hDlg );
static BOOL GetFileName( HWND hDlg, BOOL bOpenName, LPSTR lpszFile,
    int iMaxFileNmLen, LPSTR lpszFileTitle, int iMaxFileTitleLen );
static void FreeGlobalWaveData( void );
static void NewWaveFile( HWND hDlg );
static void OpenWaveFile( HWND hDlg );
static void SaveWaveFile( HWND hDlg, BOOL bAskForName );
static void RecordWaveFile( HWND hDlg );
static void Trigger( HWND hDlg );
static void Zoom( HWND hDlg, BOOL out );
static void Port( HMENU menu , int port );
static void SetSpeed( HMENU menu , int port );
static void SetSize( HMENU menu , int port );
static void NotImplemented ( HWND hDlg );
static void MemTrig ( BYTE num );
static void PortTrig( BYTE num );
static void CheckData ( HWND hDlg );
static void CloseRecord( HWND hDlg );
/*--------------------------------------------------------------------
        STATIC VARIABLES
  --------------------------------------------------------------------*/
static char szOFNDefExt[]     = "WAV";  // for file name dialog
static char *szOFNFilter[]    =
{
    "Scope Files (*.SCO)\0 *.SCO\0, All Files(*.*)\0    *.* \0\0"
};
static int iHScrollPos = 0;             // scroll bar thumb position
static char szFileName[_MAX_PATH];      // file handling
static char szFileTitle[_MAX_FNAME];
static HBRUSH hbrBackgnd;               // for dialog box's background
static DWORD  dwbufsize = 5000;
static char speed = 0; // 0 = fastest, 1-10 = fast, 11-100 = slow
static char port  = 1; // 1 = lpt1,    2 = lpt2,    3 = lpt3
static int cxblock,cyblock;
typedef struct
{
 HWND   hDlg;
 HANDLE hEvent;
 BOOL   done;
 DWORD  speed;
}
PARAMS, *PPARAMS;
static HANDLE hEvent;
static PARAMS params;
static BOOL killthread = FALSE;
/*--------------------------------------------------------------------
        SHOWWAVE DIALOG PROCEDURE
        Sort out messages for the program's main window, the ShowWave
        dialog box.
  --------------------------------------------------------------------*/
LRESULT WINAPI ShowWave_WndProc (
    HWND hDlg,
    UINT uMsg,
    WPARAM wParam,
    LPARAM lParam )
{
 file://LPMINMAXINFO lpmmi;
    switch (uMsg)
    {
        HANDLE_MSG( hDlg, WM_INITDIALOG, ShowWave_OnInitDialog );
        HANDLE_MSG( hDlg, WM_CTLCOLORSTATIC, ShowWave_OnCtlColor );
        HANDLE_MSG( hDlg, WM_CTLCOLORBTN, ShowWave_OnCtlColor );
        HANDLE_MSG( hDlg, WM_COMMAND, ShowWave_OnCommand );
        HANDLE_MSG( hDlg, WM_HSCROLL, ShowWave_OnHScroll );
        HANDLE_MSG( hDlg, WM_QUERYENDSESSION, ShowWave_OnQueryEndSession );
        HANDLE_MSG( hDlg, WM_DESTROY, ShowWave_OnDestroy );
        file://HANDLE_MSG( hDlg, WM_SIZE, ShowWave_OnSize );
  /*
  case  WM_GETMINMAXINFO:
   lpmmi = (LPMINMAXINFO) lParam;
   lpmmi->ptMaxTrackSize.y = 412;
   lpmmi->ptMinTrackSize.y = 412;
   return 0;
   break;
  */
  case  WM_USER + 1:
   zoom = 1;
   CloseRecord( hDlg );
   PaintWaveData( hDlg );
   CheckData( hDlg );
   MessageBeep(MB_OK);
   return 0;
   break;
        default:
            return( DefWindowProc(hDlg, uMsg, wParam, lParam) );
     }
}
/*--------------------------------------------------------------------
        THREAD
  --------------------------------------------------------------------*/
void Thread(PVOID pvoid)
{
 DOUBLE l = 1.0;
 DWORD i,k;
 PPARAMS pparams;
    LPSTR lp;  // points to one sample in wave data
 BYTE data;
 unsigned short inp_port;
 pparams = (PPARAMS) pvoid;
 while (TRUE)
 {
  WaitForSingleObject (pparams -> hEvent, INFINITE);
  if (killthread) break;
  if (port == 1) inp_port = rxport1;
  if (port == 2) inp_port = rxport2;
  if (port == 3) inp_port = rxport3;
  breakit = FALSE;
  lp = lpWaveData;
  if (btrig != 0)  // trigger sequence
  {
   data = btrig & (BYTE)_inp(inp_port);
   while (!breakit)
   {
    if (data != (btrig & (BYTE)_inp(inp_port))) break;
   }
  }
  for (i=0;i<dwWaveDataSize;i++)
  {
   for (k=0;k<20 * pparams->speed;k++) l = l / 0.9;
   file://Sleep(1);
      *lp++ = (BYTE)_inp(inp_port);
   if (breakit) break;
  }
  dwWaveDataSize = i - 1;
  pparams->done = !breakit;
  SendMessage (pparams -> hDlg, WM_USER + 1, 0, 0 );
 }
}
/*--------------------------------------------------------------------
        SHOWWAVE_OnSize
  --------------------------------------------------------------------*/
/*
static void ShowWave_OnSize (
    HWND hDlg,
 int i,
 WPARAM wParam,
    LPARAM lParam )
{
   cxblock = LOWORD(wParam);
   cyblock = HIWORD(wParam);
   MoveWindow(GetDlgItem( hDlg, IDD_SCROLLBAR ),
        80,
        309,
        (cxblock) - 87,
        15,
        TRUE);
   MoveWindow(GetDlgItem( hDlg, IDD_SHOWWAVE ),
        81,
        10,
        (cxblock) - 88,
        286,
        TRUE);
    UNREFERENCED_PARAMETER( i );
    UNREFERENCED_PARAMETER( lParam );
}
/*--------------------------------------------------------------------
        SHOWWAVE_ONINITDIALOG
        Handle WM_INITDIALOG here. Initialize program settings.
  --------------------------------------------------------------------*/
static BOOL ShowWave_OnInitDialog (
    HWND hDlg,
    HWND hwndFocus,
    LPARAM lParam )
{
    char szTempPath[_MAX_PATH];
 hEvent = CreateEvent(NULL,FALSE,FALSE,NULL);
 params.hDlg = hDlg;
 params.hEvent = hEvent;
 params.done = FALSE;
 params.speed = 0;
 _beginthread (Thread, 0, &params); file://LIBCMT.LIB
    /* create a brush for painting the dialog box's background */
    hbrBackgnd = CreateSolidBrush( GetSysColor(COLOR_BTNFACE) );
    /* get file name for temporary storage of waveform data */
    GetTempPath( sizeof(szTempPath), szTempPath );
    /* start out with an empty wave file */
    FORWARD_WM_COMMAND( hDlg, IDM_NEW, NULL, 0, PostMessage );
 /* do nothing with the focus */
    return( TRUE );
    UNREFERENCED_PARAMETER( hwndFocus );
    UNREFERENCED_PARAMETER( lParam );
}
/*--------------------------------------------------------------------
        SHOWWAVE_ONCTLCOLOR
        Process WM_CTLCOLOR messages here.  Give text controls
        a white background.  Shade other parts of dialog.
        RETURN
        A brush handle to use in painting the background.  The
        handle used is declared with the other static variables
        near the top of this file.
  --------------------------------------------------------------------*/
static HBRUSH ShowWave_OnCtlColor (
    HWND hDlg,                          // parent dialog window
    HDC hDC,                            // DC for control's client area
    HWND hCtl,                          // control window
    int iType )                         // type of control
{
 SetTextColor( hDC, GetSysColor(COLOR_BTNTEXT) );
 SetBkColor( hDC, GetSysColor(COLOR_BTNFACE) );
 return( hbrBackgnd );
// return;
 UNREFERENCED_PARAMETER( hDlg );
 UNREFERENCED_PARAMETER( hCtl );
}
/*--------------------------------------------------------------------
        SHOWWAVE_ONCOMMAND
        Handle WM_COMMAND messages here.  Respond to actions from
        each dialog control and from the menu.
  --------------------------------------------------------------------*/
static void ShowWave_OnCommand (
    HWND hDlg,
    int iCmd,
    HWND hCtl,
    UINT uCode )
{
 static int iSelection;
    switch (iCmd)
    {
        case IDM_NEW:                       // clear data buffer
            NewWaveFile( hDlg );
   RecordWaveFile( hDlg );
            break;
        case IDM_OPEN:                      // load a disk file
            OpenWaveFile( hDlg );
            break;
        case IDM_SAVE:                      // save to disk file
        case IDM_SAVEAS:
            SaveWaveFile( hDlg, (iCmd==IDM_SAVEAS) );
            break;
        case IDM_EXIT:                      // end program
            FORWARD_WM_CLOSE( hDlg, PostMessage );
            break;
        case ID_BUFFER_SIZE_5000:
   SetSize( GetMenu(hDlg) , iCmd );
   dwbufsize = 5000;
            break;
        case ID_BUFFER_SIZE_30000:
   SetSize( GetMenu(hDlg) , iCmd );
   dwbufsize = 30000;
            break;
        case ID_BUFFER_SIZE_100000:
   SetSize( GetMenu(hDlg) , iCmd );
   dwbufsize = 500000;
            break;
        case ID_BUFFER_SPEED_FASTEST:
   SetSpeed( GetMenu(hDlg) , iCmd );
   speed = 0;
            break;
        case ID_BUFFER_SPEED_FAST:
   SetSpeed( GetMenu(hDlg) , iCmd );
   speed = 5;
            break;
        case ID_BUFFER_SPEED_SLOW:
   SetSpeed( GetMenu(hDlg) , iCmd );
            DialogBox( GetWindowInstance(hDlg),
                MAKEINTRESOURCE(DLG_SPEEDSET),
                hDlg, Common_DlgProc );
   speed = 50;
            break;
        case IDM_SYNC:
            Trigger( hDlg );
            break;
        case IDM_ZOOMIN:
        case IDM_ZOOMOUT:
            Zoom( hDlg , (iCmd==IDM_ZOOMOUT) );
            break;
        case IDC_SYNC1:
   MemTrig( 0x08 );
            break;
        case IDC_SYNC2:
   MemTrig( 0x10 );
            break;
        case IDC_SYNC3:
   MemTrig( 0x20 );
            break;
        case IDC_SYNC4:
   MemTrig( 0x40 );
            break;
        case IDC_SYNC5:
   MemTrig( 0x80 );
            break;
        case IDC_TRIG1:
   PortTrig( 0x08 );
            break;
        case IDC_TRIG2:
   PortTrig( 0x10 );
            break;
        case IDC_TRIG3:
   PortTrig( 0x20 );
            break;
        case IDC_TRIG4:
   PortTrig( 0x40 );
            break;
        case IDC_TRIG5:
   PortTrig( 0x80 );
            break;
        case ID_EDIT_CUTBEFORE:
   lpWaveData += dwCurrentSample;
   dwWaveDataSize -= dwCurrentSample;
   dwCurrentSample = 0;
   iHScrollPos = 0;
   ScrollBar_SetRange( GetDlgItem( hDlg, IDD_SCROLLBAR ),
        0,
        (int)(dwWaveDataSize) - dwright,
        FALSE );
   ScrollBar_SetPos( GetDlgItem( hDlg, IDD_SCROLLBAR ),
         0,
         TRUE );
            break;
        case ID_EDIT_CUTAFTER:
   dwWaveDataSize = dwCurrentSample + dwright;
   ScrollBar_SetRange( GetDlgItem( hDlg, IDD_SCROLLBAR ),
        0,
        (int)(dwWaveDataSize) - dwright,
        TRUE );
            break;
        case IDM_CONNECTIONS:               // show Connection box
            DialogBox( GetWindowInstance(hDlg),
                MAKEINTRESOURCE(DLG_CONNECTION),
                hDlg, Common_DlgProc );
            break;
        case IDM_HELP:      // show Help box
            DialogBox( GetWindowInstance(hDlg),
                MAKEINTRESOURCE(DLG_HELP),
                hDlg, Common_DlgProc );
            break;
        case IDM_TEST:                     // show About box
            DialogBox( GetWindowInstance(hDlg),
                MAKEINTRESOURCE(DLG_ABOUT),
                hDlg, Common_DlgProc );
            break;
  case ID_SETTINGS_PORT_LPT1:
   Port( GetMenu(hDlg) , iCmd );
   port = 1;
            break;
        case ID_SETTINGS_PORT_LPT2:
   Port( GetMenu(hDlg) , iCmd );
   port = 2;
            break;
        case ID_SETTINGS_PORT_LPT3:
   Port( GetMenu(hDlg) , iCmd );
   NotImplemented( hDlg );
            break;
        case IDM_BREAK:
   breakit = TRUE;
            break;
        default:
            break;
    }
    return;
    UNREFERENCED_PARAMETER( hCtl );
    UNREFERENCED_PARAMETER( uCode );
}
/*--------------------------------------------------------------------
        SHOWWAVE_ONHSCROLL
        Process WM_HSCROLL messages here.  Advance through the
        sound file according to the user's signals from the scroll
        bar.
  --------------------------------------------------------------------*/
static void ShowWave_OnHScroll (
    HWND hDlg,
    HWND hCtl,
    UINT uCode,
    int iPos )
{
    int iMinPos, iMaxPos;
    ScrollBar_GetRange( hCtl, &iMinPos, &iMaxPos );
    switch (uCode)
    {
        case SB_LINEUP:
            iHScrollPos -= 11;
            break;
        case SB_LINEDOWN:
            iHScrollPos += 11;
            break;
        case SB_PAGEUP:
            iHScrollPos -= 101;
            break;
        case SB_PAGEDOWN:
            iHScrollPos += 101;
            break;
        case SB_TOP:
            iHScrollPos = iMinPos;
            break;
        case SB_BOTTOM:
            iHScrollPos = iMaxPos;
            break;
        case SB_THUMBPOSITION:
        case SB_THUMBTRACK:
            iHScrollPos = iPos;
            break;
        default:
            return;
    }
    /* update scroll bar thumb */
    iHScrollPos = max( iMinPos, min(iHScrollPos, iMaxPos) );
    ScrollBar_SetPos( hCtl, iHScrollPos, TRUE );
    /* set current sample */
    dwCurrentSample = (iHScrollPos);
    /* paint the waveform data */
    PaintWaveData( hDlg );
    return;
}
/*--------------------------------------------------------------------
        SHOWWAVE_ONQUERYENDSESSION
        Handle WM_QUERYENDSESSION messages here.  Give the user a
        chance to save any open file before program ends.
  --------------------------------------------------------------------*/
static BOOL ShowWave_OnQueryEndSession ( HWND hDlg )
{
    return( QuerySave(hDlg) );
}
/*--------------------------------------------------------------------
        SHOWWAVE_ONDESTROY
        Handle WM_DESTROY message here.  Close all devices.
  --------------------------------------------------------------------*/
static void ShowWave_OnDestroy ( HWND hDlg )
{
 breakit = TRUE;
 killthread = TRUE;
 Sleep(0);
 SetEvent(hEvent);
 _endthread();
    DeleteBrush( hbrBackgnd );
    FreeGlobalWaveData( );   // release buffer
    PostQuitMessage( 0 );                           // send WM_QUIT
    return;
}
/*--------------------------------------------------------------------
        PAINT WAVE DATA
        Repaint the dialog box's GraphClass display control
  --------------------------------------------------------------------*/
static void PaintWaveData ( HWND hDlg )
{
    HWND hwndShowWave = GetDlgItem( hDlg, IDD_SHOWWAVE );
    InvalidateRect( hwndShowWave, NULL, TRUE );
    UpdateWindow( hwndShowWave );
    return;
}
/*--------------------------------------------------------------------
        QUERY SAVE
        Ask user to confirm loss of unsaved file before closing
  --------------------------------------------------------------------*/
BOOL QuerySave ( HWND hDlg )
{
    HINSTANCE hInstance;
    char szText[MAX_RSRC_STRING_LEN];
    char szCaption[MAX_RSRC_STRING_LEN];
    char szFormat[MAX_RSRC_STRING_LEN];
    int  iRet;
    /* see if user wants to save the modifications */
    hInstance = GetWindowInstance( hDlg );
    LoadString( hInstance, IDS_CAPTION,      szCaption, sizeof(szCaption) );
    LoadString( hInstance, IDS_CONFIRMCLOSE, szFormat,  sizeof(szFormat)  );
    wsprintf( szText, szFormat, (LPSTR)szFileTitle );
    iRet = MessageBox( hDlg, szText, szCaption,
                       MB_YESNOCANCEL | MB_ICONQUESTION );
    if (iRet == IDYES)
    {
        FORWARD_WM_COMMAND( hDlg, IDM_SAVE, NULL, 0, SendMessage );
    }
    return( iRet != IDCANCEL );
}
/*--------------------------------------------------------------------
        GET FILE NAME
        Invoke the File Open or File Save As common dialog box.
        If the bOpenName parameter is TRUE, the procedure runs
        the OpenFileName dialog box.
        RETURN
        TRUE if the dialog box closes without error.  If the dialog
        box returns TRUE, then lpszFile and lpszFileTitle point to
        the new file path and name, respectively.
  --------------------------------------------------------------------*/
static BOOL GetFileName (
    HWND hDlg,
    BOOL bOpenName,                     // open file or save as
    LPSTR lpszFile,                     // buffer for file path
    int iMaxFileNmLen,                  // max file path length
    LPSTR lpszFileTitle,                // buffer for file name
    int iMaxFileTitleLen )              // max file name length
{
    OPENFILENAME ofn;
    /* initialize structure for the common dialog box */
    lpszFile[0] = '\0';
    ofn.lStructSize       = sizeof( OPENFILENAME );
    ofn.hwndOwner         = hDlg;
    ofn.hInstance         = NULL;
    ofn.lpstrFilter       = szOFNFilter[0];
    ofn.lpstrCustomFilter = NULL;
    ofn.nMaxCustFilter    = 0;
    ofn.nFilterIndex      = 1;
    ofn.lpstrFile         = lpszFile;
    ofn.nMaxFile          = iMaxFileNmLen;
    ofn.lpstrFileTitle    = lpszFileTitle;
    ofn.nMaxFileTitle     = iMaxFileTitleLen;
    ofn.lpstrInitialDir   = NULL;
    ofn.lpstrTitle        = NULL;
    ofn.nFileOffset       = 0;
    ofn.nFileExtension    = 0;
    ofn.lpstrDefExt       = szOFNDefExt;
    ofn.lCustData         = 0;
    ofn.lpfnHook          = NULL;
    ofn.lpTemplateName    = NULL;
    /* invoke the common dialog box */
    if (bOpenName)                              // Open a file
    {
        ofn.Flags = OFN_HIDEREADONLY | OFN_PATHMUSTEXIST |
                    OFN_FILEMUSTEXIST;
        return( GetOpenFileName(&ofn) );
    }
    else                                        // Save As...
    {
        ofn.Flags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;
        return( GetSaveFileName(&ofn) );
    }
}
/*--------------------------------------------------------------------
        FREE GLOBAL WAVE DATA
        Free storage associated with the global waveform data
  --------------------------------------------------------------------*/
static void FreeGlobalWaveData ( void )
{
    if (lpWaveData)
    {
        GlobalFreePtr( lpWaveData );
        lpWaveData = NULL;
    }
    return;
}
/*--------------------------------------------------------------------
        NEW WAVE FILE
        Start work on a new .WAV file.  Reset variables and update
        display.  Called in response to the New command.
  --------------------------------------------------------------------*/
static void NewWaveFile ( HWND hDlg )
{
    HINSTANCE hInstance;
    HWND hwndScrollBar = GetDlgItem( hDlg, IDD_SCROLLBAR );
    /* set file name and title */
    hInstance = GetWindowInstance( hDlg );
    LoadString( hInstance, IDS_UNTITLED, szFileTitle,
                sizeof(szFileTitle) );
    szFileName[0] = '\0';
    FreeGlobalWaveData( );                  // delete old waveform data
    /* set the new waveform data */
    dwCurrentSample = 0;
    lpWaveData      = NULL;
    dwWaveDataSize  = 0;
    /* set the new scroll bar range */
    ScrollBar_SetRange( hwndScrollBar, 0, 1, FALSE );
    /* set the new scroll bar position */
    FORWARD_WM_HSCROLL( hDlg, hwndScrollBar, 0, SB_TOP, SendMessage );
    return;
}
/*--------------------------------------------------------------------
        OPEN WAVE FILE
        Open a new .WAV file
  --------------------------------------------------------------------*/
static void OpenWaveFile ( HWND hDlg )
{
    int  iMaxPos;
    HWND hwndScrollBar = GetDlgItem( hDlg, IDD_SCROLLBAR );
    /* get a Wave file to open */
    if (!GetFileName( hDlg, TRUE,
                      szFileName,
                      sizeof(szFileName),
                      szFileTitle,
                      sizeof(szFileTitle) ))
    {
        return;
    }
    FreeGlobalWaveData( );                  // delete old waveform data
    /* read new waveform data */
    dwCurrentSample = 0;
    ReadWaveData( hDlg,
                  szFileName,
                  &lpWaveData,
                  &dwWaveDataSize);
 zoom = 1;
 /* set the new scroll bar range */
    iMaxPos = (int)(dwWaveDataSize) - dwright;
    ScrollBar_SetRange( hwndScrollBar, 0, iMaxPos, FALSE );
    /* set the new scroll bar position */
    FORWARD_WM_HSCROLL( hDlg, hwndScrollBar, 0, SB_TOP, SendMessage );
    return;
}
/*--------------------------------------------------------------------
        SAVE WAVE FILE
        Save waveform data to disk.  If the second parameter is TRUE,
        or if the current data does not yet have a file name, this
        procedure will request a name.
  --------------------------------------------------------------------*/
static void SaveWaveFile (
    HWND hDlg,
    BOOL bAskForName )
{
    HINSTANCE hInstance;
    BOOL bSave;
    char szText[MAX_RSRC_STRING_LEN];
    char szCaption[MAX_RSRC_STRING_LEN];
    int  iRet;
    /* anything to save? */
    if (!lpWaveData)
    {
        return;
    }
    /* if renaming or no name, prompt user for name */
    if ((bAskForName) || (szFileName[0] == '\0'))
    {
        /* get the name of the wavefile to save as */
        bSave = GetFileName( hDlg, FALSE,
                             szFileName,  sizeof(szFileName),
                             szFileTitle, sizeof(szFileTitle) );
    }
    else
    {
        /* no new name; confirm overwriting old file */
        hInstance = GetWindowInstance( hDlg );
        LoadString( hInstance, IDS_OVERWRITE, szText,
                    sizeof(szText) );
        LoadString( hInstance, IDS_CAPTION, szCaption,
                    sizeof(szCaption) );
        iRet = MessageBox( hDlg, szText, szCaption,
                    MB_YESNO | MB_ICONQUESTION );
        bSave = (iRet == IDYES);
    }
    /* save to the Wave file */
    if (bSave)
    {
        /* write out the waveform data */
        WriteWaveData( hDlg, szFileName,
                       lpWaveData,
                       dwWaveDataSize);
    }
    return;
}
/*--------------------------------------------------------------------
        RECORD WAVE FILE
        Record waveform data
  --------------------------------------------------------------------*/
static void CheckData ( HWND hDlg )
{
    LPSTR lp;                       // points to one sample in wave data
 DWORD i;
 BYTE  data,odata;
 DWORD ct;
 lp = lpWaveData;
 odata = (BYTE)(*lp);
 for (i=0;i<dwWaveDataSize;i++)
 {
  data = (BYTE)(*lp++);
  if (data == odata) ct++;
  else
  {
  odata = data;
  if (ct <= 2)
  {
            DialogBox( GetWindowInstance(hDlg),
                MAKEINTRESOURCE(DLG_SLOW),
                hDlg, Common_DlgProc );
   return;
  }
  else ct = 1;
  }
 }
}
static void CloseRecord( HWND hDlg )
{
    int  iMaxPos;
    HWND hwndScrollBar = GetDlgItem( hDlg, IDD_SCROLLBAR );
    /* set the new scroll bar range */
    iMaxPos = (int)(dwWaveDataSize) - dwright;
    ScrollBar_SetRange( hwndScrollBar, 0, iMaxPos, FALSE );
    /* set the new scroll bar position */
    FORWARD_WM_HSCROLL( hDlg, hwndScrollBar, 0, SB_TOP, SendMessage );
    return;
}
static void RecordWaveFile ( HWND hDlg )
{
    LPSTR lp;                       // points to one sample in wave data
 DWORD i;
 BYTE  data;
    /* record waveform data into a new file */
 dwWaveDataSize = dwbufsize;
    lpWaveData = GlobalAllocPtr( GMEM_MOVEABLE, dwWaveDataSize );
 lp = lpWaveData;
 breakit = FALSE;
 if (speed == 0)
 {
  if (port == 1)
  {
   if (btrig != 0)  // trigger sequence
   {
    data = btrig & (BYTE)_inp(rxport1);
    for (i=0;i<3000000;i++)
    {
     if (data != (btrig & (BYTE)_inp(rxport1))) break;
    }
   }
   _asm cli;
   for (i=0;i<dwWaveDataSize;i++)
   {
       *lp++ = (BYTE)_inp(rxport1);
   }
   _asm sti;
  }
  if (port == 2)
  {
   if (btrig != 0)  // trigger sequence
   {
    data = btrig & (BYTE)_inp(rxport2);
    for (i=0;i<3000000;i++)
    {
     if (data != (btrig & (BYTE)_inp(rxport2))) break;
    }
   }
   _asm cli;
   for (i=0;i<dwWaveDataSize;i++)
   {
       *lp++ = (BYTE)_inp(rxport2);
   }
   _asm sti;
  }
 }
 if (speed > 0)
 {
  params.speed = speed;
  SetEvent(hEvent);
  return;
 }
 zoom = 1;
 CloseRecord( hDlg );
    /* paint the waveform data */
    PaintWaveData( hDlg );
    CheckData( hDlg );
 return;
}
static void Trigger ( HWND hDlg )
{
    DWORD i;
    LPSTR lp;
 BYTE  data,odata;
    HWND hwndScrollBar = GetDlgItem( hDlg, IDD_SCROLLBAR );
 lp = lpWaveData + dwCurrentSample;
 odata = (BYTE)(*lp) & trig;
 for (i=dwCurrentSample;i<dwWaveDataSize;i++)
 {
  data = (BYTE)(*lp++) & trig;
  if (data != odata)
  {
   dwCurrentSample = max(i - (10 / zoom), 0);
   break;
  }
  odata = data;
 }
    /* update scroll bar thumb */
 iHScrollPos = (int)(dwCurrentSample);
 ScrollBar_SetPos( hwndScrollBar, iHScrollPos, TRUE );
    PaintWaveData( hDlg );
 return;
}
static void Zoom ( HWND hDlg, BOOL out )
{
 if (out) { if (zoom > 1)  zoom = zoom / 2; }
 else     { if (zoom < 15) zoom = zoom * 2; }
 PaintWaveData( hDlg );
}
static void Port ( HMENU menu , int port )
{
 CheckMenuItem(menu, ID_SETTINGS_PORT_LPT1, MF_UNCHECKED);
 CheckMenuItem(menu, ID_SETTINGS_PORT_LPT2, MF_UNCHECKED);
 CheckMenuItem(menu, ID_SETTINGS_PORT_LPT3, MF_UNCHECKED);
 CheckMenuItem(menu, port, MF_CHECKED);
}
static void SetSpeed ( HMENU menu , int port )
{
 CheckMenuItem(menu, ID_BUFFER_SPEED_FASTEST, MF_UNCHECKED);
 CheckMenuItem(menu, ID_BUFFER_SPEED_FAST   , MF_UNCHECKED);
 CheckMenuItem(menu, ID_BUFFER_SPEED_SLOW   , MF_UNCHECKED);
 CheckMenuItem(menu, port, MF_CHECKED);
}
static void SetSize ( HMENU menu , int port )
{
 CheckMenuItem(menu, ID_BUFFER_SIZE_5000  , MF_UNCHECKED);
 CheckMenuItem(menu, ID_BUFFER_SIZE_30000 , MF_UNCHECKED);
 CheckMenuItem(menu, ID_BUFFER_SIZE_100000, MF_UNCHECKED);
 CheckMenuItem(menu, port, MF_CHECKED);
}
static void NotImplemented ( HWND hDlg )
{
    DialogBox( GetWindowInstance(hDlg),
               MAKEINTRESOURCE(DLG_NOTYET),
               hDlg,
      Common_DlgProc );
}
static void MemTrig ( BYTE num )
{
 if (trig & num) trig = trig & ( 0xff ^ num );
    else trig = trig | num;
}
static void PortTrig( BYTE num )
{
 if (btrig & num) btrig = btrig & ( 0xff ^ num );
    else btrig = btrig | num;
}

Dopo aver letto questo codice ho raggiunto i piu´ alti gradi di pazzia.
"Tutto molto bello" direbbe Bruno Pizzul