cant call global function out of class ???

  • Thread starter Thread starter mutant
  • Start date Start date
M

mutant

Guest
Hi.
I downloaded Speech SDK from MS site just to try all that cool stuff you can do with it :). The project is a Managed Application. I use garbage collector public class to create a form, add event handler. I cant set the global function as an event handler so i create another function that points to the global function. The function is global because it doesnt seem to do what its supposed to if its in the form class. Its all good, no errors during debugging and when i try to make the app speak a sentence after clicking a button it doesnt do anything. The only way it works is if i put the speaking in main function but that sucks cause cant do much with it if its in main cause its gonna start by tself.

Here is some code:

My button event handler:
Code:
btnSpeak->Click += new EventHandler(this,&Speaker::CheckText);

My function that initializes the function that will do the speaking:
Code:
void CheckText(Object* pSender, EventArgs* pArgs)
{
	SpeakText();
}

And my speaking function (this is the global one):
Code:
int SpeakText()
{
	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"SPEAK GODDAMIT!!!!!!", SPF_IS_XML, NULL );
        pVoice->Release();
        pVoice = NULL;
    }
    ::CoUninitialize();
    return TRUE;
}

ANY help would be appreciated.

BTW. Im the first one to post in VC++ forum :D :D :)
 
Hi mutant

I'm not a c++ syntax expert (In fact, I never coded c++ in my live), but it's close enough to c#, so here goes..

Just two questions...

1) When did you hook up your button event handler (e.g. from load... windows component code area...etc) and,
2) Did you try to debug the SpeakText function? Isn't it failing on your initializing condition:

if (FAILED(::CoInitialize(NULL)))
return FALSE;

...
 
My form class is called from main function which is run on the prog start. My button handler is hooked up when i declare the button in the form class and set up all the location, size of the button, its text, and all that. Heres the code:

This the routine that sets up the button on the form

Code:
void SetUpControls()
	{
		btnSpeak = new Button();
		btnSpeak->Text = S"Speak";
		btnSpeak->Size = System::Drawing::Size(100,20);
		btnSpeak->Location = Point(10,35);
		btnSpeak->Click += new EventHandler(this,&Speaker::CheckText);
		Controls->Add(btnSpeak);

		txtText = new TextBox();
		txtText->Size = System::Drawing::Size(100,100);
		txtText->Location = Point(10,10);
		Controls->Add(txtText);
	}

As to the question no.2. The function works if i put the code in the main() function without calling anything cause code is already there. But i want the user to be able to enter what he wants to say in the textbox.
 
Sorry man.... Tried to help, failed :(

The code seems to be fine, must be to do with the Speech SDK or some other hidden reason.

No C++.Net experts out there? :confused:

Is C++ going to be a second J#? ;)

Cheers
 
I would guess so. I've never seen anyone who frequents these forums using it :)

Maybe use will increase when vs.net 2003 comes out and includes designers for it.
 
Coding forms by hand is a lot of fun and sometimes frustration ;)
Form designers are gonna kill good ol' C++ :) :-\ :rolleyes:
 
Back
Top