涉及指针时如何 P/Invoke

发布于 2024-08-04 06:53:22 字数 891 浏览 3 评论 0原文

在尝试学习在 C# 中使用 PInvoke 时,我有点不确定如何处理涉及简单值类型的指针的各种情况。

我从非托管 DLL 导入以下两个函数:

public int USB4_Initialize(short* device);
public int USB4_GetCount(short device, short encoder, unsigned long* value);

第一个函数使用指针作为输入,第二个函数使用指针作为输出。它们在 C++ 中的用法相当简单:

// Pointer as an input
short device = 0; // Always using device 0.
USB4_Initialize(&device);

// Pointer as an output
unsigned long count;
USB4_GetCount(0,0,&count); // count is output

我在 C# 中的第一次尝试产生了以下 P/Invokes:

[DllImport("USB4.dll")]
public static extern int USB4_Initialize(IntPtr deviceCount); //short*

[DllImport("USB4.dll")]
public static extern int USB4_GetCount(short deviceNumber, short encoder, IntPtr value); //ulong*

如何以与上面的 C++ 代码相同的方式在 C# 中使用这些函数? 是否有更好的方法声明这些类型的方法,也许使用MarshalAs

In an attempt to learn to use PInvoke in C#, I'm a little unsure how to handle various cases with pointers involving simple value types.

I'm importing the following two functions from an unmanaged DLL:

public int USB4_Initialize(short* device);
public int USB4_GetCount(short device, short encoder, unsigned long* value);

The first function uses the pointer as an input, the second as an output. Their usage is fairly simple in C++:

// Pointer as an input
short device = 0; // Always using device 0.
USB4_Initialize(&device);

// Pointer as an output
unsigned long count;
USB4_GetCount(0,0,&count); // count is output

My first attempt in C# results in the following P/Invokes:

[DllImport("USB4.dll")]
public static extern int USB4_Initialize(IntPtr deviceCount); //short*

[DllImport("USB4.dll")]
public static extern int USB4_GetCount(short deviceNumber, short encoder, IntPtr value); //ulong*

How do I use these functions in C# in the same way as the C++ code above? Is there a better way to declare these types, perhaps using MarshalAs?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

唔猫 2024-08-11 06:53:22

如果指针指向单个基本类型而不是数组,请使用 ref / out 来描述参数。

[DllImport("USB4.dll")]
public static extern int USB4_Initialize(ref short deviceCount);

[DllImport("USB4.dll")]
public static extern int USB4_GetCount(short deviceNumber, short encoder, ref uint32 value)

在这些示例中,out 可能更合适,但两者都可以。

If the pointer is to a single primitive type and not an array, use ref / out to describe the parameter

[DllImport("USB4.dll")]
public static extern int USB4_Initialize(ref short deviceCount);

[DllImport("USB4.dll")]
public static extern int USB4_GetCount(short deviceNumber, short encoder, ref uint32 value)

In these examples out is probably more appropriate but either will work.

夕嗳→ 2024-08-11 06:53:22

.NET 运行时可以为您执行大量转换(称为“封送处理”)。虽然显式 IntPtr 始终会完全按照您的指示执行操作,但您可以用 ref 关键字替换这样的指针。

[DllImport("USB4.dll")]
public static extern int USB4_Initialize(ref short deviceCount); //short*

[DllImport("USB4.dll")]
public static extern int USB4_GetCount(short deviceNumber, short encoder, ref short value); //ulong*

然后你可以这样称呼它们:

short count = 0;

USB4_Initialize(ref count);

// use the count variable now.

The .NET runtime can do a lot of that conversion (referred to as "marshaling") for you. While an explicit IntPtr will always do EXACTLY what you tell it to, you can likely substitute the ref keyword for a pointer like that.

[DllImport("USB4.dll")]
public static extern int USB4_Initialize(ref short deviceCount); //short*

[DllImport("USB4.dll")]
public static extern int USB4_GetCount(short deviceNumber, short encoder, ref short value); //ulong*

You can then call them like this:

short count = 0;

USB4_Initialize(ref count);

// use the count variable now.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文