Text-to_Speech MS Agent 4.0 Question

I am working on the MS Agent 4.0 application and got 2 problems.
1.  How can I change the speaker?  by default, is MSMary and I want to use other user like MSMike
2.  My code is working fine now for MS Mary but when I play a few times, it will crash!  How to solve it?

////////my code/////////////////////
BSTR sIP = ::SysAllocString(L"");
char  key[20];
int i;
TCHAR wsIP[300];
static const LPTSTR kpszAppTitle="";
static const LPWSTR kpwszCharacter = L"orgmerlin.acs";
      HRESULT                        hRes;
      _TCHAR                        szError[256];
      VARIANT                        vPath;
      BSTR                        bszSpeak;
      long                        lCharID;
      long                        lRequestID;
      IAgentEx               *pAgentEx;
      IAgentCharacterEx  *pCharacterEx = NULL;
      ISpObjectToken *pToken = NULL;
      ULONG               ulCount = 0;

hRes = CoCreateInstance(CLSID_AgentServer,NULL,      CLSCTX_SERVER,IID_IAgentEx,      (LPVOID *)&pAgentEx);

            // First try to load the default character

            vPath.vt = VT_BSTR;
            //vPath.vt = VT_EMPTY;
            vPath.bstrVal = SysAllocString(kpwszCharacter);
            hRes = pAgentEx->Load(vPath, &lCharID, &lRequestID);
        hRes = pAgentEx->GetCharacterEx(lCharID, &pCharacterEx);
        hRes = pCharacterEx->Show(TRUE, &lRequestID);
 bszSpeak = SysAllocString(A2W((LPTSTR)sIP));
pVoice->Speak(bszSpeak, NULL, &lRequestID);
Who is Participating?
lakshman_ceConnect With a Mentor Commented:
Try something like this for correct memory allocation/deallocation for sIP
Btw why do you need another BSTR bszSpeak. You can just use sIP directly.

void GetInputParameterValue(BSTR* sIP)
      *sIP = ::SysAllocString(L"Hello");

void main()
      BSTR sIP;  //don't allocate memory here as the length of the input string from external method may vary
      BSTR bszSpeak = SysAllocString(A2W((LPTSTR)sIP));
                //use bszSpeak or sIP
      SysFreeString(sIP); //deallocate the memory allocated for sIP
>>BSTR sIP = ::SysAllocString(L"");
>>bszSpeak = SysAllocString(A2W((LPTSTR)sIP));

SysAllocString  allocates a new string and copies the passed string into it. Appears you are calling SysAllocString twice but not freeing it twice.

You can pass sIP to
pVoice->Speak(sIP, NULL, &lRequestID);

Btw where you are assigning the value to sIP?

Also try to debug your application during crash to pin point the problem. Look at the call stack to determine which line of your code is causing the problem.

gorexyAuthor Commented:
actually sIP will be filled from external progrm (Virtools Dev) and Ihave checked there is content inside.
Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

1. For sIP, how you are allocating additional memory. What would be the length of the string from your external program. Inital calling  of SysAllocString has emptry string value for the sIP variable.
2. how the memory is deallocated for sIP
gorexyAuthor Commented:
hm..Ok I will copy my code tomorrow and show you.
gorexyAuthor Commented:
I use this to get input
beh->GetInputParameterValue(0, sIP);
bszSpeak = SysAllocString(A2W((LPTSTR)sIP));
gorexyAuthor Commented:
ok let me test

how about
How can I change the speaker?  by default, is MSMary and I want to use other user like MSMike
>>How can I change the speaker?  by default, is MSMary and I want to use other user like MSMike?

I am not sure about that.
All Courses

From novice to tech pro — start learning today.