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. C#
  4. How to invoke native MyFunction(MyStruct* myStructs, unsigned int nMyStructs) from C#?

How to invoke native MyFunction(MyStruct* myStructs, unsigned int nMyStructs) from C#?

Scheduled Pinned Locked Moved C#
csharpc++data-structurestutorialquestion
3 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.
  • C Offline
    C Offline
    Chesnokov Yuriy
    wrote on last edited by
    #1

    These are some C# class members:

    [StructLayout(LayoutKind.Sequential)]
    unsafe struct MyStruct
    {
    public int size;
    public byte* data;
    }

    //...

    [DllImport("MyStruct.dll", CharSet = CharSet.Unicode)]
    static extern unsafe void MyFunction(MyStruct* myStructs, unsigned int nMyStructs);

    How to create such array of MyStruct in C# as in C to pass it to MyFunction from C#?

    //C version
    unsigned int N = 10;
    MyStruct* pMyStructs = (MyStruct*)malloc(N * sizeof(MyStruct));
    //for i=1:N allocate pMyStruct[i].data with malloc, put some byte data where
    MyFunction(pMyStructs, N);

    //C# version?
    //How to allocate and fill those entries from managed byte[] arrays?

    Чесноков

    F D 2 Replies Last reply
    0
    • C Chesnokov Yuriy

      These are some C# class members:

      [StructLayout(LayoutKind.Sequential)]
      unsafe struct MyStruct
      {
      public int size;
      public byte* data;
      }

      //...

      [DllImport("MyStruct.dll", CharSet = CharSet.Unicode)]
      static extern unsafe void MyFunction(MyStruct* myStructs, unsigned int nMyStructs);

      How to create such array of MyStruct in C# as in C to pass it to MyFunction from C#?

      //C version
      unsigned int N = 10;
      MyStruct* pMyStructs = (MyStruct*)malloc(N * sizeof(MyStruct));
      //for i=1:N allocate pMyStruct[i].data with malloc, put some byte data where
      MyFunction(pMyStructs, N);

      //C# version?
      //How to allocate and fill those entries from managed byte[] arrays?

      Чесноков

      F Offline
      F Offline
      freakyit
      wrote on last edited by
      #2

      you need to rewrite the MyStruct in C# using the MarshalAs attribute at the vars declaration. something like above :) its only a example ^^ you need to take a look at the mapping of your C byte* to C#.. [StructLayout(LayoutKind.Sequential)] unsafe struct MyCSharpStruct { public int size; public IntPtr data; [MarshalAs(UnmanagedType.ByValTStr,SizeConst=32)] public string MyString; // C -> unsigned char MyString[32]; } [DllImport("MyStruct.dll")] static extern void MyFunction(ref MyCSharpStruct myStructs, unsigned int nMyStructs);

      1 Reply Last reply
      0
      • C Chesnokov Yuriy

        These are some C# class members:

        [StructLayout(LayoutKind.Sequential)]
        unsafe struct MyStruct
        {
        public int size;
        public byte* data;
        }

        //...

        [DllImport("MyStruct.dll", CharSet = CharSet.Unicode)]
        static extern unsafe void MyFunction(MyStruct* myStructs, unsigned int nMyStructs);

        How to create such array of MyStruct in C# as in C to pass it to MyFunction from C#?

        //C version
        unsigned int N = 10;
        MyStruct* pMyStructs = (MyStruct*)malloc(N * sizeof(MyStruct));
        //for i=1:N allocate pMyStruct[i].data with malloc, put some byte data where
        MyFunction(pMyStructs, N);

        //C# version?
        //How to allocate and fill those entries from managed byte[] arrays?

        Чесноков

        D Offline
        D Offline
        Daniel Grunwald
        wrote on last edited by
        #3

        You can allocate the memory whereever you want. A simple solution might be to allocate it in managed memory as .NET array:

        MyStruct[] myStructs = new MyStruct[N];
        fixed (MyStruct* pMyStructs = myStructs) {
        // fixed statement ensures the GC doesn't move the memory block while we access it using an unmanaged pointer
        MyFunction(pMyStructs, N);
        }

        In this case, the GC will take care of freeing the memory. You can also allocate on the stack (if N is small, for large N you would risk a stack overflow):

        MyStruct* pMyStructs = stackalloc MyStruct[N];
        MyFunction(pMyStructs, N);

        In this case, the memory is freed immediately when leaving the function that allocated it. If you want to allocate in the unmanaged heap, then use one of the allocation functions in the Marshal class:

        IntPtr memory = Marshal.AllocHGlobal(N * sizeof(MyStruct));
        try {
        MyStruct* pMyStructs = (MyStruct*)memory.ToPointer();
        MyFunction(pMyStructs, N);
        } finally {
        Marshal.FreeHGlobal(memory);
        }

        You could use Marshal.Copy to copy data from a managed array to an unmanaged pointer - but if you already have a managed array, you can simply use the first approach (fixed statement) to pass that without having to copy anything.

        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