Hallo.
Ich implementiere z.Z. eine SoundEngine für ein Computerspiel. Diese basiert auf DirectSound und als IDE benutze ich Delphi. Zur Visualisierung der Musik will ich eine Spektrumanalyse durchführen; also das Ergebnis einer FFT (Fast Fourier Transformation) in Form eines 8 x 1Byte großen Array zurück geben. Da die GameEngine und die GUI in C# programmiert werden, stelle ich eine DLL bereit.
Die Funktion sieht folgendermaßen aus.
Wenn ich die Funktion in ein Delphi-Programm folgendermaßen importiere, funktioniert das ganze problemlos.
In C# importiere ich folgendermaßen.
Die auskommentierte Zeile hab ich natürlich auch versucht.
Der Aufruf in C# schlägt mit folgender Meldung fehl:
Ausnahme System.Runtime.InteropServices.MarshalDirectiveExc eption wurde im ausgeführten Programm ausgelöst:
Cannot marshal 'return value': Invalid managed/unmanaged type combination.诃
Der Aufruf sieht, noch nebenbei erwähnt, so aus:
Wie kann ich das zum laufen bringen?
Ich implementiere z.Z. eine SoundEngine für ein Computerspiel. Diese basiert auf DirectSound und als IDE benutze ich Delphi. Zur Visualisierung der Musik will ich eine Spektrumanalyse durchführen; also das Ergebnis einer FFT (Fast Fourier Transformation) in Form eines 8 x 1Byte großen Array zurück geben. Da die GameEngine und die GUI in C# programmiert werden, stelle ich eine DLL bereit.
Die Funktion sieht folgendermaßen aus.
Code:
type TFFTData = array [0..7] of Byte; function GetFFTData(total : PInteger) : TFFTData; stdcall; var ... begin //get FFT of music channel (id=0) if TSoundEngine.Instance.GetChannel(0).GetFFTDataEx(BASS_DATA_FFT512, Temp) = -1 then ... //returning array of averaged values for i := 0 to 7 do begin ArrPart := GetArrayPart(i * 25, (i + 1) * 25, Temp); Result[i] := Round(100 * AverageValue(ArrPart)); end; ... //vu-meter Tempf := 0; for i := 0 to Length(Temp) - 1 do Tempf := Tempf + Temp[i]; total^ := Round(Tempf * 100 / Length(Temp)); end;
Code:
type TFFTData = array [0..7] of Byte; const SoundEngineDLL = 'SoundEngine.dll'; function GetFFTData(total : PInteger) : TFFTData; stdcall; external SoundEngineDLL;
Code:
[System.Runtime.InteropServices.DllImport("SoundEngine.dll")] //[return: System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPArray, SizeConst=8)] public static extern byte[] GetFFTData(ref int total);
Der Aufruf in C# schlägt mit folgender Meldung fehl:
Ausnahme System.Runtime.InteropServices.MarshalDirectiveExc eption wurde im ausgeführten Programm ausgelöst:
Cannot marshal 'return value': Invalid managed/unmanaged type combination.诃
Der Aufruf sieht, noch nebenbei erwähnt, so aus:
Code:
byte[] FFTData; int total = 0; FFTData = SoundEngine.GetFFTData(ref total);
Comment