在尝试编写与此问题相关的自定义封送程序(P/Invoke从C到C#而不知道数组的大小)时,我遇到了一些我无法理解的问题.这是我写的第一个定制封送器,所以毫无疑问,由于我的无知,我错过了一些明显的东西.
这是我的C#代码:
using System;
using System.Runtime.InteropServices;
using System.Text;
namespace CustomMarshaler
{
public class MyCustomMarshaler : ICustomMarshaler
{
static MyCustomMarshaler static_instance;
public IntPtr MarshalManagedToNative(object managedObj)
{
if (managedObj == null)
return IntPtr.Zero;
if (!(managedObj is int[]))
throw new MarshalDirectiveException("VariableLengthArrayMarshaler must be used on an int array.");
int[] arr = (int[])managedObj;
int size = sizeof(int) + arr.Length * sizeof(int);
IntPtr pNativeData = Marshal.AllocHGlobal(size);
Marshal.WriteInt32(pNativeData, arr.Length);
Marshal.Copy(arr, 0, pNativeData + sizeof(int), arr.Length);
return pNativeData;
}
public object MarshalNativeToManaged(IntPtr pNativeData)
{
int len = Marshal.ReadInt32(pNativeData);
int[] arr = new int[len];
Marshal.Copy(pNativeData + sizeof(int), arr, 0, len);
return arr;
}
public void CleanUpNativeData(IntPtr pNativeData)
{
Marshal.FreeHGlobal(pNativeData);
}
public void CleanUpManagedData(object managedObj)
{
}
public int GetNativeDataSize()
{
return -1;
}
public static ICustomMarshaler GetInstance(string cookie)
{
if (static_instance == null)
{
return static_instance = new MyCustomMarshaler();
}
return static_instance;
}
}
class Program
{
[DllImport(@"MyLib.dll")]
private static extern void Foo(
[In, Out, MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(MyCustomMarshaler))]
int[] arr
);
static void Main(string[] args)
{
int[] colorTable = new int[] { 1, 2, 3, 6, 12 };
Foo(colorTable);
foreach (int value in colorTable)
Console.WriteLine(value);
}
}
}
Run Code Online (Sandbox Code Playgroud)
另一方面是一个简单的本机DLL,用Delphi编写.
library MyLib;
procedure Foo(P: PInteger); stdcall;
var
i, len: Integer;
begin
len := P^;
Writeln(len);
for i := 1 to len do begin
inc(P);
Writeln(P^);
inc(P^);
end;
end;
exports
Foo;
begin
end.
Run Code Online (Sandbox Code Playgroud)
想法是将数组传递给DLL,然后打印出长度字段和数组的值.本机代码还将数组的每个值递增1.
所以,我希望看到这个输出:
5 1 2 3 6 12 2 3 4 7 13
但不幸的是我看到了这个输出:
5 1 2 3 6 12 1 2 3 6 12
在调试器下,我可以看到它MarshalNativeToManaged正在执行,并且它返回的值已经递增.但是这些递增的值没有找到返回传递给的对象的方法Foo.
我需要做些什么来解决这个问题?
多年前我遇到过类似的问题,发现自定义封送的文档很少.我怀疑使用ICustomMarshaler从未真正起飞,因为它总是可以在常规代码中使用手动编组来完成.因此,从来没有真正需要任何高级自定义编组方案的文档.
无论如何,通过各种来源和许多反复试验,我想我对大多数自定义Marshaling的工作原理进行了实践性的理解.
在您的情况下,您已为[In]编组正确设置ManagedToNative方法,并为大多数[Out]编组正确设置NativeToManaged方法,但[In,Out]编组实际上有点棘手.[In,Out] marshaling实际上是就地编组.因此,在退出的过程中,您必须将数据封送回操作的[In]侧提供的同一实例.
根据使用引用或值类型,调用是普通的pInvoke调用还是委托的回调等,这有很多小的变化.但是考虑什么需要结束在哪里才是关键.
您的代码的以下变体以您希望的方式工作(并且它似乎以.Net 2.0及更高版本的方式工作):
//This must be thread static since, in theory, the marshaled
//call could be executed simultaneously on two or more threads.
[ThreadStatic] int[] marshaledObject;
public IntPtr MarshalManagedToNative(object managedObj)
{
if (managedObj == null)
return IntPtr.Zero;
if (!(managedObj is int[]))
throw new MarshalDirectiveException("VariableLengthArrayMarshaler must be used on an int array.");
//This is called on the way in so we must keep a reference to
//the original object so we can marshal to it on the way out.
marshaledObject = (int[])managedObj;
int size = sizeof(int) + marshaledObject.Length * sizeof(int);
IntPtr pNativeData = Marshal.AllocHGlobal(size);
Marshal.WriteInt32(pNativeData, marshaledObject.Length);
Marshal.Copy(marshaledObject, 0, (IntPtr)(pNativeData.ToInt64() + sizeof(int)), marshaledObject.Length);
return pNativeData;
}
public object MarshalNativeToManaged(IntPtr pNativeData)
{
if (marshaledObject == null)
throw new MarshalDirectiveException("This marshaler can only be used for in-place ([In. Out]) marshaling.");
int len = Marshal.ReadInt32(pNativeData);
if (marshaledObject.Length != len)
throw new MarshalDirectiveException("The size of the array cannot be changed when using in-place marshaling.");
Marshal.Copy((IntPtr)(pNativeData.ToInt64() + sizeof(int)), marshaledObject, 0, len);
//Reset to null for next call;
marshalledObject = null;
return marshaledObject;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5809 次 |
| 最近记录: |