Compartir a través de


CA1415: Declare los elementos P/Invoke correctamente

Nombre de tipo

DeclarePInvokesCorrectly

Identificador de comprobación

CA1415

Categoría

Microsoft.Interoperability

Cambio problemático

No problemático: si la funcionalidad P/Invoke que declara el parámetro no se puede ver fuera del ensamblado.Problemático: si la funcionalidad P/Invoke que declara el parámetro se puede ver fuera del ensamblado.

Motivo

Se declara un método de invocación de plataforma incorrectamente.

Descripción de la regla

Un método de invocación de plataforma tiene acceso al código no administrado y se define utilizando la palabra clave Declare en Visual Basic o el atributo System.Runtime.InteropServices.DllImportAttribute.En la actualidad, esta regla busca declaraciones de método de invocación de plataforma dirigidas a funciones de Win32 que tengan un puntero a un parámetro de estructura OVERLAPPED y el parámetro administrado correspondiente no es un puntero para una estructura System.Threading.NativeOverlapped.

Cómo corregir infracciones

Para corregir una infracción de esta regla, debe declarar correctamente el método de invocación de plataforma.

Cuándo suprimir advertencias

No suprima las advertencias de esta regla.

Ejemplo

El ejemplo siguiente muestra métodos de invocación de plataforma que infringen y que cumplen la regla.

using System;
using System.Runtime.InteropServices;
using System.Threading;

namespace InteroperabilityLibrary
{
    // The platform invoke methods in this class violate the rule.
    [ComVisible(true)]
    internal class NativeMethods
    {
        private NativeMethods() { }

        [DllImport("kernel32.dll", SetLastError = true)]
        internal extern static uint ReadFile(
           IntPtr hFile, IntPtr lpBuffer, int nNumberOfBytesToRead,
           IntPtr lpNumberOfBytesRead, IntPtr overlapped);

        [DllImport("kernel32.dll", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        internal extern static bool ReadFileEx(
           IntPtr hFile, IntPtr lpBuffer, int nNumberOfBytesToRead,
           NativeOverlapped overlapped, IntPtr lpCompletionRoutine);
    }

    // The platform invoke methods in this class satisfy the rule.
    [ComVisible(true)]
    internal class UnsafeNativeMethods
    {
        private UnsafeNativeMethods() { }

        //To compile this code, uncomment these lines and compile
        //with "/unsafe".
        //[DllImport("kernel32.dll", SetLastError = true)]
        //unsafe internal extern static uint ReadFile(
        //   IntPtr hFile, IntPtr lpBuffer, int nNumberOfBytesToRead,
        //   IntPtr lpNumberOfBytesRead, NativeOverlapped* overlapped);

        //[DllImport("kernel32.dll", SetLastError = true)]
        //[return: MarshalAs(UnmanagedType.Bool)]
        //unsafe internal extern static bool ReadFileEx(
        //   IntPtr hFile, IntPtr lpBuffer, int nNumberOfBytesToRead,
        //   NativeOverlapped* overlapped, IntPtr lpCompletionRoutine);
    }
}

Vea también

Otros recursos

Interoperar con código no administrado