出现的原因:
因为你传给C或C++的委托是局部的。可能传过去之后就被垃圾回收了,再次调用就会异常。
想办法做成全局的就好
public void Play(string url) { _bassStream = Bass.BASS_StreamCreateURL(url, 0, BASSFlag.BASS_DEFAULT, callback, new IntPtr()); } private DOWNLOADPROC callback; private void DownloadCallback(IntPtr buffer, int length, IntPtr user) { //if (fs == null) //{ // // fs= File.Create(@"D:\build\d.mp3"); // //fs = File.OpenWrite(@"D:\build\e.mp3"); //} if (buffer == IntPtr.Zero) { //fs.Flush(); //fs.Close(); } else { Console.WriteLine(length + "fs"); byte[] bytes = new byte[length]; //Marshal.Copy(buffer, bytes, 0, length); //fs.Write(bytes, 0, length); //using (StreamWriter sw = new StreamWriter(ms)) //{ // sw.Write(ms); //} } }
要定义委托,运行时给委托赋值。然后就可以传,
注意 一定要定义,虽然不定义直接传递方法编绎通过,但是运行还是会报错