0
INT_PTR CALLBACK ConnectDlg(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    HDC hdc = GetDC(hDlg);
    int wmId, wmEvent;
    static HWND hIpControl;
    static HWND hPort;
    static LPWSTR lpIPAddress = (LPWSTR)malloc(sizeof(LPWSTR));
    static LPWSTR lpPort       = (LPWSTR)malloc(sizeof(LPWSTR));
    static char* IPArgtoFn;
    static size_t IPAddressLength; 
    static size_t PortLength;
    static POINT pt;
    hIpControl = GetDlgItem(hDlg, IDC_IPADDRESS1);
    hPort      = GetDlgItem(hPort, IDC_EDIT2);
    switch (message)
    {
    case WM_INITDIALOG:
        {

        return (INT_PTR)TRUE;
        break;
        }
    case WM_COMMAND:
        {

        wmId    = LOWORD(wParam);
        wmEvent = HIWORD(wParam);
        switch (wmId)
            {
            case IDCANCEL:
                EndDialog(hDlg, wmId);
                break;
            case IDCONNECT:
                IPAddressLength = GetWindowTextLength(hIpControl) + 1;
                PortLength = SendMessage(hPort, WM_GETTEXTLENGTH, 0, 0);
                GetWindowText(hIpControl, lpIPAddress, IPAddressLength);

                pt.x = 10; pt.y = 10;
                wcstombs(IPArgtoFn, lpIPAddress, IPAddressLength);
                //TextOut(hdc, 10, 10, lpIPAddress , IPAddressLength);
                mySocket.ConnectToServer(IPArgtoFn, (int)lpPort, hdc, pt); 

                return (INT_PTR)FALSE;
                break;


            default:
                return DefWindowProc(hDlg, message, wParam, lParam);
                break;
            }
        }
    }
    return (INT_PTR)FALSE;
    }

I know I have the right control selected. Its identifier is IDC_EDIT2 and I know that is right. But whenever I try to retrieve the length of the edit control and save it to the variable PortLength, the value is always 0 when I debug it. I have already tried using GetWindowTextLength with works fine with the IP control but when I use it with the edit control, the length is always 0 no matter how long of a string I enter in the box.

Iowa15
  • 3,027
  • 6
  • 28
  • 35
  • 2
    There are a number of things in here you need to clean up. I'll start with the allocation of storage for the IP address and port number. They're not allocated correctly. You're allocating memory bytes equal to the size of a pointer, not the length of the text you're trying to retrieve. Thus I can assure you that you will be stomping on heap info with the first GetWindowText() call. Sorry that doesn't address the immediate question, but you need to clean that stuff up first. – WhozCraig Sep 04 '12 at 01:04
  • 1
    As well as that note, you will leak a DC every time your window gets a message because the DC obtained with the call to GetDC at the very top is never released. You will also leak the memory allocated for the strings. – Jonathan Potter Sep 04 '12 at 01:12

2 Answers2

1

The immediate error is in this line:

hPort = GetDlgItem(hPort, IDC_EDIT2);

This should be:

hPort = GetDlgItem(hDlg, IDC_EDIT2);
Jonathan Potter
  • 36,172
  • 4
  • 64
  • 79
1
  1. GetDlgItem(hPort, ...) should be GetDlgItem(hDlg,...) for retrieving any child control handles.

  2. Allocate your memory correctly.

  3. Return FALSE only when you do NOT handle the message when using a dialog proc. This tells the invoker to forward to DefDlgProc() so you dont' have to. Dialog callbacks are different than Window callbacks on this regard.

  4. Almost/All of the local vars in this don't need to be statics. Not saving anything there really. Ironically, the child control handles are actually decent candidates to be statics, ironic in the sense that they're somewhat the heart of the issue to begin with.

WhozCraig
  • 65,258
  • 11
  • 75
  • 141