如何在 VB.NET 中获得指向整数的 IntPtr? [英] How do I get an IntPtr pointing to an Integer in VB.NET?

查看:82
本文介绍了如何在 VB.NET 中获得指向整数的 IntPtr?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要获取一个整数的地址并将其分配给一个 IntPtr 以在 API 中使用.怎么样?

I need to get the address of an integer and assign it to an IntPtr to use in an API. How?

Dim my_integer as Integer
Dim ptr as new IntPtr

' This fails because AddressOf can only be used for functions.
ptr = AddressOf my_integer

APICall(ptr)

我该怎么办?

推荐答案

Imports System.Runtime.InteropServices

Dim my_integer as Integer = 0
Dim ptr as IntPtr = Marshal.AllocHGlobal(4)

Marshal.WriteInt32(my_integer)

APICall(ptr)

my_integer = Marshal.ReadInt32(ptr)

Marshal.FreeHGlobal(ptr)

这篇关于如何在 VB.NET 中获得指向整数的 IntPtr?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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