I am trying to pass a structure from C# into C++ library. I pass structure as an object, and C++ function expects it as a pointer (void *).
I am having problem pas
Some additional information followup regarding @Rytmis's post.
From https://github.com/dotnet/runtime/blob/master/docs/coding-guidelines/interop-pinvokes.md:
Guids are usable directly in signatures. When passed by ref they can either be passed by
ref
or with the[MarshalAs(UnmanagedType.LPStruct)]
attribute.
[MarshalAs(UnmanagedType.LPStruct)]
should only be used for by ref Guids.
Try passing the structure as a ref parameter.
[DllImport("MockVadavLib.dll", CharSet = CharSet.Ansi)]
public static extern IntPtr TheFunction(ref UserRec userRec);
When you use a ref combined with a structure, it conceptually passes the address.
Incidentally, UnmanagedType.LPStruct
is rarely, if ever, the correct MarshalAs
argument. A quote from Adam Nathan who is a Microsoft employee:
UnmanagedType.LPStruct is only supported for one specific case: treating a System.Guid value type as an unmanaged GUID with an extra level of indirection.