0

I am trying to pass a const char* to a windows SetThreadDescription() function.

int pthread_setname_np(HANDLE thread, const char* name) { 
     return SetThreadDescription(thread, name);
}

and I am getting an error

'HRESULT SetThreadDescription(HANDLE,PCWSTR)': cannot convert argument 2 from 'const char *' to 'PCWSTR'

Of course I've stumbled into this and checked the character set and it was already set to Use Multi-Byte Character Set. Is there a way to fix this?

edit: add properties enter image description here

Ian Petek
  • 31
  • 1
  • 6
  • @Someprogrammerdude it says it's undefined – Ian Petek Nov 19 '22 at 16:29
  • 1
    The [`SetThreadDescription`](https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-setthreaddescription) function doesn't seem to exist in an "ASCII" (non-wide character) version. You need to convert the string to wide-character string and use that converted string in the call. – Some programmer dude Nov 19 '22 at 16:31
  • @Axalo Yes but shouldn't these functions be able to accept c style arguments like const char* – Ian Petek Nov 19 '22 at 16:32
  • @Someprogrammerdude oh that would probably be the reason. Where did you find that? – Ian Petek Nov 19 '22 at 16:34
  • @IanPetek Apologies. it seems that SetThreadDescription is an exception to the normal way that the Windows API handles this. So I guess you will have to do to the conversions yourself using MultiByteToWideChar. Or switch to wide chars in the first place, which is best practise I think, – john Nov 19 '22 at 16:36
  • 1
    @IanPetek No, on Windows you'll have to get used to working with `wchar_t`. The ANSI variants have long been deprecated and applications should always call the Unicode variants directly. Newer functions such as `SetThreadDescription` don't even have an ANSI variant. – Axalo Nov 19 '22 at 16:36
  • Because functions in the official Microsoft docs reference is listed explicitly with their ASCII or Wide versions. If you look at the function-list on the left side of the reference I link to you will see both `CreateProcessW` as well as `CreateProcessA` (for example). There just aren't any mentions of `SetThreadDescriptionA` or `SetThreadDescriptionW`. It might be because `SetThreadDescription` is too new? – Some programmer dude Nov 19 '22 at 16:38
  • 1
    MultiByteToWideChar it is – Ian Petek Nov 19 '22 at 16:40

2 Answers2

1

@someprogrammerdude provided the answer in a comment:

The SetThreadDescription function doesn't seem to exist in an "ASCII" (non-wide character) version. You need to convert the string to wide-character string and use that converted string in the call.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Ian Petek
  • 31
  • 1
  • 6
0

SetThreadDescription requires a unicode (two byte) description. Thread names are often just constant strings. If this is the case, just put an 'L' in front of the string.

PWSTR astring = L"a unicode description";
char *astring = "a description";
trindflo
  • 311
  • 3
  • 12