Click here to Skip to main content
15,071,637 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Greetings coders,

I have questions related to Windows API, that seems difficult to seek for clarification. I hope this isn't too much to ask, since this is my first. My questions are related to:

1. GetClassLong API. Is it necessary to:

*Call DestroyIcon for hicon returned from GetClassLong(hwnd, GCL_HICON) or GetClassLong(hwnd,GCL_HSMICON)

*Call DestroyCursor for hcursor returned from GetClassLong(hwnd, GCL_HCURSOR)

*Call DeleteObject for hbrush returned from GetClassLong(hwnd, GCL_HBRBACKGROUND)

*If function callers didn't do that would it introduce memory leak or wild (unclosed) handle?

2. GetClassInfoEx API

I found that the lpfnwndProc member of the WNDCLASSEX structure returns different value with value returned by GetClassLong(hwnd, GCL_WNDPROC). I'm interested in this API in a thought for better performance, instead of calling GetClassLong for each field I figure it'll be better off to use this and have all class info I need. But to see this and the failures obtaining info from non system classes discouraged me.

3. EnumThreadWindows API

I found inconsistency in EnumThreadWindows where if the command prompt window is open EnumThreadWindows find the window to belong to a thread running under the csrss.exe process, but EnumWindows or EnumChildWindows (passing desktop hwnd as hwndParent) didn't say so. GetWindowThreadProcessId agreed with EnumWindows & EnumChildWindows. This fact is found on WinXP SP2, for you who has XP machines could you please verify if this occurs in your system too. It would be nice to have clear info of which API is correct.

4. GetWindowInfo API

Why the dwExStyle member of WINDOWINFO structure gets a different value with the value returned by GetWindowLong(hwnd, GWL_EXSTYLE). I see the value differs by 0x20000000, an unknown value for ex style, I couldn't find any constant for ex style having such value.

1. No, the information returned belongs to the registered class so you should not change it, as it could affect other active Windows.
2. Same comment.
3. These functions sometimes return information about non-visible windows used by the system.
4. Some of these use extra undocumented values that are used internally by the OS.

Best advice, don't use any of these functions unless you fully understand them. And that is not easy as much of the information you need is not available unless you sign up to the MSDN developer program.
Ivan Pasaribu 2-Jun-15 2:24am
Hi Richard,

1. Thanks for the clarification, I don't intend to change the class info, I only display the info on screen, it's just to my understanding some handles must be closed or destroyed in order to free allocated memory. And the function declaration in winuser.h stated that the value type as HICON, HCURSOR, HBRUSH respectively.

2. Which API would you prefer, GetClassInfoEx or GetClassLong?. I get different value for the WNDPROC while the hwnd is the same.

3. The command prompt window is not hidden, could you be more specific on the 'non-visible' part?. did yo mean that the command prompt window is a message-only window?

4. What should I do with the undocumented ex style value. At the moment I'm using the value to generate a list of ex style constant names like Spy++. If there is a way to deal with undocumented ex style I would go with GetWindowInfo because it gives me much info in one call., your suggestion?

Thanks for the advice, I don't think I'd be signing up for MSDN developers program, I still have too much to learn, so perhaps you could point me to a link where I can learn more of these things? before I post this on CP I have read MSDN's pages related to each API but didn't find any answer, and the community comments also had no post related to the issue I'm facing.
Richard MacCutchan 2-Jun-15 3:59am
1. You should not close any handles associated with this structure: just leave it alone, it is needed by the system.
2. Neither, why are you concerned with this item?
3. Sorry, I don't understand the question.
4. Just show it as unknown value.

Finally, I am not sure exactly what issue/problem you are trying to solve here. Perhaps you should explain in more detail what you are trying to do.
Ivan Pasaribu 2-Jun-15 12:12pm
I'm writing an app that functions pretty much like Spy++ Richard, that's why my questions were related to user APIs. I have understood your point on #1 and #4, thank you.

#2 We know those functions give us info of a window's class, as I see it GetClassLong is more reliable compared to GetClassInfoEx, since GetClassInfoEx doesn't always work (especially with classes not registered by the system), but to get the same information GetClassInfoEx can give us in just one call I'd have to call GetClassLong several times, with different flag. Perhaps I should've asked if anyone ever succeeded to get GetClassInfoEx to work smoothly on any window in the first place. And if GetClassInfoEx is as reliable as GetClassLong I would surely prefer GetClassInfoEx over GetClassLong for I could have all info I need in just one call, and this question would have never been asked.

#3 If you are interested to try and see if you can see what I see in my machine you can try to follow these steps:
1. Run command prompt
2. Enumerate threads running under csrss.exe process
3. Call EnumThreadWindows on each thread you found in step 2

In my machine I found the command prompt window running under one of the csrss.exe threads because the callback function for EnumThreadWindows came up with the command prompt window's hWnd during window enumeration progress.

IMHO when there are two functions serving same purpose that's redundancy, but two functions for same purpose and one of them gives different result I say that's problem, I hope this explains enough what I meant by "Issue".

Best Regards,
Richard MacCutchan 2-Jun-15 12:28pm
You are going into the realms of deep internal workings of Windows; somewhere I have not much experience of I'm afraid. I can only suggest again that you sign up to the MSDN developer network where you will be able to get much more information on these features.
In addition to what already told by Richard about the point 2: values returned by GetClassInfoEx and GetClassLong.
These functions can return indifferently a real address or an handle to the window procedure (the reason is someway related to memory mapping of the procedure and its process). The translation between handle and address is made by the function CallWindowProc()[^], and this is the reason why you should always use this function instead of calling directly the address returned.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900