Marshal an object [] to ptr
-
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
-
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
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 thefixed
keyword in the .NET Framework SDK for more information. Another way to do this (basically, whatfixed
compiles down to) is to use theGCHandle
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