c# - 将 MediaInfo DLL 与 C# DLLImport 结合使用

标签 c# dll dllimport mediainfo

使用MediaInfo DLL(64 位),我试图获取提供有关 mp4 文件信息的标准文本输出。

为了包装 dll,我根据我对 quickstart 的(有限)理解创建了一个 MediaInfo 类。指南。

public class MediaInfo
{
    [DllImport("MediaInfo.dll")]
    private static extern IntPtr MediaInfo_New();

    [DllImport("MediaInfo.dll")]
    private static extern void MediaInfo_Open(string FileName);

    [DllImport("MediaInfo.dll")]
    private static extern IntPtr MediaInfo_Inform(IntPtr Handle);

    [DllImport("MediaInfo.DLL")]
    private static extern void MediaInfo_Close(IntPtr Handle);

    IntPtr Handle;

    public MediaInfo()
    {
        Handle = MediaInfo_New();
    }

    public void Open(string FileName)
    {
        MediaInfo_Open(FileName);
    }

    public string Inform()
    {   
        return Marshal.PtrToStringUni(MediaInfo_Inform(Handle));
    }

    public void Close()
    {
        MediaInfo_Close(Handle);
    }
}

我使用下面的控制台应用程序调用此类:

static void Main(string[] args)
    {
        var objMediaInfo = new MediaInfo();
        objMediaInfo.Open("test.mp4");
        string result = objMediaInfo.Inform();
        objMediaInfo.Close();

        Console.WriteLine(result);
        Console.ReadKey();
    }

但是,我只得到一个空白字符串作为返回。我尝试了一些不同的媒体文件。

我知道已经有一个完全开发的包装器,但我想保持这个简单和轻量级。

最佳答案

试试这个可能对你有用:

更改

[System.Runtime.InteropServices.DllImport("MediaInfo.DLL")]
private static extern UIntPtr MediaInfo_Open(IntPtr Handle, string FileName);

[System.Runtime.InteropServices.DllImport("MediaInfo.DLL")]
private static extern IntPtr MediaInfo_Inform(IntPtr Handle, UIntPtr Reserved);

public System.UIntPtr Open(string FileName)
{
    return MediaInfo_Open(Handle, FileName);
}

public string Inform()
{
    return Marshal.PtrToStringUni(MediaInfo_Inform(Handle, (UIntPtr)0));
}

清理

不要忘记删除由_MediaInfo_New_创建的指针(句柄)。您需要添加:

[DllImport("MediaInfo.dll")]
private static extern void MediaInfo_Delete(IntPtr Handle);

因此:

~MediaInfo() { if (Handle == (IntPtr)0) return; MediaInfo_Delete(Handle); }

实现

然后在您的实现中,您必须传递媒体文件的完整路径而不仅仅是其名称:

static void Main(string[] args)
{
    var objMediaInfo = new MediaInfo();
    objMediaInfo.Open(@"TheFullPathOf\test.mp4");
    string result = objMediaInfo.Inform();
    objMediaInfo.Close();

    Console.WriteLine(result);
    Console.ReadKey();
}

祝你好运。

编辑

很抱歉回答不完整。我忘记在上一篇文章中提到您还需要通过 MediaInfo_Option API 设置 Inform 选项,这是定义返回数据的重要步骤:

[System.Runtime.InteropServices.DllImport("MediaInfo.DLL")]
private static extern IntPtr MediaInfo_Option(IntPtr Handle, string option, string Value);

为此创建函数:

public string Option(string option, string Value = "")
{
    return Marshal.PtrToStringUni(MediaInfo_Option(Handle, option, Value));
}

最后,修改您的实现:

static void Main(string[] args)
{
    var objMediaInfo = new MediaInfo();
    objMediaInfo.Open(@"TheFullPathOf\test.mp4");
    objMediaInfo.Option("Complete"); //or mi.Option("Complete", "1") or mi.Option("Info_Parameters") try them..
    string result = objMediaInfo.Inform();
    objMediaInfo.Close();
    Console.WriteLine(result);
    Console.ReadKey();
}

希望这次能成功。

enter image description here

关于c# - 将 MediaInfo DLL 与 C# DLLImport 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58512491/

相关文章:

C# Mysql 插入数据不起作用

c# - 将自定义路径设置为引用的 DLL?

dll - 为什么 VB6 编译的应用程序需要 VB5 VM?

c# - CoCreateInstance() -858993460(在 comip.h 中)C++

c - 使用 MSVC 编译时,程序如何调用未修饰的 stdcall DLL 导出?

c# - 条件组合框无法正常工作

c# - 如果条件无法达到,这怎么办?

c# - 如何在 Dapper .Query<>() 中添加新的 PropertyType

包含全局函数时的c++错误lnk2005

.net - .NET 4 出现 "The specified procedure could not be found"错误