I have a vb6.0 (basic) project which I want to migrate to the new vs2022. Most of the code just translated "more or less" fine, but there are some open points. I tried checking internet, but I couldnt find solution. So here is my issue:
I have following code:
Public Structure Test
Dim a as long
Dim b as long
....
End structure
Public Sub xyz()
'here im filling Test structure locally
a = test...
..
A(a)
End sub
external dll function I want to call is A([In] IntPtr Data)
so the calling of A with my Test structure is failing, in vb6.0 it was working fine. I am not sure whats the problem. I tried some things, but I have no clue
I expected that it can just compile like its comiling in vb6.0. I have not touched any of this code part yet.
Another thing is that VarPtr() function is not available anymore. What could be the replacement in vs2022?
CodePudding user response:
When I need to pass a structure to an unmanaged API as a pointer Intptr
, I would do the following:
Allocate unmanaged memory of the size of the structure.
Dim buffer As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(GetType(T)))
Pass the structure pointer to the unmanaged API.
When the unmanaged methods returns, converts the pointer to the structure using:
Marshal.PtrToStructure(Of T)(buffer)
Do not forget to free the previously allocated unmanaged memory
Marshal.FreeHGlobal(buffer)