Click here to Skip to main content
15,890,897 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi, i'm in need of some assistance getting some dialog boxes to bloody work properly. I've never done dialog boxes before, and i now know why i avoided them :\ . When my main window is first created, I want a dialog box to pop asking for some information from the user. Then, when they press the "Connect" button on the dialog box, i wish for it to close, and for my program to go about it's business. So i have this in my WM_CREATE: of my main windowproc:

C++
int ret = DialogBox(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_DIALOG1), hwnd, DlgProc);


which works fine, the dialog opens when i start the program, and lets the person fill in the two fields, an ip control for a server ip, and an edit control for a screenname. This is the dialogwindowproc, which i'm sure is where everything goes fubar:

C++
BOOL CALLBACK DlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
    switch(Message)
    {
        case WM_INITDIALOG:
        {
	      return TRUE;
	}
	case WM_COMMAND:
	{
            switch(LOWORD(wParam))
            {
                case IDC_BUTTON1:
		{
		     DWORD ipAddress = 0;
		     char * TempIp;
		     struct in_addr addr;

		     SendDlgItemMessage(hwnd, IDC_IPADDRESS1, IPM_GETADDRESS, 0, (lParam) &ipAddress);
		     addr.s_addr = (long)ipAddress;
		     TempIp = inet_ntoa(addr);
		     strcpy_s(ServerIp, TempIp);
					
		     GetDlgItemText(hwnd, IDC_EDIT1, username, 100);

		     SendMessage(mainWindow, WM_START_WINSOCK, 0, 0);
                     EndDialog(hwnd, IDOK);
		     return true;
		}
            }
	    break;
	}
        default:
            return FALSE;
    }
    return TRUE;
}


So what i thought i was doing was, when the button with the dialog ID IDC_BUTTON1 was pressed, it would first grab the ip address in the form of DWORD via the IPM_GETADDRESS message, which i then convert to a char string and then store in a global variable 'ServerIp'. I then grab the entered username via the GetDlgItemText function and store it in the global TCHAR string, 'username'. I then send a user-defined message, which is handled by my main WinProc, but i'm not worried about that cause it doesn't get any farther. I thought the end dialog function was supposed to close the dialog box entirely, but it just stays open, and the rest of my main window pops up behind it, which i can't click on because the dialog box is still up. Then, when i try and click the Connect button again to close it, i get an "Unhandeled exception" / "Access Violation" error from MSVC++ express and my program crashes. so yeh, I'm doing this wrong. Can someone help me out?
Posted

1 solution

Sounds like you are not getting to your EndDialog() call. What does the main code do on receiving WM_START_WINSOCK?

Did you try using PostMessage() instead of SendMessage()? SendMessage will not return to you until the message is completed by the receiving WindPrc.
 
Share this answer
 
Comments
FatalCatharsis 14-Nov-11 21:53pm    
i read this and within two seconds tried it in my code, fully expecting to fix the problem :P. No, it did not occur to me at all that it would not return immediately. However, surprisingly, i got the exact same behavior, tenacious little dialog, and access violation. Which befuddled me more cause, at the very least, that would have made it continue on through and end the dialog, errors or not. so what i just did was put a messagebox to pop up just before the postmessage call, and i found it wasn't even making it that far. When i move the message up, i found that the execution works up until the senddlgitemmessage call to get the ip address. it apparently stops there originally, but doesn't throw up any errors. then when i try and call it again by pressing the button again, that's when i get the error. so what is wrong with the dlg message there?
Chuck O'Toole 14-Nov-11 22:03pm    
Well, at least I was right that you weren't getting to the EndDialog() :)

The doc on IPM_GETADDRESS is pretty straightforward so there can only be a few issues. IDC_IPADDRESS1 isn't properly defined to be in IDD_DIALOG1 or the Control itself isn't created properly. I don't use that control myself and don't have any experience with it.
Chuck O'Toole 14-Nov-11 22:06pm    
The doc on the IP control says you have to

"Creating an IP Address Control
Before creating an IP address control, call InitCommonControlsEx with the ICC_INTERNET_CLASSES flag set in the dwICC member of the INITCOMMONCONTROLSEX structure.
"

before using it. I assume you did that.
FatalCatharsis 14-Nov-11 22:08pm    
BWAHAHAHA, okay, get this. you see where i call SendDlgItemMessage() ? look at that last parameter and the way i cast it, or at least TRIED to cast it. lParam is the variable LPARAM is the type :P. FAIL! y'know, friends always tell me programming could never be fun or funny, but that right theres pretty damn funny to me :P. Thanks for your help man!
Chuck O'Toole 14-Nov-11 22:11pm    
HA. Didn't see that, I read that as you intended, not as written. Nice catch.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900