简体   繁体   English

托管代码中的指针? C#

[英]Pointer in managed Code? C#

I use C#.net. 我使用C#.net。

These is my method now: 这些是我现在的方法:

    [DllImport(DLLPath, CallingConvention = CallingConvention.Cdecl)]
    unsafe public extern static int AMRecoveryModeDeviceReboot(AMRecoveryDevice device, byte[] paramByte, int u1, int u2, int u3)

I must have a Pointer in it, the AMRecoveryDevice is a struct: 我必须有一个指针,AMRecoveryDevice是一个结构:

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
internal struct AMRecoveryDevice
{
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
    public byte[] unknown0;      /* 0 */
    public DeviceRestoreNotificationCallback callback;    /* 8 */
    public IntPtr user_info;      /* 12 */
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 12)]
    public byte[] unknown1;      /* 16 */
    public uint readwrite_pipe;    /* 28 */
    public byte read_pipe;          /* 32 */
    public byte write_ctrl_pipe;    /* 33 */
    public byte read_unknown_pipe;  /* 34 */
    public byte write_file_pipe;    /* 35 */
    public byte write_input_pipe;   /* 36 */
};

Now I must have AMRecoveryDevice as a Pointer in the first method, but then it gives an error. 现在我必须在第一种方法中将AMRecoveryDevice作为指针,但它会产生错误。 Is it impossible? 这不可能吗?

Use ref in method declaration: 在方法声明中使用ref:

[DllImport(DLLPath, CallingConvention = CallingConvention.Cdecl)]
public extern static int AMRecoveryModeDeviceReboot(
    ref AMRecoveryDevice device,
    byte[] paramByte,
    int u1,
    int u2,
    int u3)

Make device a ref parameter: 使device一个ref参数:

[DllImport(DLLPath, CallingConvention = CallingConvention.Cdecl)]
unsafe public extern static int AMRecoveryModeDeviceReboot(
    ref AMRecoveryDevice device, 
    byte[] paramByte, 
    int u1, 
    int u2, 
    int u3)

A good article on how to pass data to P/Invoke calls is this one from MSDN Magazine: 有关如何将数据传递给P / Invoke调用的好文章来自MSDN杂志:

Jason Clark: P/Invoke Revisited Jason Clark:P / Invoke Revisited

Leaving everything else the same, you could just change the struct to class . 将其他所有内容保持不变,您只需将struct更改为class

As you have specified sequential layout, this will behave just like a pointer to a struct. 正如您指定的顺序布局,这将表现得像指向结构的指针。

IOW: IOW:

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
internal class AMRecoveryDevice
{
  ...
}    

...

[DllImport(DLLPath, CallingConvention = CallingConvention.Cdecl)]
extern static int AMRecoveryModeDeviceReboot(AMRecoveryDevice device, 
   byte[] paramByte, int u1, int u2, int u3)

The pattern I usually use is to make the P/invoke declaration private and use IntPtr in place of structs. 我通常使用的模式是将P / invoke声明设为私有,并使用IntPtr代替结构。 Provide a public method to handle the marshalling. 提供处理编组的公共方法。 (You can also get rid of unsafe this way.) (你也可以通过这种方式摆脱不安全感。)

[DllImport(DLLPath, CallingConvention = CallingConvention.Cdecl)]
private extern static int AMRecoveryModeDeviceReboot(IntPtr device, byte[] paramByte, int u1, int u2, int u3)

public static int AMRecoveryModeDevice(ref AMRecoveryDevice device, byte[] paramByte, int u1, int u2, int u3) {
    var ptr = Marshal.AllocHGlobal(Marshal.SizeOf(device));
    Marshal.StructureToPointer(device, ptr, false);
    int result = AMRecoveryModeDeviceReboot(ptr, paramByte, u1, u2, u3);
    device = (AMRecoveryDevice)Marshal.PtrToStructure(ptr, typeof(AMRecoveryDevice));
    Marshal.FreeHGlobal(ptr);
    return result;
}

For your AMRecoveryDevice structure, you should use an IntPtr for the callback delegate too. 对于AMRecoveryDevice结构,您也应该使用IntPtr作为回调委托。

[MarshalAs(UnmanagedType.FunctionPtr)]
private IntPtr _callback;    /* 8 */
public DeviceRestoreNotificationCallback callback {
    get { return (DeviceRestoreNotificationCallback)Marsal.GetDelagateFromFunctionPointer(_callback, typeof(DeviceRestoreNotificationCallback)); }
    set { _calback = Marshal.GetFunctionPointerFromDelegate(value); }
}

Not if you work in an unsafe context. 如果你在不安全的环境中工作,那就没有了。 See: http://msdn.microsoft.com/en-us/library/chfa2zb8(VS.71).aspx 请参阅: http//msdn.microsoft.com/en-us/library/chfa2zb8(VS.71).aspx

It's not recommended in managed apps though. 但是不建议在托管应用程序中使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM