CA1415: P/Invoke を正しく宣言します
Item | [値] |
---|---|
規則 ID | CA1415 |
カテゴリ | Microsoft.Interoperability |
互換性に影響する変更点 | 中断なし - パラメーターを宣言する P/Invoke がアセンブリの外で表示されない場合。 中断あり - パラメーターを宣言する P/Invoke がアセンブリの外で表示される場合。 |
原因
プラットフォーム呼び出しメソッドが正しく宣言されていません。
規則の説明
プラットフォーム呼び出しメソッドは、アンマネージド コードにアクセスし、Visual Basic の Declare
キーワードまたは System.Runtime.InteropServices.DllImportAttribute を使用して定義されます。 現在、この規則は、構造体パラメーターへのポインターを持ち、対応するマネージド パラメーターが構造体への 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);
}
}
関連項目
フィードバック
https://aka.ms/ContentUserFeedback」を参照してください。
以下は間もなく提供いたします。2024 年を通じて、コンテンツのフィードバック メカニズムとして GitHub の issue を段階的に廃止し、新しいフィードバック システムに置き換えます。 詳細については、「フィードバックの送信と表示