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?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Illia Moroz
  • 343
  • 1
  • 2
  • 13

1 Answers1

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;
}
user7860670
  • 35,849
  • 4
  • 58
  • 84
  • 2
    **−1** “`LPCSTR 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. – Cheers and hth. - Alf Aug 27 '17 at 10:27
  • @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. – user7860670 Aug 27 '17 at 10:32
  • 1
    @VTT: Don't recommend building as ANSI. Some readers could take you seriously. Then you just make problems for them. Admit wrongdoing. Fix answer. – Cheers and hth. - Alf Aug 27 '17 at 10:34
  • @Cheersandhth.-Alf I didn't recommend building as ANSI anywhere. – user7860670 Aug 27 '17 at 10:38
  • @Cheers Please Be Nice. – Nic Aug 27 '17 at 12:14
  • 1
    @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. – Remy Lebeau Aug 27 '17 at 17:58