Hi guys,
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:
Code:
HRESULT LoadCmdFromFile(
LPCWSTR *pszFileName,
SPLOADOPTIONS Options
);
Now I have a global variable LPCWSTR gramFile. and have the following functions to help me to determine the value of gramFile:
Code:
std::wstring GVC::s2ws(const std::string& s)
{
int len;
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);
std::wstring r(buf);
delete[] buf;
return r;
}
void GVC::open()
{
QString fileName = QFileDialog::getOpenFileName(this);
if(!fileName.isEmpty())
{
QFile file(fileName);
if(!file.open(QFile::ReadOnly))
{
QString msg = tr("Failed to open %1\n%2").arg(fileName).arg(file.errorString());
QMessageBox::warning(this, tr("Error"), msg);
return;
}
QDomDocument doc("grammar");
if(!doc.setContent(&file))
{
file.close();
return;
}
file.close();
std::string s = fileName.toStdString();
std::wstring ws = s2ws(s);
gramFile = ws.c_str();
}
Now the start() function will then use the file name contained in gramFile in order to start the engine.
Code:
void GVC::start()
{
if(!srh->isRecognizing)
{
//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();
timer->start(12);
ui.statusLabel->setText("Game voice control has started.");
}
}
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.
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?
Many thanks!