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. Marshal an object [] to ptr

Marshal an object [] to ptr

Scheduled Pinned Locked Moved C#
csharptutorialquestion
2 Posts 2 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.
  • J Offline
    J Offline
    joeyespo
    wrote on last edited by
    #1

    Hi, Is there any painless way to marshal an object [] into a ptr? I'm writing a plugin wrapper and the object [] is a paramarray ['params'] that represents a structure in the plugin. I was wondering if there was an easy way to do this, similar to the Marshal.StructureToPtr() maybe .. Here's an example for clearification ..


    The wrapper's function: void MyPluginFunction (params object [] data) { // Convert information stored in 'data' into a ptr ptr = MarshalObjToPtr(data); PluginFunction(ptr); } The plugin function to be called (written in C# for this example) void PluginFunction (IntPtr ptr) { myData = (MyStruct)Marshal.PtrToStructure(ptr); ... } Now 'myData' now holds all info in ptr, but in MyStruct representation. NOTE: The plugin may not be written in C#, and that is why I would need is to be marshalled.


    Using the wrapper would be as simple as: MyPluginFunction(12, 8, "hello"); The above would map to the plugin's structure: private struct MyStruct { int a, b; string s; } Thus allowing the above function's 'myData' to be as follows: myData.a = 12, myData.b = 8, myData.s = "hello"


    Or if there's an easier way to do this, that could work too. Best regards, - Joe

    H 1 Reply Last reply
    0
    • J joeyespo

      Hi, Is there any painless way to marshal an object [] into a ptr? I'm writing a plugin wrapper and the object [] is a paramarray ['params'] that represents a structure in the plugin. I was wondering if there was an easy way to do this, similar to the Marshal.StructureToPtr() maybe .. Here's an example for clearification ..


      The wrapper's function: void MyPluginFunction (params object [] data) { // Convert information stored in 'data' into a ptr ptr = MarshalObjToPtr(data); PluginFunction(ptr); } The plugin function to be called (written in C# for this example) void PluginFunction (IntPtr ptr) { myData = (MyStruct)Marshal.PtrToStructure(ptr); ... } Now 'myData' now holds all info in ptr, but in MyStruct representation. NOTE: The plugin may not be written in C#, and that is why I would need is to be marshalled.


      Using the wrapper would be as simple as: MyPluginFunction(12, 8, "hello"); The above would map to the plugin's structure: private struct MyStruct { int a, b; string s; } Thus allowing the above function's 'myData' to be as follows: myData.a = 12, myData.b = 8, myData.s = "hello"


      Or if there's an easier way to do this, that could work too. Best regards, - Joe

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

      An array is already a reference type, so unless you have to marshal it as an IntPtr, just declare your P/Invoke method using the array of whatever Type is needed (if that's what the native API expects). Otherwise, you must get the address of it by fixing it in memory. See the fixed keyword in the .NET Framework SDK for more information. Another way to do this (basically, what fixed compiles down to) is to use the GCHandle to pin the object in memory so it doesn't get moved by the garbage collector (GC):

      GCHandle handle = GCHandle.Alloc(myObjArray);
      IntPtr ptr = handle.AddrOfPinnedObject();
      // Do something with ptr
      handle.Free();

      Microsoft MVP, Visual C# My Articles

      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