I set a global hook,But can use SAPI to speak the word typed!
-
I want to get the word user typed and read it with SAPI. The code as follow:
#pragma data_seg("shared") HHOOK m_hHook=NULL; ISpVoice * pVoice = NULL; HRESULT hr =NULL; #pragma data_seg() #pragma comment(linker,"/section:shared,rws") //defien some var for shared HINSTANCE glhInstance=NULL; //define the global var and the glhInstance will init the //function InitInstance() ................. BOOL CHookKBDApp::InitInstance() { glhInstance=::AfxGetInstanceHandle(); if (FAILED(CoInitialize(NULL))) { AfxMessageBox("Error to intiliaze COM"); return FALSE; } return CWinApp::InitInstance(); } //////////////////// .............. StartHook() { ... m_hHook=::SetWindowsHookEx(WH_CALLWNDPROC,CallWndProc,glhInstance,0); ...... //I set a global hook to get the message WM_IME_COMPOSITION //because I won't read the english ,just chinese //in the CallWndProc function there are some problem! LRESULT CALLBACK CallWndProc(int nCode, WPARAM wParam, LPARAM lParam ) { //When Message WM_IME_COMPOSITION occured //I use following code to read the string if( SUCCEEDED( hr ) ) { hr = pVoice->Speak(test.AllocSysString(),SPF_DEFAULT,NULL); } //test is the string I got //and each time I typed some chinese in other program //the function pVoice->Speak(test.AllocSysString(),SPF_DEFAULT,NULL); //will throw out a error //just as "the memory address 0x00000 can be read" //but why?the string test is right! }
I don'y know where the bug is ,please someone tell me it! thanks a lot! Don't look at me in that way!