Announcement

Collapse
No announcement yet.

Delphi DLL mit Array Rückgabe in C# importieren

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Delphi DLL mit Array Rückgabe in C# importieren

    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.

    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;
    Wenn ich die Funktion in ein Delphi-Programm folgendermaßen importiere, funktioniert das ganze problemlos.

    Code:
    type
      TFFTData = array [0..7] of Byte;
    
    const
      SoundEngineDLL = 'SoundEngine.dll';
    
      function GetFFTData(total : PInteger) : TFFTData; stdcall; external SoundEngineDLL;
    In C# importiere ich folgendermaßen.

    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);
    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:

    Code:
            	byte[] FFTData;
            	int total = 0;
            	
            	FFTData = SoundEngine.GetFFTData(ref total);
    Wie kann ich das zum laufen bringen?

  • #2
    Desweiteren kann ich berichten, dass ...

    Code:
    [System.Runtime.InteropServices.DllImport("SoundEngine.dll")]
    public static extern void GetFFTDataEx([System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPArray, SizeConst=8)] out byte[] FFTData, out int total);
    ... mit ...

    Code:
            	byte[] FFTData;
            	int total = 0;
            	
            	//SoundEngine.GetFFTData(ref total);
            	SoundEngine.GetFFTDataEx(out FFTData, out total);
    ... zu ...


    Cannot marshal 'parameter #1': Cannot use SizeParamIndex for ByRef array parameters.


    ... führt.

    Comment

    Working...
    X