Einzelnen Beitrag anzeigen

EWeiss
(Gast)

n/a Beiträge
 
#1

Record/Struct rückgabe by ref/var

  Alt 22. Mai 2015, 03:06
Delphi
Delphi-Quellcode:
  PBASSVIS_PARAM = ^TBASSVIS_PARAM;
  TBASSVIS_PARAM = record
    VisHandle : HVIS;
    VisGenWinHandle : DWORD;
    Kind : TBASSVIS_KIND_T;
  end;
C#
Code:
    [Serializable, StructLayout(LayoutKind.Sequential)]
    public sealed class BASSVIS_PARAM
    {
        public int VisHandle;
        public IntPtr VisGenWinHandle;
        public BASSVISKind Kind;
        public BASSVIS_PARAM(BASSVISKind kind)
        {
            this.Kind = kind;
            this.VisHandle = 0;
            this.VisGenWinHandle = IntPtr.Zero;
        }

        public BASSVIS_PARAM(BASSVISKind kind, int visHandle)
        {
            this.Kind = kind;
            this.VisHandle = visHandle;
            this.VisGenWinHandle = IntPtr.Zero;
        }
    }
Die procedure
Delphi

Delphi-Quellcode:
procedure BASSVIS_Free(
    var Base: TBASSVIS_PARAM
); stdcall; external dllfile;
C#
Code:
       
//BASSVIS_Free
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("bass_vis.dll", EntryPoint = "BASSVIS_Free", CharSet = CharSet.Auto)]
public static extern void BASSVIS_Free(BASSVIS_PARAM param);

Meine Frage ist wie kann ich in C# by ref die Classe zurückgeben?

In Delphi funktioniert das soweit alles.
procedure BASSVIS_Free(var Base: TBASSVIS_PARAM); stdcall;

Wurde das Plugin freigegeben dann setze ich die Handles auf 0.
Delphi-Quellcode:
Base.VisHandle := 0;
Base.VisGenWinHandle := 0;
Wie kann ich das jetzt in C# umsetzen?
Ein einfaches
Code:
public static extern void BASSVIS_Free(ref BASSVIS_PARAM param);
Sollte eigentlich funktionieren aber bei der Umsetzung in der Anwendung gibt es Probleme.
Das würde theoretisch funktionieren.. (Zumindest wird kein Fehler angezeigt)
Code:
BassVis.BASSVIS_Free(ref_visParam);
bool bFree = _visParam.VisHandle == 0;
if (!bFree)
Das hingegen nicht..
Code:
private IVisualization Viz = null;
//...............
BassVis.BASSVIS_Free(ref Viz.VizParam);
bool bFree = Viz.VizParam.VisHandle == 0;
if (bFree)
Error:
Zitat:
Error 15 A property, indexer or dynamic member access may not be passed as an out or ref Parameter
IVisualization ist so ausgelegt..
Code:
public interface IVisualization
{
  BassVis_Api.BASSVIS_PARAM VizParam { get; }
Nur hier kann ich kein "ref" übergeben.

gruss

Geändert von EWeiss (22. Mai 2015 um 03:14 Uhr)
  Mit Zitat antworten Zitat