I'm writing a speech recognition application using Microsoft SAPI. SAPI provides a function called LoadCmdFromFile() to load a grammar file for the speech recognition engine to use. It is defined like this:
Now I have a global variable LPCWSTR gramFile. and have the following functions to help me to determine the value of gramFile:
Now the start() function will then use the file name contained in gramFile in order to start the engine.
std::wstring GVC::s2ws(const std::string& s)
int slength = (int)s.length() + 1;
len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0);
wchar_t* buf = new wchar_t[len];
MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, buf, len);
QString fileName = QFileDialog::getOpenFileName(this);
QString msg = tr("Failed to open %1\n%2").arg(fileName).arg(file.errorString());
QMessageBox::warning(this, tr("Error"), msg);
std::string s = fileName.toStdString();
std::wstring ws = s2ws(s);
gramFile = ws.c_str();
If I set the value of gramFile manually then it will be loaded and everything will work. But when I use the functions that I showed above, it wont.
//if I set its value manually the file will be loaded and everything works fine.
//gramFile = L"Test.xml";
srh->initEngine(gramFile); // initialize the attributes of the engine, grammFile is directly passed to LoadCmdFromFile();
ui.statusLabel->setText("Game voice control has started.");
I tried to debug it and noticed that the value of gramFile at the end of the open() function is a correct path like:"D:/Test.xml". However when I press a button that calls the start() function, the value of gramFile changed to:"ﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮ ﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮJŢިĘϾ龘ϷာƲ". Which obviously doesn't mean anything. I think this is the source that causes the problem but I don't know what went wrong and how to fix it.
Would any of you help out on this one?