Win32 API and VC++ Edit Boxes

  • (7 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »

98 Replies - 7299 Views - Last Post: 23 July 2012 - 10:20 AM Rate Topic: -----

#1 nullcoding  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 99
  • Joined: 08-July 12

Win32 API and VC++ Edit Boxes

Posted 13 July 2012 - 12:58 PM

I couldn't think of a better title, but since what I'm trying to do primarily involves edit boxes, it seemed logical.

Consider this:

Posted Image

I wrote this using Win32 API in Visual Studio 2010 Professional.

I have written a working console application and wish to "convert" it to run with a frontend. It is actually a lot simpler than it sounds, considering it is not a complicated program. It asks for a number k, a number n, and computes the result using a formula. It tests it to see if it's prime or composite, and tells you which.

The only thing that involves the GUI is input and output. Because I use MPIR, I cannot have the GUI output the calculated result, as it would not be of type uint but rather mpz_t.

I have gotten stuck, though.

For testing purposes (that is, familiarizing myself with the language a bit more), I programmatically created the boxes and buttons and am trying to simply take the input of k and display it in another box - just so I know how to do it.

Well, I can't.

Here is code I am trying to use:

	case WM_COMMAND:

		wmId    = LOWORD(wParam);
		wmEvent = HIWORD(wParam);

			switch(wParam)
		{
		case IDC_BUTTON1: // when Accept k is pressed

			if(HIWORD(wParam) == BN_CLICKED) // when it's clicked, store input as k
			{
				unsigned long int kinput = GetDlgItemInt(hEdit, IDC_ENTER_K, NULL, TRUE);
				mpz_t k; 
				mpz_set_ui ( k, kinput); //sets mpz_t k from ul kinput
				SetDlgItemInt(hEdit3, IDC_PROTH, kinput, TRUE);
			}

			MessageBox(NULL, L"Value of k Accepted.", L"k Accepted", MB_ICONINFORMATION);
			return TRUE;



And so I run debug mode, enter a k, and click the button. Instead of doing what I want it to, I see this:

Quote

First-chance exception at 0x009834cc in gjs15x64_GUI.exe: 0xC0000005: Access violation writing location 0xcccccccc.


This leads me to believe some sort of memory object is either not initialized or not present.

I was poring over MSDN for hours today and never saw anything along those lines.

Can someone help me out here? Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Win32 API and VC++ Edit Boxes

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3589
  • View blog
  • Posts: 11,161
  • Joined: 05-May 12

Re: Win32 API and VC++ Edit Boxes

Posted 13 July 2012 - 01:11 PM

You should have gotten a compiler warning about line 15. Are you just ignoring those?

Scratch that. I was being dyslexic. I thought I read k being passed in on line 15.

With a debugger attached, is the crash on line 12 or line 15?

This post has been edited by Skydiver: 13 July 2012 - 01:51 PM

Was This Post Helpful? 0
  • +
  • -

#3 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3589
  • View blog
  • Posts: 11,161
  • Joined: 05-May 12

Re: Win32 API and VC++ Edit Boxes

Posted 13 July 2012 - 01:56 PM

This time, I'm not being dyslexic. I see on lines 12 and 15 that you are passing in hEdit and hEdit3 as the first parameter. If your read the documentation for these API's, they are expecting to have the HWND of the dialog that contains hEdit and hEdit3.

And another thing: On line 12, you are declaring k as unsigned, yet you pass in TRUE to the API's saying that you want the text to be treated as signed.

This post has been edited by Skydiver: 13 July 2012 - 01:58 PM

Was This Post Helpful? 0
  • +
  • -

#4 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 802
  • View blog
  • Posts: 2,363
  • Joined: 20-March 10

Re: Win32 API and VC++ Edit Boxes

Posted 13 July 2012 - 02:08 PM

Right,

a few things hEdit and hEdit3 are likley to be HWND's

your switch statement

it should be switch LOWORD(wParam)

You actually set up a variable to do this wmId then don't use it..

What's up with that...?

you could just switch (wmId)

your writing to memory incorrectly somewhere your not allocating memory

Don't know where its not in that code... you posted....

Maybe this...


mpz_set_ui ( k, kinput); //sets mpz_t k from ul kinput


But dont really know.

Snoopy.
Was This Post Helpful? 2
  • +
  • -

#5 nullcoding  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 99
  • Joined: 08-July 12

Re: Win32 API and VC++ Edit Boxes

Posted 13 July 2012 - 03:37 PM

First - they are. hEdit, hEdit2, hEdit3 are all of type hWnd according to VS2010.

Second - thanks, changing it to LOWORD got rid of the Access Violation though it still won't put text in the box. I'm still getting used to this influx of new syntax and operators and functions and such.

Skydiver, thanks, I missed that. Now it's FALSE.

I have to admit I am not sure how to allocate memory - that is, if it's on a per-function basis or even more specific ie per string, integer etc.

In the past I have written programs that allocate memory during a calculation for (expected result + 1), but I would rather not do that here since I don't have 16GB of RAM. This program makes big numbers, you see.

I ONLY want to show k and n in those boxes. I want the third box to (eventually) read "k * 2 ^ n - 1" as there is no way (of which I know) to pass an mpz_t integer to an edit box that expects an int.

Is there anything specifically wrong with line 15 up there?

I'll work on using malloc then...since right now the way I've written it in is causing access violations again.

This post has been edited by GunnerInc: 13 July 2012 - 03:59 PM
Reason for edit:: Removed uneeded quote

Was This Post Helpful? 0
  • +
  • -

#6 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 858
  • View blog
  • Posts: 2,282
  • Joined: 28-March 11

Re: Win32 API and VC++ Edit Boxes

Posted 13 July 2012 - 03:47 PM

Quote

Is there anything specifically wrong with line 15 up there?

Yes there is. Look at the documentation for SetDlgItemInt

The first parameter is the handle to the controls parent NOT the control. Normal WinProcs this would be called hWin.

The cool thing about using SetDlgItem* is you do not need the handle to a control, you just need the handle to the controls parent and the ID of the control.

Also, we don't need to quote the post right above yours.
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3589
  • View blog
  • Posts: 11,161
  • Joined: 05-May 12

Re: Win32 API and VC++ Edit Boxes

Posted 13 July 2012 - 03:56 PM

There is no need to malloc() if you don't need it. Sometimes, a stack variable is good enough.

When you run your program under the debugger and it crashes, does the IDE take you to a particular line of code? Is it that chunk of code you pasted in your original post? If so, what line? If not, can you post the function where you are getting the crash and tell us which line it is crashing on?
Was This Post Helpful? 0
  • +
  • -

#8 nullcoding  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 99
  • Joined: 08-July 12

Re: Win32 API and VC++ Edit Boxes

Posted 13 July 2012 - 04:27 PM

This creates the box:

hEdit3=CreateWindowEx(WS_EX_CLIENTEDGE,
				L"EDIT",
				L"",
				WS_CHILD|WS_VISIBLE|
				ES_MULTILINE|ES_AUTOVSCROLL|ES_AUTOHSCROLL|ES_NUMBER,
				350,
				100,
				100,
				50,
				hWnd,
				(HMENU)IDC_PROTH,
				GetModuleHandle(NULL),
				NULL);
			SendMessage(hEdit3, 
				WM_SETFONT,		
				(WPARAM)hfDefault,
				MAKELPARAM(FALSE,0));


This is supposedly going to test the box by displaying the input into one box (edit1) in edit3. I changed it to the control's parent hWnd and am still getting the same error.

case WM_COMMAND:

		wmId    = LOWORD(wParam);
		wmEvent = HIWORD(wParam);

			switch(wParam)
		{
		case IDC_BUTTON1: // when Accept k is pressed
			{
				unsigned long int kinput = GetDlgItemInt(hEdit, IDC_ENTER_K, NULL, FALSE);
				mpz_t k; 
				mpz_set_ui ( k, kinput); //sets mpz_t k from ul kinput

			if(wmId == BN_CLICKED) // when it's clicked, store input as k
			{
				
				SetDlgItemInt(hWnd, IDC_PROTH, kinput, FALSE);
			}

			MessageBox(NULL, L"Value of k Accepted.", L"k Accepted", MB_ICONINFORMATION);
			}
			return TRUE;

Was This Post Helpful? 0
  • +
  • -

#9 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3589
  • View blog
  • Posts: 11,161
  • Joined: 05-May 12

Re: Win32 API and VC++ Edit Boxes

Posted 13 July 2012 - 04:29 PM

Line 10 should also pass in hWnd.

You are still getting the same error... tell us which line the debugger is reporting it on. Press the "Break" button on the dialog box that pops up reporting the first chance exception.

This post has been edited by Skydiver: 13 July 2012 - 04:30 PM

Was This Post Helpful? 0
  • +
  • -

#10 nullcoding  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 99
  • Joined: 08-July 12

Re: Win32 API and VC++ Edit Boxes

Posted 13 July 2012 - 04:34 PM

Dialog box?

It just says "First chance exception" etc in the Debug output in the bottom right.

For what it's worth, this is clearly a problem with clicking the button. Only if I click the button does it throw that exception.

The program keeps running, sure, but it's not like it's doing very much. I am trying to give it one simple instruction and it just doesn't like it.
Was This Post Helpful? 0
  • +
  • -

#11 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3589
  • View blog
  • Posts: 11,161
  • Joined: 05-May 12

Re: Win32 API and VC++ Edit Boxes

Posted 13 July 2012 - 04:38 PM

Go to Debug.Exceptions...
For now, check all the check boxes. You can turn them off later if any particular exception is thrown and handled.
Was This Post Helpful? 0
  • +
  • -

#12 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 858
  • View blog
  • Posts: 2,282
  • Joined: 28-March 11

Re: Win32 API and VC++ Edit Boxes

Posted 13 July 2012 - 04:45 PM

line 10: GetDlgItemInt(hEdit, IDC_ENTER_K, NULL, FALSE);

hEdit should be hWnd

API calls return values. You have to read the docs for the API calls to see what return value is an error and if you need to call GetLastError. By you using the wrong handle in the API calls, they would of returned NULL to show something was wrong, you then would of called GetLastError to get the exact error.
Was This Post Helpful? 0
  • +
  • -

#13 nullcoding  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 99
  • Joined: 08-July 12

Re: Win32 API and VC++ Edit Boxes

Posted 13 July 2012 - 05:14 PM

It is hWnd. I changed them all.

The mpz_t functions were causing the errors, so I moved them.

Now no access violations. Still doesn't do anything though. I suppose you want me try this:

case IDC_BUTTON1: // when Accept k is pressed
		{
			BOOL* write;
			
			if(wmId == BN_CLICKED) // when it's clicked, store input as k
			{				
				unsigned long int kinput = GetDlgItemInt(hWnd, IDC_ENTER_K, write, FALSE);
				SetDlgItemInt(hWnd, IDC_PROTH, kinput, FALSE);			
			
			if (*write == 1)
				{
				MessageBox(NULL, L"Value of k Accepted.", L"k Accepted", MB_ICONINFORMATION);
				}
			else
				{
				MessageBox(NULL, L"Error reading k!", L"Error!", MB_IConerror);
				}	
			}
		}
			return TRUE;


Now it's not doing anything. No errors, no throws, nothing at all.
Was This Post Helpful? 0
  • +
  • -

#14 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 858
  • View blog
  • Posts: 2,282
  • Joined: 28-March 11

Re: Win32 API and VC++ Edit Boxes

Posted 13 July 2012 - 05:16 PM

Post your Window Proc, bits and pieces are hard to go by
Was This Post Helpful? 0
  • +
  • -

#15 nullcoding  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 99
  • Joined: 08-July 12

Re: Win32 API and VC++ Edit Boxes

Posted 13 July 2012 - 05:20 PM

Sure. With my luck, the problem's in there somewhere.

LRESULT CALLBACK WinProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam)

{
	int wmId, wmEvent;
	//HANDLE kheap = HeapCreate (0, 0, 5000);
	//HeapAlloc (kheap, HEAP_ZERO_MEMORY, 1000);

	mpz_t k;					
	unsigned long int kinput;

	switch(msg)
	{
		case WM_CREATE:
		{
			// Create an edit box for k
			hEdit=CreateWindowEx(WS_EX_CLIENTEDGE,
				L"EDIT",
				L"",
				WS_CHILD|WS_VISIBLE|
				ES_MULTILINE|ES_AUTOVSCROLL|ES_AUTOHSCROLL|ES_NUMBER,
				50,
				100,
				100,
				50,
				hWnd,
				(HMENU)IDC_ENTER_K,
				GetModuleHandle(NULL),
				NULL);
			HGDIOBJ hfDefault=GetStockObject(DEFAULT_GUI_FONT);
			SendMessage(hEdit,
				WM_SETFONT,
				(WPARAM)hfDefault,
				MAKELPARAM(FALSE,0));

			// Create a push button to accept and store that k
			HWND hWndButton=CreateWindowEx(NULL,
				L"BUTTON",
				L"Accept k",
				WS_TABSTOP|WS_VISIBLE|
				WS_CHILD,
				50,
				150,
				100,
				24,
				hWnd,
				(HMENU)IDC_BUTTON1,
				GetModuleHandle(NULL),
				NULL);
			SendMessage(hWndButton,
				WM_SETFONT,
				(WPARAM)hfDefault,
				MAKELPARAM(FALSE,0));

			// Create an edit box for n
			hEdit2=CreateWindowEx(WS_EX_CLIENTEDGE,
				L"EDIT",
				L"",
				WS_CHILD|WS_VISIBLE|
				ES_MULTILINE|ES_AUTOVSCROLL|ES_AUTOHSCROLL|ES_NUMBER,
				150,
				100,
				100,
				50,
				hWnd,
				(HMENU)IDC_ENTER_N,
				GetModuleHandle(NULL),
				NULL);
			SendMessage(hEdit,
				WM_SETFONT,
				(WPARAM)hfDefault,
				MAKELPARAM(FALSE,0));

			// Create a push button to accept and store that n
			HWND hWndButton2=CreateWindowEx(NULL,
				L"BUTTON",
				L"Accept n",
				WS_TABSTOP|WS_VISIBLE|
				WS_CHILD,
				150,
				150,
				100,
				24,
				hWnd,
				(HMENU)IDC_BUTTON2,
				GetModuleHandle(NULL),
				NULL);
			SendMessage(hWndButton2,
				WM_SETFONT,
				(WPARAM)hfDefault,
				MAKELPARAM(FALSE,0));

			// Create a button that, when pressed, will calculate Proth and display it in the box 

			HWND hWndButton3=CreateWindowEx(NULL,
				L"BUTTON",
				L"Calculate Proth!",
				WS_TABSTOP|WS_VISIBLE|
				WS_CHILD,
				250,
				100,
				100,
				74,
				hWnd,
				(HMENU)IDC_BUTTON3,
				GetModuleHandle(NULL),
				NULL);
			SendMessage(hWndButton3,
				WM_SETFONT,
				(WPARAM)hfDefault,
				MAKELPARAM(FALSE,0));

			// Create a box that will eventually display the value of the Proth number!
			hEdit3=CreateWindowEx(WS_EX_CLIENTEDGE,
				L"EDIT",
				L"",
				WS_CHILD|WS_VISIBLE|
				ES_MULTILINE|ES_AUTOVSCROLL|ES_AUTOHSCROLL|ES_NUMBER,
				350,
				100,
				100,
				50,
				hWnd,
				(HMENU)IDC_PROTH,
				GetModuleHandle(NULL),
				NULL);
			SendMessage(hEdit3, // this is eventually where we will call this box to display the calculated value
				WM_SETFONT,		// of the Proth number.
				(WPARAM)hfDefault,
				MAKELPARAM(FALSE,0));

				// Create a push button to (eventually) test that number!
			HWND hWndButton4=CreateWindowEx(NULL,
				L"BUTTON",
				L"TEST!",
				WS_TABSTOP|WS_VISIBLE|
				WS_CHILD|BS_DEFPUSHBUTTON,
				350,
				150,
				100,
				24,
				hWnd,
				(HMENU)IDC_TESTB,
				GetModuleHandle(NULL),
				NULL);
			SendMessage(hWndButton4,
				WM_SETFONT,
				(WPARAM)hfDefault,
				MAKELPARAM(FALSE,0));
		}
		break;

		case WM_COMMAND:

		wmId    = LOWORD(wParam);
		wmEvent = HIWORD(wParam);

			switch(wParam)
		{
		case IDC_BUTTON1: // when Accept k is pressed
		{
			BOOL* write;
			
			if(wmId == BN_CLICKED) // when it's clicked, store input as k
			{				
				unsigned long int kinput = GetDlgItemInt(hWnd, IDC_ENTER_K, write, FALSE);
				SetDlgItemInt(hWnd, IDC_PROTH, kinput, FALSE);			
			
			if (*write == 1)
				{
				MessageBox(NULL, L"Value of k Accepted.", L"k Accepted", MB_ICONINFORMATION);
				}
			else
				{
				MessageBox(NULL, L"Error reading k!", L"Error!", MB_IConerror);
				}	
			}
		}
			return TRUE;

		case IDC_BUTTON2: // when Accept n is pressed

				if(wmId == BN_CLICKED) // when it's clicked, store input as c
					// YES IT IS CALLED c IN THIS PROGRAM! The user sees it as "n" because
					// that is how the Proth formula actually uses it...
			{				
				unsigned long int n = GetDlgItemInt(hWnd, IDC_ENTER_N, NULL, FALSE);
				//void mpz_set_ui (mpz_t c, unsigned long int cinput);
				}

				MessageBox(NULL, L"Value of n Accepted.", L"n Accepted", MB_ICONINFORMATION);
			return TRUE;

		case IDC_BUTTON3: // when Calculate Proth! is pressed

				if(wmId == BN_CLICKED) // when it's clicked, calculate Proth number!!
			{
				mpz_t c, two, rawproth, k, proth;
				unsigned long int n;
				unsigned long int one = 1;
				mpz_set_ui (two, 2UL);

				mpz_pow_ui ( c, two, n); // sets c to 2^n
				mpz_mul ( rawproth, k, c);		   // sets rawproth = k * c
				mpz_sub_ui ( proth, rawproth, one); // subtracts 1, creating Proth number!
				
				//SetDlgItemText (hEdit3, IDC_PROTH, L"proth");
			}
			
			return TRUE;
		}
			switch(wmId)
		{
		case IDM_about:
			DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
			break;
		
		case ID_FILE_EXIT:
			DestroyWindow(hWnd);
			break;
		case ID_HELP_INSTRUCTIONS:
			DialogBox(hInst, MAKEINTRESOURCE(IDD_INSTRUCTIONS), hWnd, Instructions);
			break;
		}

		

		break;



			break;

		case WM_DESTROY:
		{
			PostQuitMessage(0);
			return 0;
		}
		break;
	}

	return DefWindowProc(hWnd,msg,wParam,lParam);

	mpz_set_ui ( k, kinput); //sets mpz_t k from ul kinput
}


Was This Post Helpful? 0
  • +
  • -

  • (7 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »