重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
当然可以的,需要System.Runtime.InteropServices 命名空间中的 Marshal 类
长兴网站建设公司创新互联公司,长兴网站设计制作,有大型网站制作公司丰富经验。已为长兴数千家提供企业网站建设服务。企业网站搭建\成都外贸网站建设公司要多少钱,请找那个售后服务好的长兴做网站的公司定做!
Imports System.Runtime.InteropServices '这里一定要有
Public Class Form1
Public Structure m_Point
Dim x As Integer
Dim y As Integer
End Structure
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim i As Integer = 50
Dim ai() As Integer = {1, 2, 3, 4, 5}
Dim pi As IntPtr = GCHandle.Alloc(i, GCHandleType.Pinned).AddrOfPinnedObject() '取得整形变量的指针
Dim pai As IntPtr = GCHandle.Alloc(ai, GCHandleType.Pinned).AddrOfPinnedObject() '取得整形数组首地址指针
MsgBox(Marshal.ReadInt32(pi, 0)) '读回整形变量指针指向的值
MsgBox(Marshal.ReadInt32(pai, 0 * 4)) '读回数组的第一个元素
MsgBox(Marshal.ReadInt32(pai, 1 * 4)) '读回数组的第二个元素
MsgBox(Marshal.ReadInt32(pai, 2 * 4)) '读回数组的第三个元素
'-----下面是结构--------------------------
Dim m_p As New m_Point
m_p.x = 100
m_p.y = 50
Dim pm_p As IntPtr = GCHandle.Alloc(m_p, GCHandleType.Pinned).AddrOfPinnedObject() '取得结构首地址指针
MsgBox(Marshal.ReadInt32(pm_p, 0 * 4)) '读回结构的第一个值
MsgBox(Marshal.ReadInt32(pm_p, 1 * 4)) '读回结构的第二个值
End Sub
End Class
struct T_ChildStruct
{
int nChildData;
string strChildData;
T_ChildStruct()
{
nChildData = 0;
strChildData = ""; // string可以不用写初始化,本身构造中就有
}
};
struct T_FatherStruct
{
int nFatherData;
string strFatherData;
T_ChildStruct arrChild[10];
T_FatherStruct()
{
nFatherData = 0;
strFatherData = "";
}
};
结构体无法初始化值,你可以用类实现,或者写一个构造函数,把值传进去。
Public Structure wheelmodel
Public ID As Short
Public swapway() As Short
Public start As Short
Public Sub New(ByVal Size As UShort) 'Size就是传入的数组的大小
swapway = New Short(Size) {}
End Sub
End Structure
调用的时候:
Dim x As wheelmodel = New wheelmodel(10)