How do I marshal a structure as a pointer to a structure?
Asked Answered
J

3

11

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 passing the structure.

[DllImport("MockVadavLib.dll", CharSet = CharSet.Ansi)]
public static extern IntPtr TheFunction([MarshalAs(UnmanagedType.LPStruct)] UserRec userRec);

Here is the run-time exception text I get:

"Cannot marshal 'parameter #1': Invalid managed/unmanaged type combination (this value type must be paired with Struct)."

Though I found an MSDN article that uses LPStruct in exactly this context.

This is my structure I'm trying to marshal:

[StructLayout(LayoutKind.Sequential)]
public struct UserRec {
    [MarshalAs(UnmanagedType.I4)]
    public int userParam1;
}

This is C++ function:

MOCKVADAVLIB_API tVDACQ_CallBackRec * TheFunction(void * userParams) {...
Jolinejoliotcurie answered 6/5, 2009 at 1:40 Comment(0)
I
20

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.

Inaugural answered 6/5, 2009 at 1:43 Comment(3)
You don't even need the [MarshalAs] in this case I think.Bricole
End working result: public static extern IntPtr TheFunction([MarshalAs(UnmanagedType.Struct), In] ref UserRec userRec); And [MurshalAs] here is just to make it explicit, but not required.Jolinejoliotcurie
Isn't [In] ref better than ref because the marshaler won't bother copying back?Kevyn
K
21

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.

Ketti answered 28/10, 2011 at 11:55 Comment(2)
Btw, thanks for the explanation of why this is so. That's what I really wanted.Kevyn
(Wait, what is this madness? I used ref in the signature but not at the call site! The C# language must have an exception for COM interfaces?)Kevyn
I
20

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.

Inaugural answered 6/5, 2009 at 1:43 Comment(3)
You don't even need the [MarshalAs] in this case I think.Bricole
End working result: public static extern IntPtr TheFunction([MarshalAs(UnmanagedType.Struct), In] ref UserRec userRec); And [MurshalAs] here is just to make it explicit, but not required.Jolinejoliotcurie
Isn't [In] ref better than ref because the marshaler won't bother copying back?Kevyn
C
2

Some additional information followup regarding @Rytmis's post.

From https://learn.microsoft.com/en-us/dotnet/standard/native-interop/best-practices#guids:


DO NOT Use [MarshalAs(UnmanagedType.LPStruct)] for anything other than ref GUID parameters.

Commutual answered 31/12, 2019 at 18:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.