Hello! I'm having a slight problem with a event... What I'm basically trying to do is:
Activate a on button event, that fetches source code, then posts it within the edit window with "SetWindowText(hTextBox, get_source("website", "path"));".

Sadly, this didn't quite work out that well, as I recieve a Run- Time Check Failure, "The variable "hTextBox" is being use without being initialized.". I tried assigning NULL to the objects; it silenced the error, but no apparent side-effect(s).

Note: "SetWindowText(hTextBox, get_source("website", "path"));", works perfectly, if I place it in the WM_CREATE event.

Any ideas?

LRESULT CALLBACK WndProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
    HWND hTextBox = NULL, hButtonGet = NULL, hButtonQuit = NULL;
    HINSTANCE hInst = GetModuleHandle(NULL);
    switch (msg) {
    case WM_CREATE:
        hTextBox = CreateWindowExW(WS_EX_CLIENTEDGE, L"EDIT", NULL, WS_CHILD|WS_VISIBLE|WS_VSCROLL|WS_HSCROLL|ES_MULTILINE|ES_AUTOVSCROLL, 7, 35, 300, 200, hwnd, NULL, hInst, NULL);
        hButtonGet = CreateWindowEx(NULL, "BUTTON", "Get Source", WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON, 10, 250, 100, 25, hwnd, (HMENU)IDC_BUTTON_GET, hInst, NULL);
        hButtonQuit = CreateWindowEx(NULL, "BUTTON", "Quit", WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON, 130, 250, 100, 25, hwnd, (HMENU)IDC_BUTTON_QUIT, hInst, NULL);
        //SetWindowText(hTextBox, get_source("www.mznation.com", "/beta/index.php"));
        break;
    case WM_COMMAND:
        switch (LOWORD(wParam)) {
        case IDC_BUTTON_GET:
            SetWindowText(hTextBox, get_source("www.mznation.com", "/index.php"));
            break;
        case IDC_BUTTON_QUIT:
            DestroyWindow(hwnd);
            break;
        }
        break;
    case WM_CLOSE:
        DestroyWindow(hwnd);
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    return 0;
}

Dani AI

Generated

Quick diagnosis: the edit control handle isn't being preserved between messages. In the posted WndProc the HWND variables are local to the function, so the handle you set during creation is lost on the next call. That is why SetWindowText works when called from the create path but not from the button handler. As noted, also verify get_source parameters and return value, but the immediate problem is the scope/lifetime of hTextBox.

Fixes (pick one):

  • Make the HWND persistent (file-scope or static).
  • Store the HWND in the window's user data with SetWindowLongPtr/GetWindowLongPtr.
  • Give the edit control a control ID and call GetDlgItem(hwnd, IDC_EDIT_TEXT) from the command handler.

A minimal example of storing/retrieving the handle via window user-data:

/* store in WM_CREATE */
SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)hTextBox);

/* retrieve in WM_COMMAND */
HWND hTextBox = (HWND)GetWindowLongPtr(hwnd, GWLP_USERDATA);

If you prefer the ID approach, create the edit with an ID and then call:

HWND hTextBox = GetDlgItem(hwnd, IDC_EDIT_TEXT);
SetWindowTextW(hTextBox, L"..."); // or convert string to wide

Important notes: match ANSI vs Unicode — if the project is Unicode, convert the char* result of get_source to wchar_t* (or make it return std::wstring) before calling SetWindowTextW. Also avoid doing blocking network I/O on the UI thread; fetch in a worker thread and post a message to the window to update the control. See the Microsoft docs for SetWindowLongPtr, GetDlgItem and SetWindowText for details.

Could the problem possibly be that you're using two different paths in your get_source function and the path is WM_CREATE is valid but the path in the case statement IDC_BUTTON_GET is invalid.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.