Solved

Getting instance of an extended interface to use base interface functions

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

  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 …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

770 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