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

This is a discussion on I'm begging for help: How to use SAPI in C? within the C Programming forums, part of the General Programming Boards category; Hello everyone, I have searched all around the Internet for the last 4 hours(!) and still cannot find a single ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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
    611
    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

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