Solved

Getting instance of an extended interface to use base interface functions

Posted on 2008-10-29
4
931 Views
Last Modified: 2010-04-21
I have been working with Microsoft's Speech API 5.1, and now need to use the extended functionality in SAPI 5.3.  In SAPI 5.3, the new Interface ISpRecognizer2 extends ISpRecognizer from SAPI 5.1.  Now I create an instance of an ISpRecognizer2 and it works fine for the extended functions such as SetTrainingState(). But when I try to execute functions such as SetInput() that are in ISpRecognizer, I get the error "'SetInput' : is not a member of 'ATL::_NoAddRefReleaseOnCComPtr<T>' with [  T=ISpRecognizer2  ].

I'm a relative novice to C++, but have used Java and Smalltalk for decades, and it has always been true that a class or interface extending another one inherits all the functions of the base class. I know that C++ Interfaces are a bit of a workaround, but don't understand how to create an instance of the extended interface that executes functions from both levels of interface. I'm sure it's simple, but I haven't found any documentation.

The SAPI 5.3 is at http://msdn.microsoft.com/en-us/library/ms720424(VS.85).aspx
The code snippet shows the creation and cases of methods that work and don't.

Thanks for the help!
#include <sapi.h>
 
CComPtr<ISpRecognizer2> cpRecognizer;
 
cpRecognizer.CoCreateInstance(CLSID_SpInprocRecognizer);
 
cpRecognizer->SetTrainingState(TRUE, TRUE);//THIS FROM EXTENDED INTERFACE WORKS
 
cpRecognizer->SetInput(cpStream, TRUE);//THIS FROM BASE INTERFACE GETS ERROR

Open in new window

0
Comment
Question by:hutch100
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 3

Expert Comment

by:romanm
ID: 22844522
looks like it does not inherit from ISpRecognizer, so you need to QueryInterface to get ISpRecognizer,

// should do the trick. CComPtr copy constructor will do the QueryInterface for you.
CComPtr<ISpRecognizer> cpRecognizerBase = cpRecognizer;
// now we can use base functions
cpRecognizerBase->SetInput(cpStream, TRUE);
0
 

Author Comment

by:hutch100
ID: 22845939
I looked at the sapi.h (part of Windows SDK now) and it does seem that the 2 interfaces are defined independently, despite the API saying that one extends the other (and it should, to be useful). Yet both can be constructed with the same line:
cpRecognizer.CoCreateInstance(CLSID_SpInprocRecognizer).

I tried your suggestion and got the following error:

error C2440: 'initializing' : cannot convert from 'ATL::CComPtr<T>' to 'ATL::CComPtr<T>'
        with     [  T=ISpRecognizer2    ]
        and      [  T=ISpRecognizer      ]
 No constructor could take the source type, or constructor overload resolution was ambiguous

Might it work to copy the functions I need in the sapi files? Or other idea? Thanks!
0
 
LVL 3

Accepted Solution

by:
romanm earned 500 total points
ID: 22846803
Try,
CComPtr<ISpRecognizer> cpRecognizerBase;
cpRecognizer.QueryInterface<ISpRecognizer>(&cpRecognizerBase);

I am assuming you are using ATL::CComPtr.
0
 

Author Closing Comment

by:hutch100
ID: 31511492
That was brilliant!  Microsoft erred in not making it a subinterface as their doc claimed (which I confirmed by changing the .h file and getting it to compile but of course still a runtime error), but without source code I couldn't fix it.  Please accept my gratitude for saving me a lot of trouble and delay!!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question