在使用与C#代码的联合编组C结构时获取垃圾数据

时间:2013-09-13 08:13:33

标签: c# c++ pinvoke

我正在尝试在托管应用程序中使用以下C结构

typedef struct libvlc_media_track_t
{
    uint32_t    i_codec;
    uint32_t    i_original_fourcc;
    int         i_id;
    libvlc_track_type_t i_type;

    int         i_profile;
    int         i_level;

    union {
        libvlc_audio_track_t *audio;
        libvlc_video_track_t *video;
        libvlc_subtitle_track_t *subtitle;
    };

    unsigned int i_bitrate;
    char *psz_language;
    char *psz_description;

} libvlc_media_track_t;

typedef struct libvlc_audio_track_t
    {
        unsigned    i_channels;
        unsigned    i_rate;
    } libvlc_audio_track_t;

    typedef struct libvlc_video_track_t
    {
        unsigned    i_height;
        unsigned    i_width;
        unsigned    i_sar_num;
        unsigned    i_sar_den;
        unsigned    i_frame_rate_num;
        unsigned    i_frame_rate_den;
    } libvlc_video_track_t;

    typedef struct libvlc_subtitle_track_t
    {
        char *psz_encoding;
    } libvlc_subtitle_track_t;

unsigned libvlc_media_tracks_get( libvlc_media_t *p_md, libvlc_media_track_t ***tracks );

.NET版看起来像这样:

[StructLayout(LayoutKind.Explicit)]
public struct libvlc_media_track_t
{
    [FieldOffset(0)]
    public uint i_codec;
    [FieldOffset(4)]
    public uint i_original_fourcc;
    [FieldOffset(8)]
    public int i_id;
    [FieldOffset(12)]
    public libvlc_track_type_t i_type;
    [FieldOffset(16)]
    public int i_profile;
    [FieldOffset(20)]
    public int i_level;
    [FieldOffset(24)]
    public libvlc_audio_track_t audio;
    [FieldOffset(24)]
    public libvlc_video_track_t video;
    [FieldOffset(24)]
    public libvlc_subtitle_track_t subtitle;
    [FieldOffset(48)]
    public uint i_bitrate;
    [FieldOffset(52)]
    public IntPtr psz_language;
    [FieldOffset(56)]
    public IntPtr psz_description;
}

[StructLayout(LayoutKind.Sequential)]
public struct libvlc_audio_track_t
{
    public uint i_channels;
    public uint i_rate;
}

[StructLayout(LayoutKind.Sequential)]
public struct libvlc_video_track_t
{
    public uint i_height;
    public uint i_width;
    public uint i_sar_num;
    public uint i_sar_den;
    public uint i_frame_rate_num;
    public uint i_frame_rate_den;
}

[StructLayout(LayoutKind.Sequential)]
public struct libvlc_subtitle_track_t
{
    public IntPtr psz_encoding;
}

[DllImport("libvlc", CallingConvention = CallingConvention.Cdecl)]
public static unsafe extern int libvlc_media_tracks_get(IntPtr media, libvlc_media_track_t*** ppTracks);

unsafe
{
     libvlc_media_track_t** ppTracks;
     int num = LibVlcMethods.libvlc_media_tracks_get(m_hMedia, &ppTracks);
     if (num == 0 || ppTracks == null)
     {
         throw new Exception();
     }

     for (int i = 0; i < num; i++)
     {
         libvlc_media_track_t* pTrackInfo = ppTracks[i];            
     }

     LibVlcMethods.libvlc_media_tracks_release(ppTracks, num);
}

代码没有任何异常,但我在大多数结构/联合字段中都获得了垃圾数据。

请指教, 提前致谢

编辑:我也尝试了以下内容,但结果相同

[StructLayout(LayoutKind.Sequential)]
    public struct libvlc_media_track_t
    {
        public uint i_codec;
        public uint i_original_fourcc;
        public int i_id;
        public libvlc_track_type_t i_type;
        public int i_profile;
        public int i_level;
        public MediaVariant media;
        public uint i_bitrate;
        public IntPtr psz_language;
        public IntPtr psz_description;
    }

[StructLayout(LayoutKind.Explicit)]
public struct MediaVariant
{
    [FieldOffset(0)]
    public libvlc_audio_track_t audio;

    [FieldOffset(0)]
    public libvlc_video_track_t video;

    [FieldOffset(0)]
    public libvlc_subtitle_track_t subtitle;
}

1 个答案:

答案 0 :(得分:1)

您错误地翻译了联盟。我怀疑你说:

  

我在大多数结构/联合字段中获取垃圾数据。

你的意思是在联合之前数据是好的,之后是垃圾。我们来看看工会吧。它是:

union {
    libvlc_audio_track_t *audio;
    libvlc_video_track_t *video;
    libvlc_subtitle_track_t *subtitle;
};

联合包含指针。但是你已经通过将结构放在联合中来翻译它。您的C#翻译等同于:

union {
    libvlc_audio_track_t audio;
    libvlc_video_track_t video;
    libvlc_subtitle_track_t subtitle;
};

所以,只需将联盟替换为IntPtr,你就会很好。

[StructLayout(LayoutKind.Sequential)]
public struct libvlc_media_track_t
{
    public uint i_codec;
    public uint i_original_fourcc;
    public int i_id;
    public libvlc_track_type_t i_type;
    public int i_profile;
    public int i_level;
    public IntPtr media;
    public uint i_bitrate;
    public IntPtr psz_language;
    public IntPtr psz_description;
}

另外,如果你愿意,可以很容易地避免使用unsafe。虽然,如果您没有任何缺点,那么能够使用指针将使代码更容易编写。