-
char to WCHAR
Hey, is there any function to convert a char to a WCHAR? I can't get the speach function to work because it wants a WCHAR. I've tried type casting but it doesn't work. I can hard code the file in but i want the user to be able to choose which file they want. To hard code it I have to add an L infront of the quation marks for the text, but how could i do something like this with a normal char?
The code below doesn't compile and gives me the following error:
cannot convert parameter 1 from 'char [255]' to 'const unsigned short *
Code:
#include <sapi.h>
#include <iostream.h>
#include <fstream.h>
int main(int argc, char* argv[])
{
ifstream file;
char string[255];
ISpVoice * pVoice = NULL;
if (FAILED(::CoInitialize(NULL)))
return 0;
cout << "Enter the name of a text file you want spoken: ";
cin >> string;
HRESULT hr = CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpVoice, (void **)&pVoice);
if( SUCCEEDED( hr ) )
{
hr = pVoice->Speak(string, SPF_IS_FILENAME | SPF_IS_XML, NULL);
pVoice->Release();
pVoice = NULL;
}
::CoUninitialize();
return 0;
}
-
This is what you are looking for.
Code:
int MultiByteToWideChar(
UINT CodePage, // code page
DWORD dwFlags, // character-type options
LPCSTR lpMultiByteStr, // address of string to map
int cchMultiByte, // number of characters in string
LPWSTR lpWideCharStr, // address of wide-character buffer
int cchWideChar // size of buffer
);
-
ahhh i don't think so...
if i put the following code in it will compile:
hr = pVoice->Speak(L"Hi how are you?", SPF_IS_FILENAME | SPF_IS_XML, NULL);
but if i do something like this it won't:
char string[150] = "hi how are you?";
hr = pVoice->Speak(string, SPF_IS_FILENAME | SPF_IS_XML, NULL);
I need to have an L infront of the quotations for it to work. How can i do that with a char?
-
Surely if you are using ASCII, then the first 255 members of the UNICODE set have the same numerical value, so why can't you simply create a 16 bit array and copy the values of the ASCII string into it?
-
*gasp* Okay, the function that I posted would work like this:
Code:
char string[150] = "How are you?";
TCHAR wstring[150];
MultiByteToWideChar(CP_ACP, 0, string, -1, wstring, 150);
Puting the L in front of the quotes (or using the _T() or TEXT() macros) tells the compiler that the string is a wide character string. It isn't really anything special beyond that. The first part of you code worked because you were passing the correct type of string. The second didn't work because you where passing in an ascii string.
-