以前VC6下写的封装调用 TTS的dll,换到 windows7下用不了,不知道为什么?
调用后程序没反应,即使用了兼容模式运行也没用
是不是线程模型出问题了?// SpeechText.cpp : Defines the entry point for the DLL application.
////#include "stdafx.h"
#include <windows.h>
#include <atlbase.h> // ATL //CComPtr#include <sapi.h>
#include <sphelper.h> //SpFindBestToken#pragma comment(lib, "ole32.lib")
#pragma comment(lib, "sapi.lib")CComPtr<ISpVoice> gpVoice;
//ISpVoice * gpVoice = NULL;BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
HRESULT hret;
ISpObjectToken* pToken = NULL; switch (fdwReason) {
case DLL_PROCESS_ATTACH:
hret = CoInitialize(NULL);
if(FAILED(hret))
return FALSE; //hret = CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpVoice, (void **)&gpVoice);
hret = gpVoice.CoCreateInstance(CLSID_SpVoice);
if (FAILED(hret))
return FALSE;
if (!gpVoice)
return FALSE; //找中文男声
hret = SpFindBestToken(SPCAT_VOICES, L"language=804", L"Name=Microsoft Simplified Chinese", &pToken);
if(hret==SPERR_NOT_FOUND)
{
//找系统装的中文引擎(女声)
hret = SpFindBestToken(SPCAT_VOICES, L"language=804", NULL, &pToken);
if(hret==SPERR_NOT_FOUND)
return FALSE;
}
if(FAILED(hret))
return FALSE; gpVoice->SetVoice( pToken ); break;
case DLL_PROCESS_DETACH:
/*
if (gpVoice) gpVoice->Release();
*/
//gpVoice = NULL;
//用智能指针CComPtr会自动释放,不用Release了 CoUninitialize();
break;
default:
break;
}
return TRUE;
}BOOL WINAPI MT_SpeechText(char const * text, int async)
{
WCHAR * msg;
BOOL ret;
int len;
DWORD dwFlags; len = strlen(text) + 1;
msg = new WCHAR[len];
MultiByteToWideChar(CP_ACP, 0, text, len, msg, len);
if (async) {
dwFlags = SPF_ASYNC;
} else {
dwFlags = SPF_DEFAULT;
}
if (FAILED(gpVoice->Speak(msg, dwFlags, NULL))) {
ret = FALSE;
} else {
ret = TRUE;
}
delete msg;
return ret;
}
调用后程序没反应,即使用了兼容模式运行也没用
是不是线程模型出问题了?// SpeechText.cpp : Defines the entry point for the DLL application.
////#include "stdafx.h"
#include <windows.h>
#include <atlbase.h> // ATL //CComPtr#include <sapi.h>
#include <sphelper.h> //SpFindBestToken#pragma comment(lib, "ole32.lib")
#pragma comment(lib, "sapi.lib")CComPtr<ISpVoice> gpVoice;
//ISpVoice * gpVoice = NULL;BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
HRESULT hret;
ISpObjectToken* pToken = NULL; switch (fdwReason) {
case DLL_PROCESS_ATTACH:
hret = CoInitialize(NULL);
if(FAILED(hret))
return FALSE; //hret = CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpVoice, (void **)&gpVoice);
hret = gpVoice.CoCreateInstance(CLSID_SpVoice);
if (FAILED(hret))
return FALSE;
if (!gpVoice)
return FALSE; //找中文男声
hret = SpFindBestToken(SPCAT_VOICES, L"language=804", L"Name=Microsoft Simplified Chinese", &pToken);
if(hret==SPERR_NOT_FOUND)
{
//找系统装的中文引擎(女声)
hret = SpFindBestToken(SPCAT_VOICES, L"language=804", NULL, &pToken);
if(hret==SPERR_NOT_FOUND)
return FALSE;
}
if(FAILED(hret))
return FALSE; gpVoice->SetVoice( pToken ); break;
case DLL_PROCESS_DETACH:
/*
if (gpVoice) gpVoice->Release();
*/
//gpVoice = NULL;
//用智能指针CComPtr会自动释放,不用Release了 CoUninitialize();
break;
default:
break;
}
return TRUE;
}BOOL WINAPI MT_SpeechText(char const * text, int async)
{
WCHAR * msg;
BOOL ret;
int len;
DWORD dwFlags; len = strlen(text) + 1;
msg = new WCHAR[len];
MultiByteToWideChar(CP_ACP, 0, text, len, msg, len);
if (async) {
dwFlags = SPF_ASYNC;
} else {
dwFlags = SPF_DEFAULT;
}
if (FAILED(gpVoice->Speak(msg, dwFlags, NULL))) {
ret = FALSE;
} else {
ret = TRUE;
}
delete msg;
return ret;
}
可能还是线程模型有问题。