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

二次信任 提交于 2019-12-20 03:23:39

问题


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.


回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/8019214/name-varptr-is-not-declared-in-old-vb-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!