views:

60

answers:

2

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)
+1  A: 

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%28VS.71%29.aspx

Mark Wilkins
+1  A: 

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.

Hans Passant