DllImport return the result of the function as a structure



  • Hello, everyone. Help me deal with this problem. Rewrite the function of losing bik video on C#.

    public enum BinkOpenEnum {
        BINK_OPEN_STREAM = 0x800000
    }
    

    [StructLayout(LayoutKind.Sequential)]
    public struct TBink {
    public uint Width;
    public uint Height;
    public uint Frames;
    public uint CurrentFrame;
    public uint LastFrame;
    public uint FrameRate;
    public uint FrameRate2;
    public uint ErrorCode;
    public uint Flags;
    }
    [DllImport("binkw32.dll")]
    public static extern TBink BinkOpen(string filename, BinkOpenEnum flags);

    void Start () {
    public TBink bink2 = new TBink();
    bink2 = BinkOpen("D:\bikVideo.bik", BinkOpenEnum.BINK_OPEN_STREAM);
    }

    Then I take the bink2.Width, bink2.Height, etc. and get the wrong numbers, the numbers are huge, and they sometimes change at every launch of the program.
    This is the code for Delphi:

    type
    HBink = ^Tbink;

    Tbink = packed record
        Width, Height, Frames: Longword;
        CurrentFrame: Longword;
        LastFrame: Longword;
        FrameRate: Longword;
        FrameRate2: Longword;
        ErrorCode: Longword;
        Flags: Longword;
    end;
    

    var
    Fmovie: HBink;
    FMODHandle: THandle;
    BinkOpen: function(Filename: PChar; Flags: Longword): HBink; stdcall;
    begin
    FMODHandle := LoadLibrary('binkw32.dll');
    @BinkOpen := GetAddress(FMODHandle, '_BinkOpen@8');
    Fmovie := BinkOpen('D:\bikVideo.bik', BINK_OPEN_STREAM);
    end;

    The function here reverts the index to the structure, and this also occurs on C+++. Help me put this on C#, please.



  • First, I understand the second argument of the function BinkOpen must have a type uint
    Secondly, try to declare the function that she returns the index:

    private static extern IntPtr BinkOpen(string filename, BinkOpenEnum flags);
    

    The structure can then be copied from an unmanageable memory area addressed by the index to a managed context using the method https://msdn.microsoft.com/ru-ru/library/4ca6d5z7(v=vs.110).aspx ♪

    As a result, something like this should be obtained:

    public enum BinkOpenEnum : uint
    {
        BINK_OPEN_STREAM = 0x800000
    }
    

    [StructLayout(LayoutKind.Sequential)]
    public struct Bink
    {
    public uint Width;
    public uint Height;
    public uint Frames;
    public uint CurrentFrame;
    public uint LastFrame;
    public uint FrameRate;
    public uint FrameRate2;
    public uint ErrorCode;
    public uint Flags;
    }
    [DllImport("binkw32.dll")]
    private static extern IntPtr BinkOpen(string filename, BinkOpenEnum flags);

    public static Bink BinkOpen(string filename)
    {
    IntPtr hBink = BinkOpen(filename, BinkOpenEnum.BINK_OPEN_STREAM);
    return (Bink)Marshal.PtrToStructure(hBink, typeof(Bink));
    }




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2