c++windowsqttext-to-speechsapi

Errors are generated when SAPI is run in QT


Here is my code:

#include <sapi.h>

int main(int argc, char* argv[])
{
    ISpVoice * pVoice = NULL;

    if (FAILED(::CoInitialize(NULL)))
        return FALSE;

    HRESULT hr = CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpVoice, (void **)&pVoice;);
    if( SUCCEEDED( hr ) )
    {
        hr = pVoice->Speak(L"Hello world", 0, NULL);
        pVoice->Release();
        pVoice = NULL;
    }

    ::CoUninitialize();
    return TRUE;
}

This is my very first SAPI 5.1 Run, and this is the sample hello world provided by MS.

I have added the SAPI "bin" location to the system variable "Path".

Following is my QT project contect (.pro content)

#-------------------------------------------------
#
# Project created by QtCreator 2013-04-26T12:59:05
#
#-------------------------------------------------

QT       += core

QT       -= gui

TARGET = Tired
CONFIG   += console
CONFIG   -= app_bundle

TEMPLATE = app


SOURCES += main.cpp \
    ContentDetector.cpp \
    Speech.cpp

INCLUDEPATH += C:/opencv/build/include
INCLUDEPATH += C:/opencv/build/include/opencv
INCLUDEPATH += "C:/Program Files/Java/jdk1.7.0/include"
INCLUDEPATH += "C:/Program Files/Java/jdk1.7.0/include/win32"
INCLUDEPATH += "C:/Program Files/Microsoft Speech SDK 5.1/Include"

LIBS += C:/opencv/build/x86/vc9/lib/opencv_calib3d240.lib
LIBS += C:/opencv/build/x86/vc9/lib/opencv_contrib240.lib
LIBS += C:/opencv/build/x86/vc9/lib/opencv_core240.lib
LIBS += C:/opencv/build/x86/vc9/lib/opencv_features2d240.lib
LIBS += C:/opencv/build/x86/vc9/lib/opencv_flann240.lib
LIBS += C:/opencv/build/x86/vc9/lib/opencv_highgui240.lib
LIBS += C:/opencv/build/x86/vc9/lib/opencv_imgproc240.lib
LIBS += C:/opencv/build/x86/vc9/lib/opencv_objdetect240.lib
LIBS += C:/opencv/build/x86/vc9/lib/opencv_video240.lib
LIBS += "C:/Program Files/Java/jdk1.7.0/lib/jvm.lib"
LIBS += "C:/Program Files/Java/jdk1.7.0/lib/jawt.lib"
LIBS += "C:/Program Files/Microsoft Speech SDK 5.1/Lib/i386/sapi.lib"

Please note that SAPI include in the last include and SAPI lib is the last lib.

When I run this code, the followng error is generated.

C:\Users\yohan\Documents\QTPeojects\Tired\Speech.cpp:10: error: C2143: syntax error : missing ')' before ';'
C:\Users\yohan\Documents\QTPeojects\Tired\Speech.cpp:10: error: C2059: syntax error : ')'

I do not understand why.

Update

The above error is now gone after following the instructions from John. But, now the below errors are generated. Why is that?

Speech.obj:-1: error: LNK2019: unresolved external symbol __imp__CoUninitialize@0 referenced in function _main

Speech.obj:-1: error: LNK2019: unresolved external symbol __imp__CoCreateInstance@20 referenced in function _main

Speech.obj:-1: error: LNK2019: unresolved external symbol __imp__CoInitialize@4 referenced in function _main

release\Tired.exe:-1: error: LNK1120: 3 unresolved externals

Solution

  • The only way was adding the ATL.

      #define _ATL_APARTMENT_THREADED
    
    #include <atlbase.h>
    //You may derive a class from CComModule and use it if you want to override something,
    //but do not change the name of _Module
    extern CComModule _Module;
    #include <atlcom.h>
    
    #include <sapi.h>
    #include <iostream>
    
    using namespace std;
    
    int main(int argc, char* argv[])
    {
        cout << "Hello" << endl;
        ISpVoice * pVoice = NULL;
    
        if (FAILED(::CoInitialize(NULL)))
            return FALSE;
    
        HRESULT hr = CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpVoice, (void **)&pVoice);
        if( SUCCEEDED( hr ) )
        {
            cout << "Succeeded" << endl;
            hr = pVoice->Speak(L"Hello world", 0, NULL);
            pVoice->Release();
            pVoice = NULL;
        }
        else
        {
            cout << "Not succeeded" << endl;
        }
    
        ::CoUninitialize();
        return TRUE;
    }