Netcf Как пропустить структуру с помощью reftedioocontrol

StackOverflow https://stackoverflow.com//questions/10649480

  •  11-12-2019
  •  | 
  •  

Вопрос

Я новый к .NET Compact Framework.Мне нужно позвонить в функцию dewaysiOcontrol и пропускать структуры как параметры ввода и вывода к функции iocontrol.

в pinvoke / deviceiocontrol Я нашел, как получить доступ к самой функцииОтказНо как я могу пройти указатель структуры в виде GenSodicetagCode и параметра InBuf?

Устройствооконтрол определяется как p / iscoke:

[DllImport("coredll", EntryPoint = "DeviceIoControl", SetLastError = true)]
  internal static extern int DeviceIoControlCE(
    int hDevice, int dwIoControlCode,
    byte[] lpInBuffer, int nInBufferSize,
    byte[] lpOutBuffer, int nOutBufferSize,
    ref int lpBytesReturned, IntPtr lpOverlapped);
.

Обеспечение структуры У этого макета:

struct Query
{
  int a;
  int b;
  char x[8];
}

struct Response
{
  int result;
  uint32 success;
}

void DoIoControl ()
{
  Query q = new Query();
  Response r = new Response();
  int inSize = System.Runtime.InteropServices.Marshal.SizeOf(q);
  int outSize = System.Runtime.InteropServices.Marshal.SizeOf(r);
  NativeMethods.DeviceIoControlCE((int)handle, (int)IOCTL_MY.CODE,
    ref q, inSize, ref r, outSize, ref bytesReturned, IntPtr.Zero);   
}
.

Редактировать : Когда я пытаюсь компилировать этот код, я получаю ошибку:

cannot convert from 'ref MyNamespace.Response' to 'byte[]'
.

Как я могу пройти адрес структуры в функцию DeviceiOcontrol, что ожидает указателя на байт вместо Struct Ref?

Это было полезно?

Решение

The issue is that your P/Invoke declaration doesn't match your call. DeviceIoControl takes in pointers for the in/out paramters:

BOOL DeviceIoControl(
  HANDLE hDevice, 
  DWORD dwIoControlCode, 
  LPVOID lpInBuffer, 
  DWORD nInBufferSize, 
  LPVOID lpOutBuffer, 
  DWORD nOutBufferSize, 
  LPDWORD lpBytesReturned, 
  LPOVERLAPPED lpOverlapped
);

So you can "adjust" your declaration in a lot of ways. The one in the link you provide uses a byte[] probably for convenience where they were using it. In your case, since you're passing simple structs (i.e. no internal pointers to other data), then the easiest "fix" is to just change you P/Invoke declaration:

[DllImport("coredll", SetLastError = true)]    
internal static extern int DeviceIoControl(    
    IntPtr hDevice, 
    IOCTL.MY dwIoControlCode,    
    ref Query lpInBuffer,
    int nInBufferSize,    
    ref Response lpOutBuffer, 
    int nOutBufferSize,    
    ref int lpBytesReturned, 
    IntPtr lpOverlapped);    

And you code should work. Note I also changed the types of the first two parameters to allow making your calling code more clear without casts.

EDIT 2

If you find you need different signatures, simply overload the P/Invoke. For example, the Smart Device Framework code has at least 11 overloads for DeviceIoControl. Here are just some of them to give you a flavor:

    [DllImport("coredll.dll", EntryPoint = "DeviceIoControl", SetLastError = true)]
    internal static extern int DeviceIoControl<TInput, TOutput>(
        IntPtr hDevice,
        uint dwIoControlCode,
        ref TInput lpInBuffer,
        int nInBufferSize,
        ref TOutput lpOutBuffer,
        int nOutBufferSize,
        out int lpBytesReturned,
        IntPtr lpOverlapped)
        where TInput : struct
        where TOutput : struct;

    [DllImport("coredll.dll", EntryPoint = "DeviceIoControl", SetLastError = true)]
    internal unsafe static extern int DeviceIoControl(
        IntPtr hDevice,
        uint dwIoControlCode,
        void* lpInBuffer,
        int nInBufferSize,
        void* lpOutBuffer,
        int nOutBufferSize,
        out int lpBytesReturned,
        IntPtr lpOverlapped);

    [DllImport("coredll.dll", EntryPoint = "DeviceIoControl", SetLastError = true)]
    internal static extern int DeviceIoControl(
        IntPtr hDevice,
        uint dwIoControlCode,
        IntPtr lpInBuffer,
        uint nInBufferSize,
        IntPtr lpOutBuffer,
        uint nOutBufferSize,
        out int lpBytesReturned,
        IntPtr lpOverlapped);

    [DllImport("coredll.dll", EntryPoint = "DeviceIoControl", SetLastError = true)]
    internal static extern int DeviceIoControl(
        IntPtr hDevice,
        uint dwIoControlCode,
        byte[] lpInBuffer,
        int nInBufferSize,
        IntPtr lpOutBuffer,
        int nOutBufferSize,
        out int lpBytesReturned,
        IntPtr lpOverlapped);
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top