I have a problem I'm trying to write to a list box but everytime I do the program states :

Unhandled exception at 0x5c560a97 (msvcr90d.dll) in Dialog_Box.exe: 0xC0000005: Access violation reading location 0x00070000.

when I take out SetWindowText(); the program is fine. here is a copy of my code:

#include <windows.h>
#include "Resource.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

//---------------------------------------------------------------------------
wchar_t *ComboBoxItems = L"Time not Set";	//holds the text
wchar_t *ComboBoxItems1 = L"None";	//holds the text
wchar_t *ComboBoxItems2 = L"None";	//holds the text
wchar_t *ComboBoxItems3 = L"None";	//holds the text
string Combo;
string temp;
HWND Handle = NULL;	// Specify a handle as null
HWND hTimer;
HWND hWnd;	//specify another handle this will be used in the dialog box
LRESULT CALLBACK DlgProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam); //Function
//which handles requests for the dialog box
ifstream file;
int input_position = 0;
//---------------------------------------------------------------------------
INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
				   LPSTR lpCmdLine, int nCmdShow)
{
	ofstream file2("Gui_display.txt");
	file2<<"No Class"<<endl;
	file2<<"No Class"<<endl;
	file2<<"No Message"<<endl;
	file2.close();

	
	DialogBox(hInstance, MAKEINTRESOURCE(IDD_DLGFIRST),
	          hWnd, reinterpret_cast<DLGPROC>(DlgProc));
	
	

	return FALSE;
}
//---------------------------------------------------------------------------
LRESULT CALLBACK DlgProc(HWND hWndDlg, UINT Msg, WPARAM wParam, LPARAM lParam)
{
	
	SetTimer(hWndDlg, IDC_CHECKTIME, 2000, NULL);
	HWND hWndPrincipal;
	HWND hWndPrincipal2;
	HWND hWndPrincipal3;
	HWND hWndPrincipal4;
	hWndPrincipal = GetDlgItem(hWndDlg, IDC_EDIT1);
	hWndPrincipal2 = GetDlgItem(hWndDlg, IDC_EDIT2);
	hWndPrincipal3 = GetDlgItem(hWndDlg, IDC_EDIT3);
	hWndPrincipal4 = GetDlgItem(hWndDlg, IDC_EDIT5);
	string class1;
	string class2;
	string message;

	switch(Msg)
	{
	case WM_INITDIALOG:
		
		SetWindowText(hWndPrincipal, L"No Time");
		SetWindowText(hWndPrincipal2, L"No Class");
		SetWindowText(hWndPrincipal3, L"No Class");
		SetWindowText(hWndPrincipal4, L"No Message");
		return TRUE;


	case WM_COMMAND:
		switch(wParam)
		{
		case IDQUIT:
			EndDialog(hWndDlg, 0);
			return TRUE;

		case IDTK:
			ShellExecute(Handle,L"open",L"Time_keeper.exe",
				NULL,NULL,SW_SHOWDEFAULT);
			return TRUE;

		case IDOCS:
			ShellExecute(Handle,L"open",L"input.txt",
				NULL,NULL,SW_SHOWDEFAULT);
			return TRUE;

		case IDOML:
			ShellExecute(Handle,L"open",L"command.txt",
				NULL,NULL,SW_SHOWDEFAULT);
			return TRUE;

		}

	case WM_TIMER:
		file.open("Gui_display.txt");
		input_position = file.tellg();	
		file.close();
		file.open("Gui_display.txt");
		file.seekg(input_position);
		getline(file, class1);
		getline(file, class2);
		getline(file, message);
		file.close();
		//hWndPrincipal = GetDlgItem(hWndDlg, IDC_EDIT1);
		//hWndPrincipal2 = GetDlgItem(hWndDlg, IDC_EDIT2);
		//hWndPrincipal3 = GetDlgItem(hWndDlg, IDC_EDIT3);
		//hWndPrincipal4 = GetDlgItem(hWndDlg, IDC_EDIT5);
		[B][U]SetWindowText(hWndPrincipal, L"Hello");[/U][/B]
		return TRUE;

		break;
	}

	return FALSE;
}
//---------------------------------------------------------------------------

Recommended Answers

All 9 Replies

I should also mention that I'm taking information from a text file and displaying them in the textbox. I use a timer to update every 2 sec.
any help would be appreciated
chris

Maybe you should try this:

Make the following Window Handles Global:

HWND hWndPrincipal;
HWND hWndPrincipal2;
HWND hWndPrincipal3;
HWND hWndPrincipal4;

I'm not sure if this will work, but I made a program that is similar to what you are trying to do, and making my handles global resources sorted out many problems that I had.

I tried making the handles global and it still crashes, just now at a different place. It now takes me to some assembly code.

Any other suggestions will be greatly appreciated,
Chris

I tried making the handles global and it still crashes, just now at a different place. It now takes me to some assembly code.

Any other suggestions will be greatly appreciated,
Chris

Could you provide us with your complete program code, so that we can compile and debug it?

here is my main file

/*
//This is the graphical interface for the time keeper
//Author: Christopher Surage
//Date: Tuesday May 12, 2009
Initial Revision: May 14, 2009
*/
#include <windows.h>
#include "Resource.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

//---------------------------------------------------------------------------
UINT TimmerID = 0;
LRESULT message = 0;
string temp;
HWND Handle = NULL;	// Specify a handle as null
HWND hTimer;
HWND hWnd;	//specify another handle this will be used in the dialog box
LRESULT CALLBACK DlgProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam); //Function
//which handles requests for the dialog box
ifstream file;
int input_position = 0;
HWND hWndPrincipal;
HWND hWndPrincipal2;
HWND hWndPrincipal3;
HWND hWndPrincipal4;
//---------------------------------------------------------------------------
INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
				   LPSTR lpCmdLine, int nCmdShow)
{
	ofstream file2("Gui_display.txt");
	file2<<"No Class"<<endl;
	file2<<"No Class"<<endl;
	file2<<"No Message"<<endl;
	file2.close();

	
	DialogBox(hInstance, MAKEINTRESOURCE(IDD_DLGFIRST),
	          hWnd, reinterpret_cast<DLGPROC>(DlgProc));
	
	

	return FALSE;
}
//---------------------------------------------------------------------------
LRESULT CALLBACK DlgProc(HWND hWndDlg, UINT Msg, WPARAM wParam, LPARAM lParam)
{
	
	TimmerID = SetTimer(hWndDlg, NULL, 10000, NULL);
	hWndPrincipal = GetDlgItem(hWndDlg, IDC_EDIT1);
	hWndPrincipal2 = GetDlgItem(hWndDlg, IDC_EDIT2);
	hWndPrincipal3 = GetDlgItem(hWndDlg, IDC_EDIT3);
	hWndPrincipal4 = GetDlgItem(hWndDlg, IDC_EDIT5);
	string class1;
	string class2;
	string message;
	//SendMessage(hWndDlg, WM_COMMAND, 

	switch(Msg)
	{
	case WM_INITDIALOG:
		
		SetWindowText(hWndPrincipal, L"No Time");
		SetWindowText(hWndPrincipal2, L"No Class");
		SetWindowText(hWndPrincipal3, L"No Class");
		SetWindowText(hWndPrincipal4, L"No Message");
		return TRUE;


	case WM_COMMAND:
		switch(wParam)
		{
		case IDQUIT:
			EndDialog(hWndDlg, 0);
			return TRUE;

		case IDTK:
			ShellExecute(Handle,L"open",L"Time_keeper.exe",
				NULL,NULL,SW_SHOWDEFAULT);
			return TRUE;

		case IDOCS:
			ShellExecute(Handle,L"open",L"input.txt",
				NULL,NULL,SW_SHOWDEFAULT);
			return TRUE;

		case IDOML:
				ShellExecute(Handle,L"open",L"command.txt",
				NULL,NULL,SW_SHOWDEFAULT);
				hWndPrincipal2 = GetDlgItem(hWndDlg, IDC_EDIT2);
				SetWindowText(GetDlgItem(hWndDlg, IDC_EDIT2), L"Hello");
			return TRUE;

		}

	case WM_TIMER
      SetWindowText(hWndPrincipal2, L"Hello");
       return TRUE;

	default:
		break;
	}

	return FALSE;
}
//---------------------------------------------------------------------------

here is my resource file:

// Microsoft Visual C++ generated resource script.
//
#include "resource.h"

#define APSTUDIO_READONLY_SYMBOLS
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 2 resource.
//
#include "afxres.h"

/////////////////////////////////////////////////////////////////////////////
#undef APSTUDIO_READONLY_SYMBOLS

/////////////////////////////////////////////////////////////////////////////
// English (U.S.) resources

#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU)
#ifdef _WIN32
LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US
#pragma code_page(1252)
#endif //_WIN32

#ifdef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// TEXTINCLUDE
//

1 TEXTINCLUDE 
BEGIN
    "resource.h\0"
END

2 TEXTINCLUDE 
BEGIN
    "#include ""afxres.h""\r\n"
    "\0"
END

3 TEXTINCLUDE 
BEGIN
    "\r\n"
    "\0"
END

#endif    // APSTUDIO_INVOKED



/////////////////////////////////////////////////////////////////////////////
//
// Dialog
//

IDD_DLGFIRST DIALOGEX 260, 200, 316, 186
STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "Timekeeper Interface"
FONT 8, "MS Shell Dlg", 400, 0, 0x1
BEGIN
    PUSHBUTTON      "Quit",IDQUIT,259,165,50,14
    DEFPUSHBUTTON   "Run Timekeeper",IDTK,232,30,77,16
    DEFPUSHBUTTON   "Open Message List",IDOML,232,79,77,16
    DEFPUSHBUTTON   "Open Class Schedule",IDOCS,232,55,77,16
    EDITTEXT        IDC_EDIT1,21,36,145,15,ES_AUTOHSCROLL
    EDITTEXT        IDC_EDIT2,21,68,145,14,ES_AUTOHSCROLL
    EDITTEXT        IDC_EDIT3,22,97,144,14,ES_AUTOHSCROLL
    EDITTEXT        IDC_EDIT5,22,126,144,14,ES_AUTOHSCROLL
    CONTROL         "Radio1",IDC_RADIO1,"Button",BS_AUTORADIOBUTTON,233,103,38,10
END


/////////////////////////////////////////////////////////////////////////////
//
// DESIGNINFO
//

#ifdef APSTUDIO_INVOKED
GUIDELINES DESIGNINFO 
BEGIN
    IDD_DLGFIRST, DIALOG
    BEGIN
        LEFTMARGIN, 7
        RIGHTMARGIN, 309
        TOPMARGIN, 7
        BOTTOMMARGIN, 179
    END
END
#endif    // APSTUDIO_INVOKED

#endif    // English (U.S.) resources
/////////////////////////////////////////////////////////////////////////////



#ifndef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 3 resource.
//


/////////////////////////////////////////////////////////////////////////////
#endif    // not APSTUDIO_INVOKED

Here is my header:

//{{NO_DEPENDENCIES}}
// Microsoft Visual C++ generated include file.
// Used by Dialog_Box.rc
//
#define IDTK                            3
#define IDOK3                           4
#define IDOML                           4
#define IDOCS                           5
#define IDD_DLGFIRST                    101
#define IDC_CHECKTIME                   102
#define IDC_EDIT1                       1002
#define IDC_EDIT2                       1003
#define IDC_EDIT3                       1004
#define IDC_EDIT5                       1005
#define IDQUIT                          1006
#define IDC_COMBO1                      1008
#define IDC_RADIO1                      1009

// Next default values for new objects
// 
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE        102
#define _APS_NEXT_COMMAND_VALUE         40001
#define _APS_NEXT_CONTROL_VALUE         1010
#define _APS_NEXT_SYMED_VALUE           101
#endif
#endif

I used visual c++ 2008 to write the code:

Hi!

I compiled your program, and it compiled successfully, but I never got the GUI part of it to work. I made some modifications to your code, and now its running, but I've no idea if it will work(function in the desired way) as I do not have "Time_keeper.exe". Anyways, just give this a try...

/*
//This is the graphical interface for the time keeper
//Author: Christopher Surage
//Date: Tuesday May 12, 2009
Initial Revision: May 14, 2009
*/
#include <windows.h>
#include "Resource.h"
#include <iostream>
#include <fstream>
#include <string.h>
using namespace std;

//---------------------------------------------------------------------------
UINT TimmerID = 0;
LRESULT message = 0;
string temp;
HWND Handle = NULL; // Specify a handle as null
HWND hTimer;
HWND hWnd; //specify another handle this will be used in the dialog box
LRESULT CALLBACK DlgProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam); //Function
//which handles requests for the dialog box
ifstream file;
int input_position = 0;
HWND hWndPrincipal;
HWND hWndPrincipal2;
HWND hWndPrincipal3;
HWND hWndPrincipal4;

//---------------------------------------------------------------------------
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
      ofstream file2("Gui_display.txt");
      file2<<"No Class"<<endl;
      file2<<"No Class"<<endl;
      file2<<"No Message"<<endl;
      file2.close();
      
      DialogBox(hInstance, MAKEINTRESOURCE(IDD_DLGFIRST), hWnd, reinterpret_cast<DLGPROC>(DlgProc));
      
      return EXIT_SUCCESS;
}

//---------------------------------------------------------------------------
LRESULT CALLBACK DlgProc(HWND hWndDlg, UINT Msg, WPARAM wParam, LPARAM lParam)
{
        TimmerID = SetTimer(hWndDlg, NULL, 10000, NULL);
        hWndPrincipal = GetDlgItem(hWndDlg, IDC_EDIT1);
        hWndPrincipal2 = GetDlgItem(hWndDlg, IDC_EDIT2);
        hWndPrincipal3 = GetDlgItem(hWndDlg, IDC_EDIT3);
        hWndPrincipal4 = GetDlgItem(hWndDlg, IDC_EDIT5);
        string class1;
        string class2;
        string message;
        //SendMessage(hWndDlg, WM_COMMAND,
        
        switch(Msg)
        {
             case WM_INITDIALOG:
             {
                  SetWindowText(hWndPrincipal, "No Time");
                  SetWindowText(hWndPrincipal2, "No Class");
                  SetWindowText(hWndPrincipal3, "No Class");
                  SetWindowText(hWndPrincipal4, "No Message");
             }
             break;
             
             case WM_COMMAND:
             {
                  switch(wParam)
                  {
                       case IDQUIT:
                       {
                            EndDialog(hWndDlg, 0);
                            return TRUE;
                       }
                       
                       case IDTK:
                       {
                            ShellExecute(Handle,"open","Time_keeper.exe", NULL, NULL, SW_SHOWDEFAULT);
                       }
                       break;
                       
                       case IDOCS:
                       {
                            ShellExecute(Handle,"open","input.txt",
                            NULL,NULL,SW_SHOWDEFAULT);
                       }
                       break;
                       
                       case IDOML:
                       {
                            ShellExecute(Handle,"open", "command.txt",
                            NULL,NULL,SW_SHOWDEFAULT);
                            
                            hWndPrincipal2 = GetDlgItem(hWndDlg, IDC_EDIT2);
                            
                            SetWindowText(GetDlgItem(hWndDlg, IDC_EDIT2), "Hello");
                       }
                  }
             }
             break;
             
             case WM_TIMER:
             {
                  SetWindowText(hWndPrincipal2, "Hello");
             }
             break;
                  
             default: return FALSE;
        }
             
        return TRUE;
}
//---------------------------------------------------------------------------

*Note*: I haven't made any changes to the supplementary files.

I would also like to mention that from the memory side of things, and from the program logic, you might like to use:

BOOL CALLBACK DlgProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam); //Function
//which handles requests for the dialog box
.
.
.
BOOL CALLBACK DlgProc(HWND hWndDlg, UINT Msg, WPARAM wParam, LPARAM lParam)
{ ... }

instead of:

LRESULT CALLBACK DlgProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam); //Function
//which handles requests for the dialog box
.
.
.
LRESULT CALLBACK DlgProc(HWND hWndDlg, UINT Msg, WPARAM wParam, LPARAM lParam)
{ ... }

LRESULT is a mnemonic of the data type long. In the code, this function in question returns a boolean flag (TRUE or FALSE), so it really doesn't make sense to define it as such.

Cheers!

Thank you so much for your help. GUI works fine, was it the brackets that I missed?

Thank you so much for your help. GUI works fine, was it the brackets that I missed?

Hi!

Glad it helped! :) and yes, it was the braces which was the main problem. Just remember to put braces even for the simplest of all switch(case) statements, especially if you are going to nest it with further switch(case) statements...

There was also a minor return statement mix-up, but that wouldn't have mattered much(you would have caught it later, at the debugging stage)...

Cheers!

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.