Enum HWND properties c++
Asked Answered
C

1

6

I am trying to get properties from an HWND. I'm used information from Using Window Properties, but the example below is not working for me. I'm getting an error while compiling my code.

argument of type "BOOL (__stdcall *)(HWND hwndSubclass, LPCSTR lpszString, HANDLE hData)" is incompatible with parameter of type "PROPENUMPROCEXW"

Here is my callback function

BOOL CALLBACK PropEnumProcEx(HWND hwndSubclass, LPCSTR lpszString, HANDLE hData) {
    return TRUE;
}

and this how I'm using it

EnumPropsEx(hwnd, PropEnumProcEx, NULL);

Does someone have any suggestions of how this can be fixed?

Coloquintida answered 27/8, 2017 at 9:53 Comment(1)
The error message couldn't be clearer. Do you understand it?Motorbike
F
4

LPCSTR lpszString should be LPTSTR lpszString. This argument should accept a pointer to either ANSI or Unicode null-terminated string. PROPENUMPROCEXW indicates that you are building Unicode application so EnumPropsEx macro expands to EnumPropsExW call so you need to provide callback accepting wide string as an argument. Typically you should always explicitly call Unicode variants of API functions.

Also you are missing last argument ULONG_PTR dwData.

So your callback should look like:

BOOL CALLBACK
PropEnumProcEx(HWND hwndSubclass, LPTSTR lpszString, HANDLE hData, ULONG_PTR dwData)
{
    return TRUE;
}
Fondafondant answered 27/8, 2017 at 10:0 Comment(6)
−1LPCSTR lpszString should be LPTSTR lpszString” is very ungood advice. The T macros had their time between 1995 and 2000. In 2000 we got Layer for Unicode, making the macros obsolete. Today our tools can't even produce executables for the systems, Windows 9x, that the macros targeted. It's lunacy to continue using them for non-legacy code.Auditor
@Cheersandhth.-Alf It is acceptable if this code is supposed to be built in ANSI variant as well. And my answer contains suggestion to prefer using Unicode variants of API calls.Fondafondant
@VTT: Don't recommend building as ANSI. Some readers could take you seriously. Then you just make problems for them. Admit wrongdoing. Fix answer.Auditor
@Cheersandhth.-Alf I didn't recommend building as ANSI anywhere.Fondafondant
@Cheers Please Be Nice.Eleventh
@Cheersandhth.-Alf: EnumPropsEx is a TCHAR based macro, so it is perfectly correct and reasonable to use TCHAR in the callback to match. If you don't want to support ANSI, don't use TCHAR APIs in the first place. In this case, use EnumPropsExW directly, and use LPWSTR in the callback.Nanci

© 2022 - 2024 — McMap. All rights reserved.