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;
}