I am sort of new to Win32 API programming with C++ and I am having an issue trying to save what is in the textboxes to a file when you hit the close button. I am using the WriteFile() and CreateFile() functions. Everytime I try to do it I get errors like it is missing something.

main.cpp

#include <windows.h>
#include <ctime>
#include <string>
#include "resource.h"

static bool keepRunning = true;
static HANDLE hThread = NULL;
static void run();
static void end();
static DWORD WINAPI ThreadTime(LPVOID lpParam);
static bool keepRunning1 = true;
static void breakrun();
static void breakend();
static DWORD WINAPI ThreadBreak(LPVOID lpParam);
static bool keepRunning2 = true;
static void lunchrun();
static void lunchend();
static DWORD WINAPI ThreadLunch(LPVOID lpParam);

LPCTSTR Caption = "Time for Aux!"; // Set the Caption for all Msg Boxes

// Declare Variables as Global Variables
char strCurrentTime[20], strMeetingTime[20], strStartTime[20], 
      strBreak1Start[20], strLunch[20], strBreak2Start[20], strLeaveTime[20], 
      strLateBreak[20], strTime[20]; 

HWND hWndCurrentTime, hWndMeetingTime, hWndStartTime, hWndBreak1, hWndLunch, 
       hWndBreak2, hWndLeaveTime, hWndLateBreak;

HWND hWnd;
HINSTANCE hInst;
LRESULT CALLBACK DlgProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);

int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                       LPSTR lpCmdLine, int nCmdShow)
{
  DialogBox(hInstance, MAKEINTRESOURCE(IDD_CONTROLS_DLG), hWnd,
              reinterpret_cast<DLGPROC>(DlgProc));

  hInst = hInstance;

  return 0;
}

LRESULT CALLBACK DlgProc(HWND hWndDlg, UINT Msg, WPARAM wParam, LPARAM lParam)
{
  hWndCurrentTime = GetDlgItem(hWndDlg, IDC_CURRENT_TIME);
  hWndMeetingTime = GetDlgItem(hWndDlg, IDC_MEETING_TIME);
  hWndStartTime   = GetDlgItem(hWndDlg, IDC_START_TIME);
  hWndBreak1      = GetDlgItem(hWndDlg, IDC_BREAK1);
  hWndLunch       = GetDlgItem(hWndDlg, IDC_LUNCH);
  hWndBreak2      = GetDlgItem(hWndDlg, IDC_BREAK2);
  hWndLeaveTime   = GetDlgItem(hWndDlg, IDC_LEAVE_TIME);
  hWndLateBreak   = GetDlgItem(hWndDlg, IDC_LATE_BREAK_BTN);

  switch(Msg)
  {
    case WM_INITDIALOG: // What loads when app starts
      run();  // start thread for showing current time
      SetWindowText(hWndMeetingTime, "");
      SetWindowText(hWndStartTime, "");
      SetWindowText(hWndBreak1, "");
      SetWindowText(hWndLunch, "");
      SetWindowText(hWndBreak2, "");
      SetWindowText(hWndLeaveTime, "");
    return TRUE;

    case WM_COMMAND:
      switch(wParam)
      {
        case IDC_LATE_BREAK_BTN: // Start a 15 minute timer
          Sleep(900000);
          MessageBox(NULL, "Time to return from your late break.", Caption, MB_OK);
        return TRUE;

        case IDCANCEL: // Quit button
          end();
          CreateFile("/Dat Files/StartTime.dat", GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
          WriteFile("/Dat Files/StartTime.dat", strStartTime, 20, 20, NULL);
          CreateFile("/Dat Files/Break1Start.dat", GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
          WriteFile("/Dat Files/Break1Start.dat", strBreak1Start, 20, 20, NULL);
          CreateFile("/Dat Files/Lunch.dat", GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
          WriteFile("/Dat Files/Lunch.dat", strLunch, 20, 20, NULL);
          CreateFile("/Dat Files/Break2Time.dat", GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
          WriteFile("/Dat Files/Break2Time.dat", strBreak2Start, 20, 20, NULL);
          CreateFile("/Dat Files/LeaveTime.dat", GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
          WriteFile("/Dat Files/LeaveTime.dat", strLeaveTime, 20, 20, NULL);


          EndDialog(hWndDlg, 0);
        return TRUE;
      }
    break;
  }

  return FALSE;
}
// Current Time Thread
static void run()
{
  DWORD dummy;
  hThread = CreateThread(NULL, 0, ThreadTime, NULL, 0, &dummy);
}

static void end()
{
  keepRunning = false;
}

static DWORD WINAPI ThreadTime(LPVOID lpParam)
{
  while(keepRunning)
  {
    time_t rawtime;
    struct tm * timeinfo;
    time( &rawtime);
    timeinfo = localtime ( &rawtime);
    strftime(strCurrentTime,20,"%I:%M:%S %p",timeinfo);
    SetWindowText(hWndCurrentTime, strCurrentTime);
    Sleep(1000);
    GetWindowText(hWndMeetingTime, strMeetingTime, 20);
    GetWindowText(hWndStartTime, strStartTime, 20);
    GetWindowText(hWndBreak1, strBreak1Start, 20);
    GetWindowText(hWndLunch, strLunch, 20);    
    GetWindowText(hWndBreak2, strBreak2Start, 20);
    GetWindowText(hWndLeaveTime, strLeaveTime, 20);
    if (strcmp(strStartTime, strCurrentTime) == 0)
    {
      MessageBox(NULL, "Time to start your day.", Caption, MB_OK);
    }
    if (strcmp(strMeetingTime, strCurrentTime) == 0)
    {
      MessageBox(NULL, "Time to go to your meeting.", Caption, MB_OK);
    }
    if (strcmp(strBreak1Start, strCurrentTime) == 0)
    {
      MessageBox(NULL, "Time to start your break..", Caption, MB_OK);
      breakrun();
    }
    if (strcmp(strLunch, strCurrentTime) == 0)
    {
      MessageBox(NULL, "Time to start your lunch.", Caption, MB_OK);
      lunchrun();
    }
    if (strcmp(strBreak2Start, strCurrentTime) == 0)
    {
      MessageBox(NULL, "Time to go to your last break.", Caption, MB_OK);
      breakrun();
    }
    if (strcmp(strLeaveTime, strCurrentTime) == 0)
    {
      MessageBox(NULL, "Time to ACW and get ready to go home.", Caption, MB_OK);
    }
  }
  return 0;
}

// Break Thread
static void breakrun()
{
  DWORD dummy;
  hThread = CreateThread(NULL, 0, ThreadBreak, NULL, 0, &dummy);
}

static void breakend()
{
  keepRunning1 = false;
}

static DWORD WINAPI ThreadBreak(LPVOID lpParam)
{
  while(keepRunning1)
  {
    Sleep(900000);
    MessageBox(NULL, "Time to login from break.", Caption, MB_OK);
    breakend();
  }
  return 0;
}

// Lunch Thread
static void lunchrun()
{
  DWORD dummy;
  hThread = CreateThread(NULL, 0, ThreadLunch, NULL, 0, &dummy);
}

static void lunchend()
{
  keepRunning2 = false;
}

static DWORD WINAPI ThreadLunch(LPVOID lpParam)
{
  while(keepRunning1)
  {
    Sleep(1800000);
    MessageBox(NULL, "Time to login from lunch.", Caption, MB_OK);
    lunchend();
  }
  return 0;
}

Recommended Answers

All 2 Replies

Have you ever seen Windows API file access function sugnatures?

HANDLE CreateFile(
    LPCTSTR  lpFileName,	// address of name of the file 
    DWORD  dwDesiredAccess,	// access (read-write) mode 
    DWORD  dwShareMode,	// share mode 
    LPSECURITY_ATTRIBUTES  lpSecurityAttributes,	// address of security descriptor 
    DWORD  dwCreationDistribution,	// how to create 
    DWORD  dwFlagsAndAttributes,	// file attributes 
    HANDLE  hTemplateFile 	// handle of file with attributes to copy  
   );
BOOL WriteFile(
    HANDLE  hFile,	// handle of file to write to 
    LPCVOID  lpBuffer,	// address of data to write to file 
    DWORD  nNumberOfBytesToWrite,	// number of bytes to write 
    LPDWORD  lpNumberOfBytesWritten,	// address of number of bytes written 
    LPOVERLAPPED  lpOverlapped 	// addr. of structure needed for overlapped I/O  
   );
BOOL CloseHandle(
    HANDLE  hObject 	// handle of object to close  
   );

Besides that you must check up i/o function return codes, for example:

HANDLE fh;
fh = CreateFile(("\\Dat Files\\StartTime.dat",...);
if (fh == INVALID_HANDLE_VALUE) {
    // open failed
}
...
if (WriteFile(fh,...) == FALSE) { // use file handle
    // read failed
}
...
// don't forget to close opened file handle!
CloseHandle(fh);

So with what I have and knowing the filenames how can I use that syntax and where would I put it.. and instead of using multiple files can i usew just one dat file and read the saved times in from that?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.