Solved

Getting instance of an extended interface to use base interface functions

Posted on 2008-10-29
4
905 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
  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 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.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

914 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now