C#调用BCB写的dll不成功
BCB中的DLL是这样写的
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
{
return 1;
}
//---------------------------------------------------------------------------
extern "C" __declspec(dllexport) __stdcall AnsiString PlayVoice(AnsiString playStr);//导出函数
AnsiString __stdcall PlayVoice(AnsiString playStr)
{
AnsiString result="播放失败";
CoInitialize(NULL);
TSpVoice *SpVoice1;
SpVoice1=new TSpVoice(NULL);
SpeechVoiceSpeakFlags spFlags=SVSFlagsAsync;
wchar_t * sptxt=new wchar_t();
try
{
AnsiString s=playStr;
sptxt=s.WideChar(sptxt,s.WideCharBufSize());
Sleep(100);
SpVoice1->Speak(sptxt,spFlags);
SpVoice1->WaitUntilDone(1000*120);
result="播放成功";
CoUninitialize();
}
catch(Exception &E)
{
result="播放失败"+E.Message;
delete sptxt;
return result;
}
return result;
}
然后在c#中调用
[DllImport("SpeechDll.dll",EntryPoint="PlayVoice",SetLastError=true,
CharSet=CharSet.Ansi, ExactSpelling=true,
CallingConvention=CallingConvention.StdCall)]
public static extern string PlayVoice(string playstr);
private void button1_Click(object sender, System.EventArgs e)
{
PlayVoice("sdf");
}
调用时老提示
未处理的“System.NullReferenceException”类型的异常出现在 WindowsApplication1.exe 中。
其他信息: 未将对象引用设置到对象的实例。
问题点数:20、回复次数:1Top
1 楼Knight94(愚翁)回复于 2006-03-23 19:03:02 得分 20
把参数中
string => StringBuilder
即string playstr 改为StringBuilder
调用的时候,要记得给StringBuilder初始化,参看msdnTop




