GetDlgCtrlID and GetLastError returning 0

I'm doing a WIN32 application 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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
    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);
    	m_setToEnable->insert(m_wID);
    	return 0;
    }
    
    DWORD WINAPI Casilla::Disable()
    {
    	InvalidateRect(*m_window, NULL, TRUE);
    	m_setToDisable->insert(m_wID);
    	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;
    	size_t size = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
    		NULL, errorMessageID, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&messageBuffer, 0, NULL);
    
    	std::string message(messageBuffer, size);
    
    	//Free the buffer.
    	LocalFree(messageBuffer);
    
    	return message;
    }


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

1
2
3
4
5
    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);
    }
Last edited on
1
2
3
WND 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);

It's not a good idea to pass the address of a temporary variable.

Try this:
1
2
3
Casilla::Casilla(HWND window, std::set<int>* stackToEnable, std::set<int>* stackToDisable)
In AddSlot
Casilla* cas = new Casilla(tmp, windowsToChangeColorToEnable, indowsToChangeColorToDisable);

Who is going to delete cas ?
Wht not just using Casilla cas(tmp, windowsToChangeColorToEnable, indowsToChangeColorToDisable)
I come from C# and I still have some problems about heap/stack objects.

I did it like that thinking that maybe the copy of the HWND would be much more expensive than copying the Pointer. Didn't thought about the delete of stack variables when leaving the scope.

If I do Casilla cas(tmp, windowsToChangeColorToEnable, indowsToChangeColorToDisable)

Am I not going to have the same problem? Is not going to dissapear at the end of the scope?

Thanks!
Last edited on
I also have a C# background- actually it's my favourite language. :)
HWND is actually a pointer i.e. PVOID => void*
Am I not going to have the same problem? Is not going to dissapear at the end of the scope?
No, if you pass a pointer by value it gets copied(shallow copy)
https://stackoverflow.com/questions/4426474/is-passing-pointer-argument-pass-by-value-in-c

Didn't thought about the delete of stack variables when leaving the scope.
Unfortunately C++ doesn't have a garbage collector, but it has smart pointers.
https://msdn.microsoft.com/en-us/library/hh279674.aspx

BTW: Is it working now?
Topic archived. No new replies allowed.