Advertisement Jump to content
Carlos Hdez Barbera

C++ GetDlgCtrlID and GetLastError returning 0 - Win32 Game

Recommended Posts

I'm doing a WIN32 application (kind of a table game like Tic-Tac-Toe) and I need to keep track (or get in someway) the ID of the different statics elements to change the colors.

The problem comes because the function GetDlgCtrlID is returning 0 and GetLastError is returning also a non error value.

This is the class where I'm trying to get the ID and save it

Casilla::Casilla(HWND* window, std::set<int>* stackToEnable, std::set<int>* stackToDisable)
    m_window = window;
    m_wID = GetDlgCtrlID(*window);
    std::string str = GetLastErrorAsString();
    m_setToEnable = stackToEnable;
    m_setToDisable = stackToDisable;

DWORD WINAPI Casilla::Enable()
    InvalidateRect(*m_window, NULL, TRUE);
    return 0;

DWORD WINAPI Casilla::Disable()
    InvalidateRect(*m_window, NULL, TRUE);
    return 0;

//Returns the last Win32 error, in string format. Returns an empty string if there is no error.
std::string Casilla::GetLastErrorAsString()
    //Get the error message, if any.
    DWORD errorMessageID = ::GetLastError();
    if (errorMessageID == 0)
        return std::string(); //No error message has been recorded

    LPSTR messageBuffer = nullptr;
        NULL, errorMessageID, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&messageBuffer, 0, NULL);

    std::string message(messageBuffer, size);

    //Free the buffer.

    return message;

And this is the code that create the element and the "Casilla" to keep track of the ID:

void AddSlot(HWND hWnd)
    HWND tmp = CreateWindowW(L"static", L"TO CHANGE COLOR", WS_VISIBLE | WS_CHILD | WS_BORDER | SS_CENTER, 500, 500, 100, 100, hWnd, NULL, NULL, NULL);
    Casilla* cas = new Casilla(&tmp, windowsToChangeColorToEnable, windowsToChangeColorToDisable);

Hope you can help me!

Share this post

Link to post
Share on other sites

Microsoft Docs ( say:


An application sets the identifier for a child window when it creates the window by assigning the identifier value to the hmenu parameter when calling the CreateWindow or CreateWindowEx function.

You call CreateWindow ( with NULL as hMenu, so it has the ID 0 and this is what the function returns.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!