Search code examples
cpointerswinapistruct

Problem accessing fields after first in a struct in C using WinApi


Recently I was learning about the Win32 API for the C language, I am following the standard tutorial on Microsoft's site (this one).

While trying to implement what the tutorial teaches I have been experiencing a problem with accessing fields in a custom STATEINFO struct, specifically every time I try to access a field which is not the first one in the declaration of the struct in the WindowProcess function, my program compiles with no problem but on runtime it crashes with error 0xC000041D. For reference, my compiler is MinGW and my IDE is CodeBlocks. I have tried debugging but I can't wrap my head around this bizarre error, it had never occurred to me that I couldn't access any field after the first.

Here is a simplified version of my code:

#include <stdlib.h>
#include <stdio.h>
#include <windows.h>

LRESULT CALLBACK WindowProcess(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

typedef struct {UINT lastMessage1; UINT lastMessage2;} STATEINFO;

STATEINFO* GetAppState(HWND hwnd);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR pCmdLine, int nCmdShow) {
    // creating the window class
    const wchar_t WindowClassName[] = L"Test Window";

    WNDCLASS wc = {};
    wc.lpfnWndProc      = WindowProcess;
    wc.hInstance        = hInstance;
    wc.lpszClassName    = WindowClassName;

    RegisterClass(&wc);

    STATEINFO stateInfo = {}, *pStateInfo = &stateInfo;

    // creating the window instance
    HWND hwnd = CreateWindowEx(
        0, WindowClassName, "Window Title", WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
        NULL, NULL, hInstance, pStateInfo);
    ShowWindow(hwnd, nCmdShow);

    // starting the message loop of the window
    MSG msg = {};
    while (GetMessage(&msg, NULL, 0, 0) > 0) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
}

LRESULT CALLBACK WindowProcess(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {

    STATEINFO *pStateInfo = malloc(sizeof(STATEINFO*));

    if (uMsg == WM_CREATE) {
        pStateInfo = (STATEINFO*)(*((CREATESTRUCT*)(lParam))).lpCreateParams;
        SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)pStateInfo);
    } else {
        pStateInfo = GetAppState(hwnd);
    }
    switch (uMsg) {
        case WM_CLOSE: {
            DestroyWindow(hwnd);
            return 0;
        }
        case WM_DESTROY: {
            PostQuitMessage(0);
            return 0;
        }
        break;
    }
    // here is the problem:
    pStateInfo->lastMessage1 = 1; // this is fine
    pStateInfo->lastMessage2 = 1; // this is NOT fine, if this line is present the program crashes
    return DefWindowProc(hwnd, uMsg, wParam, lParam);
}

STATEINFO* GetAppState(HWND hwnd)
{
    LONG_PTR ptr = GetWindowLongPtr(hwnd, GWLP_USERDATA);
    STATEINFO *pStateInfo = (STATEINFO*)(ptr);
    return pStateInfo;
}

The problem presents itself whenever i try to write to the second element in the STATEINFO struct, maybe it's some type of error relating to the pointer type cast from void* to STATEINFO*? I have tried debugging but it seems that the only thing that crashes the code really is the line

pStateInfo->lastMessage2 = 1;

Accessing the field doesn't cause the problem, but parsing expressions with it does.

I have no idea why, and cannot find anything useful debugging, in the Windows tutorial or on other questions in this site.

If anyone has any suggestions to solve this problem I'm willing to learn everything I can from the replies, and if anything else is needed I will gladly provide more information in the comments.

Please keep in mind that I am not an experienced programmer, especially with the Win32 API which I am just trying to learn about, and that I might be missing some aspects of C theory. Furthermore this is my first ever question of StackOverflow so if you have any tips on how to formulate better questions I am happy to learn.

Thanks to everybody in advance!


Solution

  • the problem is with how you allocate memory for the STATEINFO structure and how you handle the pointers.

    1. You allocate memory for a pointer to STATEINFO instead of allocating memory for the actual STATEINFO structure. This causes undefined behavior when you access the fields of the structure.
    2. you correctly set and retrieve the GWLP_USERDATA to store and retrieve the pointer to your STATEINFO structure ?!
    3. memory for STATEINFO when processing the WM_CREATE message and store the pointer using SetWindowLongPtr ???
    4. avoid memory leaks when the window is destroyed !!???
    #include <stdlib.h>
    #include <stdio.h>
    #include <windows.h>
    
    LRESULT CALLBACK WindowProcess(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
    
    typedef struct {
        UINT lastMessage1;
        UINT lastMessage2;
    } STATEINFO;
    
    STATEINFO* GetAppState(HWND hwnd);
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR pCmdLine, int nCmdShow) {
        // creating the window class
        const wchar_t WindowClassName[] = L"Test Window";
    
        WNDCLASS wc = {};
        wc.lpfnWndProc      = WindowProcess;
        wc.hInstance        = hInstance;
        wc.lpszClassName    = WindowClassName;
    
        RegisterClass(&wc);
    
        STATEINFO stateInfo = {}, *pStateInfo = &stateInfo;
    
        // creating the window instance
        HWND hwnd = CreateWindowEx(
            0, WindowClassName, L"Window Title", WS_OVERLAPPEDWINDOW,
            CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
            NULL, NULL, hInstance, pStateInfo);
        ShowWindow(hwnd, nCmdShow);
    
        // starting the message loop of the window
        MSG msg = {};
        while (GetMessage(&msg, NULL, 0, 0) > 0) {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    
        return 0;
    }
    
    LRESULT CALLBACK WindowProcess(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
        STATEINFO *pStateInfo = NULL;
    
        if (uMsg == WM_CREATE) {
            CREATESTRUCT *pCreate = (CREATESTRUCT*)lParam;
            pStateInfo = (STATEINFO*)pCreate->lpCreateParams;
            SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)pStateInfo);
        } else {
            pStateInfo = GetAppState(hwnd);
        }
    
        if (pStateInfo != NULL) {
            switch (uMsg) {
                case WM_CLOSE:
                    DestroyWindow(hwnd);
                    return 0;
    
                case WM_DESTROY:
                    PostQuitMessage(0);
                    return 0;
    
                default:
                    // This is where you update the state info
                    pStateInfo->lastMessage1 = 1; // This is fine
                    pStateInfo->lastMessage2 = 1; // This should now be fine
                    break;
            }
        }
    
        return DefWindowProc(hwnd, uMsg, wParam, lParam);
    }
    
    STATEINFO* GetAppState(HWND hwnd) {
        return (STATEINFO*)GetWindowLongPtr(hwnd, GWLP_USERDATA);
    }