c# 如何获取结构体首址的内存地址
就是类似VB6.0中 .pGetData = VarPtr(GetData(0))
麻烦写一个案例最好复制粘贴就能用。
#define ptr &(((stud*) 0)->score)
可以参考一下这个方法 Marshal.StructureToPtr()
官方文档: https://docs.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.marshal.structuretoptr?view=net-6.0
文档中有例子参考
取地址尝试过嘛?
1.定义结构体
2.定义结构体变量及指针
3.指针指向变量,指针中就是变量地址
或者直接用&变量名,获得地址
你用下面这个函数,它跟vb用法是一样的
int VarPtr(object e)
{
System.Runtime.InteropServices.GCHandle gh = System.Runtime.InteropServices.GCHandle.Alloc(e, System.Runtime.InteropServices.GCHandleType.Pinned);
int gc = gh.AddrOfPinnedObject().ToInt32();
gh.Free();
return gc;
}
或者从VB复制过来直接引用
Declare Function VarPtr Lib "msvbvm50.dll" (Var As Any) As Long