Click to See Complete Forum and Search --> : Password box as Edit Control problem.......


ChokeK
March 5th, 2006, 05:26 PM
Hello, this is my first post on the forums.

I'm definitely new to C++ and Win32, and programming in general. In fact, I just started self-teaching myself C++ and Win32 programming about a week ago.

I'm just trying to make a program for fun that presents a password window upon the WM_CLOSE message. I made my dialog box with CreateWindowEx(), here's the code for that...

//This is in the main WndProc...
case WM_CLOSE:
hQuitBox = CreateWindowEx(0, //bring up the password window
szQBName,
"Exit Attempt",
WS_OVERLAPPED|WS_BORDER|WS_CAPTION|WS_VISIBLE,
400,
400,
200,
150,
hWnd,
NULL,
hInst,
NULL
);
ShowWindow(FindWindow("hQuitBox", 0), SW_SHOW);
UpdateWindow(FindWindow("hQuitBox", 0));
break;
//END

So hQuitBox is the HWND of the password input window. Now, the WNDCLASSEX szQBName uses the DlgProc...and this is the code for that....

LRESULT CALLBACK DlgProc(HWND hQuitBox, UINT messg2, WPARAM wParam,
LPARAM lParam)
{
HDC hdc; //handle to device context
PAINTSTRUCT pstruct2; //struct for the call to beginpaint
HWND pwEdit;

switch(messg2)
{
case WM_CREATE:
CreateWindowEx( 0,
TEXT("STATIC"),
TEXT(message4),
WS_CHILD|WS_VISIBLE|SS_LEFT,
5,
5,
125,
30,
hQuitBox,
(HMENU)IDC_STATICTXT2,
hInst,
NULL
);

pwEdit = CreateWindowEx( WS_EX_CLIENTEDGE,
"EDIT",
NULL,
WS_CHILD|WS_VISIBLE|WS_BORDER|ES_PASSWORD,
0,
20,
150,
25,
hQuitBox,
(HMENU)IDC_PWBOX,
hInst,
NULL
);

CreateWindowEx( 0,
"BUTTON",
"&OK",
WS_CHILD|WS_VISIBLE|BS_DEFPUSHBUTTON,
100,
85,
75,
25,
hQuitBox,
(HMENU)IDOK,
hInst,
NULL
);
break;

case WM_COMMAND:
switch(LOWORD(wParam)){
case IDOK:
{
//Get the length of the text user typed into the edit
long Len1 = GetWindowTextLength(GetDlgItem(hQuitBox,
IDC_PWBOX));

//set the password needed to "asdf" and init szPasswordEntered
char szPasswordNeeded[] = "asdf";
char szPasswordEntered[Len1+1];

//Get the text the user typed into the edit, store it in
szPasswordEntered
GetDlgItemText(hQuitBox, IDC_PWBOX, szPasswordEntered, sizeof
(szPasswordEntered));

//If szPasswordEntered is longer than the password, do this...
if(sizeof(szPasswordEntered) > 5){
MessageBox(hQuitBox, "Invalid Password!",
"Incorrect Password",
MB_OK | MB_ICONEXCLAMATION);
SendMessage(hQuitBox, WM_CLOSE, 0, 0);
}
//If the edit is empty, do this...
if(sizeof(szPasswordEntered) == 1){
MessageBox(hQuitBox, "Null Entry", "Error",
MB_OK | MB_ICONEXCLAMATION);
}
//If the user put the password in correctly, do this...
else if(szPasswordEntered == szPasswordNeeded){
MessageBox(hQuitBox, "Correct Password Entered!", "Goodbye!",
MB_OK | MB_ICONEXCLAMATION);
SendMessage(hQuitBox, WM_CLOSE, 0, 0);
SendMessage(FindWindowEx(NULL, NULL, szProgName,
szProgName),
WM_DESTROY, 0, 0);
}
//If the user put the password in incorrectly, do this...
else if(szPasswordEntered != szPasswordNeeded){
MessageBox(hQuitBox, szPasswordEntered, "Incorrect
Password",
MB_OK | MB_ICONEXCLAMATION);
SendMessage(pwEdit, WM_SETTEXT, 0, 0);
SendMessage(hQuitBox, WM_CLOSE, 0, 0);
}
}
break;
}
break;

szProgName is the WNDCLASSEX of the main app window. As you can see, I have it set up so that if the user puts the wrong password in, a message window pops up displaying the text the user entered. And the password needed in this sample code is "asdf".......but when I type "asdf" into the password box, I get the message box telling me it's incorrect, but it does display "asdf" as the password I entered. Why is this? Am I doing something wrong with the char variables? I don't think I am, because everything displays correctly, and if I take out the "+1" in the line
"char szPasswordEntered[Len1+1];" then it displays "asd" as the incorrect password entered.

Help!!!

Let me know if you need the whole source, I'll repost with it. Thanks much, in advance!!

-Kurt G.

Notsosuperhero
March 5th, 2006, 07:15 PM
To compare char strings use strcmp (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt_strcmp.2c_.wcscmp.2c_._mbscmp.asp)


if (strcmp(szPasswordEntered, szPasswordNeeded) == 0))
{
// The text entered matches the text needed
}
else
{
// Invalid password
}

TiMBuS
March 5th, 2006, 10:38 PM
if(sizeof(szPasswordEntered) > 5)

sizeof() returns the size of a variable (or type) in bytes. I _think_ (haven't tried it) that it will return the total size of the pointer, not the amount of chars in the array.

To get the length of a string, try the function strlen()

ChokeK
March 5th, 2006, 11:27 PM
Thank you!!!!

ChokeK
March 5th, 2006, 11:30 PM
And to TiMBuS: Although it was not my question, that information will undoubtedly help me in the future. I noticed that sizeof() returns the length of the string +1....I think it has something to do with the Null Terminating character...which I'm not exactly understanding at this point in my learning. But thank you!

greek
March 13th, 2006, 09:45 PM
Hi Friend ,

i am a new member...i need ur help to how to scroll a reports using scroll mouse.....

Bornish
March 13th, 2006, 11:02 PM
And to TiMBuS: Although it was not my question, that information will undoubtedly help me in the future. I noticed that sizeof() returns the length of the string +1....I think it has something to do with the Null Terminating character...which I'm not exactly understanding at this point in my learning. But thank you!Other things you didn't ask ;) :
- Regarding "CreateWindowEx(0,"... you could use CreateWindow() instead of passing NULL as first parameter to CreateWindowEx; why calling the extended version of the function if you don't need to set any extended styles?
- Calling ShowWindow() after creating it with WS_VISIBLE style is redundant.
- hQuitBox is, as you've said, the handle to the window (HWND); both ShowWindow() and UpdateWindow() need the HWND as first parameter; so, why are you calling FindWindow() if you already have it?
- what is your FindWindow() call supposed to do anyway? 'cause you seem to use a variable name in quotation marks and give no window title for your search...
- Regarding "SendMessage(FindWindowEx(NULL, NULL, szProgName, szProgName), WM_DESTROY, 0, 0);"... simply call PostQuitMessage(0)

Regards,

ChokeK
March 14th, 2006, 04:06 AM
Thanks, I suppose. I can't tell if you were trying to help me or insult me with your reply here...or both simultaneously.

I send WM_DESTROY cause there were other things I wanted the program to do upon receiving that message...(redisplay taskbar and start button, change a registry entry).

Anywho, thanks.

philkr
March 14th, 2006, 12:36 PM
Nobody wants to insult you. Bornish was just giving you some corrections.
Besides: I think the right way is to call DestroyWindow() when you want to close the application window and then do the cleanup in the WM_DESTROY handler and call PostQuitMessage(0) there.

Bornish
March 14th, 2006, 10:55 PM
@philkr: I agree with DestroyWindow() being the right choice for a general window (non-modal dialog).
@ChokeK: Is your password dialog a modal one created when processing the WM_CLOSE message of your main window? If yes, then you should terminate it with EndDialog(), where specifying the dialog's return value. Do not use -1 as a custom return value, because DialogBox() function returns -1 in case of failure. In main window procedure, after DialogBox (or DialogBoxParam, DialogBoxIndirect, DialogBoxIndirectParam) returns, test the return value and decide if you should close the main window or not. "return 0;" will cancel user's initial request for closing, while passing WM_CLOSE to the default window procedure (the DefWindowProc(...) call) or calling DestroyWindow(hwnd) will cleanly terminate the main application window.
Regards,

Bornish
March 14th, 2006, 11:37 PM
char szPasswordNeeded[] = "asdf";
char szPasswordEntered[Len1+1];Is this how your password string gets initialized? Because the compiler will generate a constant string "asdf" (or whatever your password is) and store it directly in the compiled form of your module (exe or dll). That means a quick look with a disassembler (or even a binary file viewer) would crack your password instantly. The least you could do is to scramble (if not encrypt) your password.
In fact, I strongly recommend that your dialog asking for the password shouldn't even test for correctness, since could be easily discovered and patched to always succeed. Instead, you could have your dialog to only ask for a password and store it, then post a message to the main window (using PostMessage), and when that custom message is received, pasword could be checked and application terminated.
Best regards,