Weird Characters With GetDlgItemText

This is a discussion on Weird Characters With GetDlgItemText within the Windows Programming forums, part of the Platform Specific Boards category; In all the time i have been programming and coding, i have never seen a problem as weird as this: ...

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    137

    Weird Characters With GetDlgItemText

    In all the time i have been programming and coding, i have never seen a problem as weird as this:

    I have a login form on my program, and username and password field and a login button:

    Heres my validate function:

    Code:
    BOOL CppWnd::validateInput(){
         LPSTR Test;
         LPSTR Passy;
         int nLen = SendMessage(m_hUsernameEdit, WM_GETTEXTLENGTH,0,0);
         int nLenPass = SendMessage(m_hPasswordEdittwo, WM_GETTEXTLENGTH,0,0);
         GetDlgItemText(hAppWnd, IDE_USERNAME, Test, 55);
         GetDlgItemText(hAppWnd, IDE_PASS, Passy, 54);
    	// else
    	MessageBox(hAppWnd, (LPSTR)Passy, "Invalid Data Entered", 
    		MB_OK | MB_ICONEXCLAMATION);
    	MessageBox(hAppWnd, (LPSTR)Test, "Invalid Data Entered", 
    		MB_OK | MB_ICONEXCLAMATION);
    	 if(((lstrlen(Test)) < 1) || ((lstrlen(Passy)) < 1)){
            MessageBox(hAppWnd, Passy, "Invalid Data Entered", 
    		MB_OK | MB_ICONEXCLAMATION);
    		return FALSE;
         }
    }
    Here's my createwindows in the WM_CREATE section:
    Code:
    m_hUsernameEdit = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "", WS_VISIBLE | WS_CHILD, 
            60,20, 100, 20, hAppWnd, (HMENU)IDE_USERNAME, hInst, NULL);      
            m_hPasswordEdittwo = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "", WS_VISIBLE | WS_CHILD, 230, 20, 
             100, 20, hAppWnd, (HMENU)IDE_PASS, hInst, NULL);
    I made sure there were no name conflicts or anything.

    The program compiles fine so i put some message boxes to test a bug,

    When i put something for username, username shows up FINE!!!! WORKS GREAT!!!, but when i do the same for Password field, (i removed ES_PASSWORD), it gives wierd string with weird alphabets, and says it doesnt have a string length.

    Now how can TWO identical EDIT fields, with IDENTICAL processes (I looked all over my program with find, and couldnt find any conflicts of name or anything) have a result where one works and the other doesnt?

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Code:
         LPSTR Test;
         LPSTR Passy;
    These declare char pointers (char*). Writing to or reading from a pointer that has not been initialised is known as "a very bad thing".
    Try:
    Code:
    TCHAR Test[60];
    TCHAR Passy[60];
    Also, read the following two links:
    http://blogs.msdn.com/oldnewthing/ar...28/508689.aspx
    http://blogs.msdn.com/oldnewthing/ar...20/555511.aspx

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    137
    oooooh maaan, i am sooooo dumb..... thank you so much... (cant believe i wasted 1 hour without figuring that out).

    i was thinking LPSTR Test[60]; but i didnt think it make a difference.

    But i was SURE it was LPSTR, because thats what header file says, and it worked for username, so i thought it was something else.

    One question, could there be buffer overflows that may be possible here?
    GetDlgItemText(hAppWnd, IDE_USERNAME, Test, 55);

    if someone enters morethan 55 chars?
    Last edited by execute; 05-02-2006 at 07:42 PM.

  4. #4
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    One question, could there be buffer overflows that may be possible here?
    GetDlgItemText(hAppWnd, IDE_USERNAME, Test, 55);

    if someone enters morethan 55 chars?
    The last argument of GetDlgItemText specifies the maximum number of characters, including the nul terminator, that can be returned. Therefore, if the user enters more than 54 characters the string will be truncated.

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    137
    ok, but i defined it as
    TCHAR Test[60];

    so its only maximum at 60.
    I can do a check for getdlgitemtext by getting length then adding 1.
    but should i do same for defining the Test variable?

    Like this i think would be Great:
    Code:
    int nLen = SendMessage(m_hUsernameEdit, WM_GETTEXTLENGTH,0,0);
         int nLenPass = SendMessage(m_hPasswordEdittwo, WM_GETTEXTLENGTH,0,0);
         TCHAR Test[nLen+1];
         TCHAR Passy[nLenPass+1];
         GetDlgItemText(hAppWnd, IDE_USERNAME, Test, nLen+1);
         GetDlgItemText(hAppWnd, IDE_PASS, Passy, nLenPass+1);
    The sendmsg thing not working
    anyone know another way to get length?
    Last edited by execute; 05-04-2006 at 05:57 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 11:39 PM
  2. getting weird characters and numbers when searchin
    By starvinmarvin in forum C Programming
    Replies: 3
    Last Post: 12-03-2008, 03:45 AM
  3. Replies: 10
    Last Post: 07-10-2008, 04:45 PM
  4. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 05:54 PM
  5. Getting weird characters in Strings
    By steve8820 in forum C Programming
    Replies: 3
    Last Post: 09-18-2001, 03:49 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21