CA1415: P-Invoke를 올바르게 선언합니다.

항목
RuleId CA1415
범주 Microsoft.Interoperability
주요 변경 내용 호환성이 손상되지 않는 변경 - 매개 변수를 선언하는 P/Invoke를 어셈블리 외부에서 볼 수 없는 경우 호환성이 손상되는 변경 - 매개 변수를 선언하는 P/Invoke를 어셈블리 외부에서 볼 수 있는 경우

원인

플랫폼 호출 메서드가 잘못 선언되었습니다.

규칙 설명

플랫폼 호출 메서드는 관리되지 않는 코드에 액세스하고 Visual Basic 또는 System.Runtime.InteropServices.DllImportAttributeDeclare />의 키워드(keyword) 사용하여 정의됩니다. 현재 이 규칙은 OVERLAPPED 구조체 매개 변수에 대한 포인터가 있는 Win32 함수를 대상으로 하는 플랫폼 호출 메서드 선언을 찾는데 해당하는 관리형 매개 변수가 System.Threading.NativeOverlapped 구조체에 대한 포인터가 아닙니다.

위반 문제를 해결하는 방법

이 규칙의 위반 문제를 해결하려면 플랫폼 호출 메서드를 올바르게 선언합니다.

경고를 표시하지 않는 경우

이 규칙에서는 경고를 표시해야 합니다.

예시

다음 예제에서는 규칙을 위반하는 플랫폼 호출 메서드와 규칙을 충족하는 플랫폼 호출 메서드를 보여 줍니다.

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);
    }
}

참고 항목

비관리 코드와의 상호 운용