Well, you need to create an instance of class, as is described here:
Marshalling Classes, Structures, and Unions - .NET Framework | Microsoft Learn[
^]
MyPerson personName;
personName.first = "Mark";
personName.last = "Lee";
IntPtr buffer = Marshal.AllocCoTaskMem(Marshal.SizeOf(personName));
Marshal.StructureToPtr(personName, buffer, false);
So, instead of:
Dim pnt As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(Foo))
Marshal.StructureToPtr(Foo, pnt, False)
You need something like that:
Dim f as Foo = ...
Dim pnt As IntPtr = Marshal.AllocCoTaskMem(Marshal.SizeOf(f))
Marshal.StructureToPtr(f, pnt, False)
Do you see the difference?