Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. Need help regarding marshaling

Need help regarding marshaling

Scheduled Pinned Locked Moved .NET (Core and Framework)
csharptutorialquestionbusinesshelp
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Thomas Freudenberg
    wrote on last edited by
    #1

    Hi all, in my C# app I have to call a function in a legacy Win32 DLL. This function fills a complex struct. Unfortunately, I don't know how to port the data structure to .NET. Here's a simple example:

    public class Test
    {
    [StructLayout(LayoutKind.Sequential)]
    public class Test
    {
    int i;
    }

    \[StructLayout(LayoutKind.Sequential)\]
    public class Test2
    {
        \[MarshalAs(UnmanagedType.ByValArray, SizeConst=10)\]
        public Test1\[\] tests;
    }
    
    public Test()
    {
        int size = Marshal.SizeOf(typeof(Test2));
    }
    

    }

    In the constructor, Marshal.SizeOf throws the exception Type Test2 can not be marshaled as an unmanaged structure; no meaningful size or offset can be computed. How do I marshal fixed size arrays of struct properly? Thanks in advance. Regards Thomas


    Disclaimer:
    Because of heavy processing requirements, we are currently using some of your unused brain capacity for backup processing. Please ignore any hallucinations, voices or unusual dreams you may experience. Please avoid concentration-intensive tasks until further notice. Thank you.

    H 1 Reply Last reply
    0
    • T Thomas Freudenberg

      Hi all, in my C# app I have to call a function in a legacy Win32 DLL. This function fills a complex struct. Unfortunately, I don't know how to port the data structure to .NET. Here's a simple example:

      public class Test
      {
      [StructLayout(LayoutKind.Sequential)]
      public class Test
      {
      int i;
      }

      \[StructLayout(LayoutKind.Sequential)\]
      public class Test2
      {
          \[MarshalAs(UnmanagedType.ByValArray, SizeConst=10)\]
          public Test1\[\] tests;
      }
      
      public Test()
      {
          int size = Marshal.SizeOf(typeof(Test2));
      }
      

      }

      In the constructor, Marshal.SizeOf throws the exception Type Test2 can not be marshaled as an unmanaged structure; no meaningful size or offset can be computed. How do I marshal fixed size arrays of struct properly? Thanks in advance. Regards Thomas


      Disclaimer:
      Because of heavy processing requirements, we are currently using some of your unused brain capacity for backup processing. Please ignore any hallucinations, voices or unusual dreams you may experience. Please avoid concentration-intensive tasks until further notice. Thank you.

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      First, those need to be structs. Second, you may need to specify the Test1 struct size, not just the number of elements. If you expect a variable number of elements, you should look at the SizeParam(?) property of the MarshalAsAttribute to specify the index of a member that specifies the number of elements in the struct array.


      Reminiscent of my younger years... 10 LOAD "SCISSORS" 20 RUN

      T 1 Reply Last reply
      0
      • H Heath Stewart

        First, those need to be structs. Second, you may need to specify the Test1 struct size, not just the number of elements. If you expect a variable number of elements, you should look at the SizeParam(?) property of the MarshalAsAttribute to specify the index of a member that specifies the number of elements in the struct array.


        Reminiscent of my younger years... 10 LOAD "SCISSORS" 20 RUN

        T Offline
        T Offline
        Thomas Freudenberg
        wrote on last edited by
        #3

        Heath Stewart wrote: First, those need to be structs. Ok, but it doesn't help at the moment. Heath Stewart wrote: Second, you may need to specify the Test1 struct size, not just the number of elements. If you expect a variable number of elements, you should look at the SizeParam(?) property of the MarshalAsAttribute to specify the index of a member that specifies the number of elements in the struct array. It's fixed sized, therefore I used SizeConst. :confused: Thanks anyway. Regards Thomas


        Disclaimer:
        Because of heavy processing requirements, we are currently using some of your unused brain capacity for backup processing. Please ignore any hallucinations, voices or unusual dreams you may experience. Please avoid concentration-intensive tasks until further notice. Thank you.

        L 1 Reply Last reply
        0
        • T Thomas Freudenberg

          Heath Stewart wrote: First, those need to be structs. Ok, but it doesn't help at the moment. Heath Stewart wrote: Second, you may need to specify the Test1 struct size, not just the number of elements. If you expect a variable number of elements, you should look at the SizeParam(?) property of the MarshalAsAttribute to specify the index of a member that specifies the number of elements in the struct array. It's fixed sized, therefore I used SizeConst. :confused: Thanks anyway. Regards Thomas


          Disclaimer:
          Because of heavy processing requirements, we are currently using some of your unused brain capacity for backup processing. Please ignore any hallucinations, voices or unusual dreams you may experience. Please avoid concentration-intensive tasks until further notice. Thank you.

          L Offline
          L Offline
          leppie
          wrote on last edited by
          #4

          I had the same problem and the only way is to Marshal it as a byte[] array and not the encapsulating class. If the class has more members, things become a bit more difficult. MyDUMeter: a .NET DUMeter clone

          T 1 Reply Last reply
          0
          • L leppie

            I had the same problem and the only way is to Marshal it as a byte[] array and not the encapsulating class. If the class has more members, things become a bit more difficult. MyDUMeter: a .NET DUMeter clone

            T Offline
            T Offline
            Thomas Freudenberg
            wrote on last edited by
            #5

            leppie wrote: If the class has more members, things become a bit more difficult. In fact, the structurs are cascaded about three levels X| Regards Thomas


            Disclaimer:
            Because of heavy processing requirements, we are currently using some of your unused brain capacity for backup processing. Please ignore any hallucinations, voices or unusual dreams you may experience. Please avoid concentration-intensive tasks until further notice. Thank you.

            L 1 Reply Last reply
            0
            • T Thomas Freudenberg

              leppie wrote: If the class has more members, things become a bit more difficult. In fact, the structurs are cascaded about three levels X| Regards Thomas


              Disclaimer:
              Because of heavy processing requirements, we are currently using some of your unused brain capacity for backup processing. Please ignore any hallucinations, voices or unusual dreams you may experience. Please avoid concentration-intensive tasks until further notice. Thank you.

              L Offline
              L Offline
              leppie
              wrote on last edited by
              #6

              I think it is time for a Custom Marshaller. You need to implement this interface.

              public interface ICustomMarshaler
              {
              public void CleanUpManagedData(object ManagedObj);
              public void CleanUpNativeData(IntPtr pNativeData);
              public int GetNativeDataSize();
              public IntPtr MarshalManagedToNative(object ManagedObj);
              public object MarshalNativeToManaged(IntPtr pNativeData);
              }

              And set MarshalAs(UnmanagedType.CustomMarshaler) on the class. From there you should havenough control to marshal just about anything. Note: You can now make classes without Layout. Cheers :) MyDUMeter: a .NET DUMeter clone

              T 1 Reply Last reply
              0
              • L leppie

                I think it is time for a Custom Marshaller. You need to implement this interface.

                public interface ICustomMarshaler
                {
                public void CleanUpManagedData(object ManagedObj);
                public void CleanUpNativeData(IntPtr pNativeData);
                public int GetNativeDataSize();
                public IntPtr MarshalManagedToNative(object ManagedObj);
                public object MarshalNativeToManaged(IntPtr pNativeData);
                }

                And set MarshalAs(UnmanagedType.CustomMarshaler) on the class. From there you should havenough control to marshal just about anything. Note: You can now make classes without Layout. Cheers :) MyDUMeter: a .NET DUMeter clone

                T Offline
                T Offline
                Thomas Freudenberg
                wrote on last edited by
                #7

                leppie wrote: I think it is time for a Custom Marshaller. :omg: Ok, it's an opportunity to learn something new ;) BTW, I am not the only one with this problem: http://www.dotnet247.com/247reference/msgs/28/141537.aspx[^] Regards Thomas


                Disclaimer:
                Because of heavy processing requirements, we are currently using some of your unused brain capacity for backup processing. Please ignore any hallucinations, voices or unusual dreams you may experience. Please avoid concentration-intensive tasks until further notice. Thank you.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups