error about"cannot be marshaled as an unmanaged structure"

i got a struct like:
---------------------
public ref struct OutPacket{
int m_replyIndex;
List<MainInfo^>^ m_mainInfos;
};
---------------------


when i pass it to another process, i have to transfer it to intptr :
---------------------
IntPtr pnt = Marshal::AllocCoTaskMem(Marshal::SizeOf(outPacket));
Marshal::StructureToPtr(outPacket,pnt,true);
return pnt;
---------------------

then i got a message:

*********
err = {"Type 'CProcess.OutPacket' cannot be marshaled as an unmanaged structure; no meaningful size or offset can be computed."}
*********

i think the trouble is that i have to process a "List<>"Type,maybe i should use "StructLayout",but i can't handle this well,
please help me thank u



Answer this question

error about"cannot be marshaled as an unmanaged structure"

  • xyktiger

    Pass it to where If you wish to pass it to Win32 then you can't have the system do it automatically instead you are going to have to do it manually - i.e. create and initialize the native representation yourself. If you wish to pass an instance to C# then you don't need to do anything as C# will understand the definition of the type.

  • Tahereh farrokhi

    but i want to pass the struct like this:

    public ref struct OutPacket{
    int m_replyIndex;
    List<MainInfo^>^ m_mainInfos;
    };

    if i don't marshal this ,how i can transfer it to intptr


  • Grattier

    Yes: C# will understand the definition of the type: that is what the CLR is all about. Just use your type as the return type: you don't need to do any marshalling if you are going between C++/CLI and C#.



  • BURK

    then how i can pass the "Generic.List" or "Arrayist" in vc.net to c#
  • Ed Moya

    You only need to define it once (in either C# or C++) you should define it twice because in this case you are defining two difference types.

  • hackbabu

    You can't marshall something as complex as this to a native Win32 - in general you can only automatically marshall simple types (the most complex I have tried is some of the simpler Win32 structs).

    What are you trying to acheive here: how do you expect this type to be represented on the native side



  • GunterPete

    ok. if i want to use the OutPacket passed from vc.net,i have this:

    -------------
    OutPacket outPacket = .....

    List<MainInfo> mainInfos = outPacket.m_mainInfos;
    ---------------
    then i got the error:

    cannot convert from 'System.Collections.Generic.List<NameSpace1.MainInfo>' to 'System.Collections.Generic.List<NameSpace2.MainInfo>'


  • ShadowOfTheBeast

    C# will understand the definition

    my vc.net code is:
    "SomeType" OnReply(WPARAM wParam, LPARAM lParam)
    {
    }

    my c# code will call the OnReply, then i should use the "SomeType" in my c# code

    please tell me the way i can solve the "SomeType",if it's not a "Intptr"


  • tistalker

    in c# and vc.net i declare the same struct OutPacket


    public ref struct OutPacket{
    int m_replyIndex;
    List<MainInfo^>^ m_mainInfos;
    };


    public struct OutPacket
    {
    int m_replyIndex;
    List<MainInfo^>^ m_mainInfos;
    }


    but i have this trouble when compiling:
    Error 2 Cannot implicitly convert type 'Namespace1.OutPacket' to 'Namespace2.OutPacket'


  • Michael Kariv

    If the types are defined in different namespaces then they are different types and there is not implicit conversion between the types. If you intend MainInfo to be the same type then you need to define it only once in either C# or C++/CLI.

  • Antara

    If you are passing these types to C# then you don't need to marshall them - that's part of what the CLR is all about: different languages being able to talk to each other. You only need to marshall types like this if you are going between the native world (Win32) and the managed world (CLR).

  • error about"cannot be marshaled as an unmanaged structure"