非托管DLL函数正确调用约定 [英] Proper calling convention of unmanaged DLL function

查看:121
本文介绍了非托管DLL函数正确调用约定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着写一个最基本的超简单的轻量包装的LibVLC DLL库。我并不需要访问很多,只是能力,播放暂停,停止媒体文件。我在看文档,我发现,说明LibVLC的旧版本的其他环节,但它是过时的最新版本。我也试过LibVLC.Net,但它也已经过时,我无法找到我要找的源$ C ​​$ C匹配到我想要导出功能。

I'm trying to write a bare-bones ultra-simple light-weight wrapper for the LibVLC DLL Library. I don't need access to much, just the ability to play pause and stop media files. I'm looking at the documentation and this other link I found that explains an older version of LibVLC, but it's outdated for the most recent version. I also tried LibVLC.Net but it too is outdated and I can't find what I'm looking for in the source code to match it to the functions I'm trying to export.

我有以下签名我想出口:

I have the following signature I'm trying to export:

libvlc_new (int argc, const char *const *argv)

说明:

argc    the number of arguments (should be 0)
argv    list of arguments (should be NULL)

这是方法我想。

And this is the method I'm trying.

[DllImport("libvlc", EntryPoint = "libvlc_new")]
public static extern IntPtr New(Int32 argc, String[] argv);

描述表明,它应该是一个数组,我认为这个问题是第二个参数。我尝试过:

The description suggests it should be an array, and I think the problem is the second argument. I've tried:

[MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.LPStr)] String[] argv

如根据此处,并且有一几个其他的选项,如一个字符串和StringBuilder的的建议这里但它仍然发生,每次我打电话的功能,我得到一个失衡的PInvoke堆栈。

as according to here, and there are a couple of other options such as a String and StringBuilder as suggested here but it still happens that every time I call the function I get an Imbalanced PInvoke stack.

我需要知道这个正确的调用约定,并且很可能其他几个,功能。 A的PInvoke傻瓜在线参考将是超好。

I need to know what the proper calling convention of this, and very likely several other, functions are. A "PInvoke For Dummies" online reference would be super good.

推荐答案

没有太多声明参数类型,如果只为NULL是允许的点。刚刚宣布它的IntPtr并通过IntPtr.Zero。

Not much point in declaring the argument type if only NULL is permitted. Just declare it IntPtr and pass IntPtr.Zero.

调试器是指出你忘了申报CallingConvention。这是不是默认的.NET,这是一个__cdecl函数。所以正确的声明将是:

The debugger is pointing out that you forgot to declare the CallingConvention. It is not the default for .NET, this is a __cdecl function. So the proper declaration would be:

[DllImport("libvlc", EntryPoint = "libvlc_new", 
     CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr New(int argc, IntPtr argv);

调用为:

New(0, IntPtr.Zero);

请尽量挑一个更好的名字......

Do try to pick a better name...

这篇关于非托管DLL函数正确调用约定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆