Name 'VarPtr' is not declared.In old vb co

2019-03-02 12:52发布

I have a old code in VB.Now I convert it into vb.net.There is a line in a code

Dim pCParameters As Integer

pCParameters = VarPtr(Parameters)

When I execute code the error occure that

Name 'VarPtr' is not declared.

VarPtr not supported in vb.net.So how I replace it.

2条回答
戒情不戒烟
2楼-- · 2019-03-02 13:20

Yes I found the answer.The new VarPtr function is

Public Function VarPtr(ByVal e As Object) As Integer
Dim GC As GCHandle = GCHandle.Alloc(e, GCHandleType.Pinned)
Dim GC2 As Integer = GC.AddrOfPinnedObject.ToInt32
GC.Free()
Return GC2
查看更多
混吃等死
3楼-- · 2019-03-02 13:23

This is not as straight forward because your variables in .NET are managed. To do exactly what you are asking you need to look at GCHandle.Alloc and pin the variable so it cannot be moved. Then you can get its memory address.
Something like this (from memory):

GCHandle handle = GCHandle.Alloc(pCParameters , Pinned )
IntPtr ptr = handle.AddressOfPinnedObject
查看更多
登录 后发表回答