Search code examples
atltext-to-speechsapi

How do I convert from CSpDynamicString to `char *` with ATL?


I'm writing a custom text-to-speech program that uses SAPI 5, and one problem I'm facing is that enumerating voices with SpEnumTokens and iterating over them produces CSpDynamicString objects.

My question is, how do I convert CSpDynamicString to char * so I could printf them?

It looks like I've to use some kind of text-conversion macro from ATL. I found an example that does this (given dstrDesc is CSpDynamicString):

CSpDynamicString dstrDesc;
SpGetDescription(voiceToken, &dstrDesc);

USES_CONVERSION;

printf("%s\n", W2T(dstrDesc));

However this only prints the first character of the voice name!

Any ideas?


Solution

  • CSpDynamicString implements an operator to convert to WCHAR* and also manages the LPWSTR pointer internally. So, W2T gets you LPTSTR pointer as printf argument. If you have a Unicode build, this is results still in a WCHAR* pointer and printf("%s"... expects CHAR* argument - this is where you can have the problem you are describing.

    Try it this way:

    CSpDynamicString dstrDesc;
    SpGetDescription(voiceToken, &dstrDesc);
    printf("%ls\n", (WCHAR*) dstrDesc);