A LPWSTR problem, help please!!!

This is a discussion on A LPWSTR problem, help please!!! within the C++ Programming forums, part of the General Programming Boards category; Hi guys, I'm writing a speech recognition application using Microsoft SAPI. SAPI provides a function called LoadCmdFromFile() to load a ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    34

    A LPWSTR problem, help please!!!

    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!

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,646
    The memory returned by "ws.c_str()" is no longer valid when "ws" is no longer value. They have the same "lifetime".

    gg

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    34
    Thank you Codeplug! I've fixed it now.

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,655
    Ha! When I started reading this thread, I thought, "Hmm, looks like a question for Codeplug" (ie: "master of the code-page"). How ironic.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21