如何在 VB.NET 中访问变量地址
我必须将旧版 VB6 应用程序升级到 VB.NET;此应用程序使用来自 .dll 的函数调用,该函数将内存地址作为其参数之一。 VB6 应用程序使用 VarPtr() 函数执行此操作,但 .NET 中不存在此函数。如何检索 .NET 中变量的内存位置?
-Edit1
例如
aVariable1 = aFunctionCall(VarPtr(aVariable2))
-Edit2
确切的函数调用位于名为 FTD2XX.DLL 的 DLL 中,确切的调用是
FT_STATUS = FT_ListDevices(arg1, arg2, _
FT_LIST_BY_INDEX or FT_OPEN_BY_SERIAL_NUMBER)
I have to upgrade a legacy VB6 app to VB.NET; this app uses a function call from a .dll that takes a memory address as one of it's parameters. The VB6 app does this with the VarPtr() function, but this function does not exist in .NET. How do I retrieve the memory location of a variable in .NET?
-Edit1
For example
aVariable1 = aFunctionCall(VarPtr(aVariable2))
-Edit2
The exact function call is in a DLL called FTD2XX.DLL and the exact call is
FT_STATUS = FT_ListDevices(arg1, arg2, _
FT_LIST_BY_INDEX or FT_OPEN_BY_SERIAL_NUMBER)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试将托管代码 (.NET) 中的某些内容的地址传递给非托管 DLL 可能不是最好的计划。除了相似的语法和相似的发音名称之外,VB6 和 VB.NET 没有太多共同点。您可能需要在传递地址之前固定内存。您需要研究平台调用: http:// msdn.microsoft.com/en-us/library/aa288468(VS.71).aspx
Trying to pass addresses of something in managed code (.NET) to an unmanaged DLL might not be the best plan. VB6 and VB.NET don't have a lot in common beyond similar syntax and a similar sounding name. You may need to to pin the memory before passing an address. You will need to look into platform invoke: http://msdn.microsoft.com/en-us/library/aa288468(VS.71).aspx
当您使用 Declare 关键字声明外部函数时,它是自动的。您所要做的就是使用 ByRef 声明参数。这会强制 P/Invoke 编组器传递指向本机代码的指针。与 VarPtr 相同。仅当传递 ByVal 时,才必须将传递的参数显式转换为指针。
It is automatic when you declare the external function with the Declare keyword. All you have to do is declare the argument with ByRef. That forces the P/Invoke marshaller to pass a pointer to the native code. Same thing as VarPtr. Only if you pass ByVal do you have to explicitly convert the passed argument to a pointer.