Marshal void *(句柄)作为返回参数

发布于 2025-01-13 23:33:59 字数 626 浏览 1 评论 0原文

非托管 DLL 提供(简化的)函数,

int GetHandle(void* handle)
{
   int result = AssignHandle(&handle);
   return result;
}

该函数分配句柄并返回结果代码。

我必须如何封送句柄输出参数,以便我可以在 C# 代码中使用此句柄?

该代码片段

class Program
{
       [DllImport("unmanaged", CharSet = CharSet.Ansi, CallingConvention CallingConvention.StdCall)]
       extern static in GetHandle(out IntPtr s7DosHandle)
       static void Main(string[] args)
       {
           IntPtr handle;
           GetHandle(out handle);
       }
}

GetHandle 中正确分配了一个非空句柄(我可以通过调试验证),但 .NET main 函数中的 out 参数为 null。

An unmanaged DLL provides the (simplified) function

int GetHandle(void* handle)
{
   int result = AssignHandle(&handle);
   return result;
}

Which assigns a handle and returns a result code.

How do I have to marshal the handle output parameter, such that I can use this handle in C# code?

The snippet

class Program
{
       [DllImport("unmanaged", CharSet = CharSet.Ansi, CallingConvention CallingConvention.StdCall)]
       extern static in GetHandle(out IntPtr s7DosHandle)
       static void Main(string[] args)
       {
           IntPtr handle;
           GetHandle(out handle);
       }
}

Correctly assigns a non-null handle in GetHandle (which I can verify by debugging) but the out parameter in the .NET main function is null.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文