Thread: I'm begging for help: How to use SAPI in C?

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    3

    Smile [SOLVED]I'm begging for help: How to use SAPI in C?

    Hello everyone,

    I have searched all around the Internet for the last 4 hours(!) and still cannot find a single answer to my problem:

    I need to do Text-To-Speech on Windows from withing C code. All of the examples for SAPI programming are that I have found were in C++ or C#. Some people in other posts online claimed to use SAPI from C, but I found no actual example.

    Please, show me a simple example on how to say something using SAPI in C code. I really need this and after 4 hours of googling I'm exhausted. Need your help.
    Last edited by premudriy; 10-07-2010 at 01:30 PM. Reason: Problem solved

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    So whatīs wrong with
    Text-to-Speech Tutorial (SAPI 5.3)

    If you know that :: is the C++ scope operator, and you can delete it for a C program, does it work?

    What is going to be real hard work is dealing with COM in a C program.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Tried reading Speech API documentation on MSDN, I assume?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Tried or Tired?

    Hey, itīs only a typo
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    3
    First of all, thank you very much for replying, people.


    I have read the SAPI SDK help. In fact, I am trying to at least compile the code mintioned in Text-to-Speech Tutorial (SAPI 5.3) at "Step 4":

    This code works in C++:
    Code:
    #include <stdafx.h>
    #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;
    }

    The closest "translation" of the code above into C code as following (this produces the least amount of errors during compiling so far):
    Code:
    #include <sapi.h>
    int main(int argc, char* argv[])
    {
        HRESULT hr;
    
        ISpVoice *pVoice = NULL;
    
    
        if (FAILED(CoInitialize(NULL)))
            printf("Error to intiliaze COM");
        else
            printf("ISpVoice Initialized\n\n");
        
    
        hr = CoCreateInstance(&CLSID_SpVoice, NULL, CLSCTX_ALL, &IID_ISpVoice, (void **)&pVoice);
        if(SUCCEEDED(hr))
        {
            hr = pVoice->Speak("Hello world", SPF_DEFAULT, NULL);
    
            if ( pVoice != NULL )
                pVoice->Release();
    
            pVoice = NULL;
        }
        CoUninitialize();
    
        return 0;
    }
    When compiling the second blob of code above I get two errors:

    Code:
    error C2039: 'Speak' : is not a member of 'ISpVoice'
    and
    error C2039: 'Release' : is not a member of 'ISpVoice'
    and I tried and tried and I can't get it to work in C in any way.


    [EDIT]: and if I use just
    Code:
    ...
     hr = Speak("Hello world", SPF_DEFAULT, NULL);
    ...
    then it says "Speak()" is undefined. Same with "Release()" function.
    Last edited by premudriy; 10-07-2010 at 12:45 PM.

  6. #6
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    639
    Code:
    #define COBJMACROS
    #include <sapi.h>
    #include <ole2.h>
    
    int main(int argc, char* argv[])
    {
        ISpVoice * pVoice = NULL;
        HRESULT hr;
    
        if (FAILED(CoInitialize(NULL)))
            return FALSE;
    
        hr = CoCreateInstance(&CLSID_SpVoice, NULL, CLSCTX_ALL, &IID_ISpVoice, (void **)&pVoice;);
        if( SUCCEEDED( hr ) )
        {
    //
    // With COBJMACROS defined, you can do this 
    //
            hr = ISpVoice_Speak(pVoice, L"Hello world", 0, NULL);
    //
    // Otherwise, you have to go through the vtable manually
    //
    //     hr = pVoice->lpVtbl->Speak(L"Hello world", 0, NULL);
            ISpVoice_Release(pVoice);
            pVoice = NULL;
        }
    
        CoUninitialize();
        return TRUE;
    }

  7. #7
    Registered User
    Join Date
    Oct 2010
    Posts
    3
    Adeyblue, thank you very very very much! It works perfectly. There's no way I could have gotten to this solution by myself.

    Once more, thank you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Error in Generating Voice using SAPI 5.1
    By lakul in forum C Programming
    Replies: 0
    Last Post: 03-13-2009, 05:02 AM
  2. Help me to this please I'm begging..
    By lesrhac03 in forum C Programming
    Replies: 23
    Last Post: 03-27-2008, 05:43 PM
  3. Begging on wall street
    By vasanth in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 10-04-2004, 04:50 PM
  4. Replies: 3
    Last Post: 08-18-2004, 07:39 AM
  5. SAPI and UNICODE
    By 123456 in forum Windows Programming
    Replies: 4
    Last Post: 09-07-2003, 07:47 AM