Marshal void *(句柄)作为返回参数
非托管 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论