Pointer in managed code? WITH#

I am using C # .net.

Now this is my method:

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

I should have a pointer, AMRecoveryDevice is a structure:

[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 should have AMRecoveryDevice as a pointer in the first method, but then it gives an error. It's impossible?

+3
source share
5 answers

Use ref in the method declaration:

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

Make parameter devicea 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 post from the MSDN log:

Jason Clark: P / Invoke Revisited

+4
source

, struct class.

, , .

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)
+2

I usually use a template to make the P / invoke declaration private and use IntPtrstructs instead. Provide a publicly accessible marshalling method. (You can also get rid of unsafe ones 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 the AMRecoveryDevice structure, you must also use IntPtr for the callback delegate.

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

Not if you work in an insecure context. See: http://msdn.microsoft.com/en-us/library/chfa2zb8(VS.71).aspx

This is not recommended in managed applications.

0
source

Source: https://habr.com/ru/post/1754970/


All Articles