From 636a94498a51ad4487ea3c814d3788bccb4b9ef7 Mon Sep 17 00:00:00 2001 From: Aaron R Robinson Date: Wed, 9 Jul 2025 14:14:39 -0700 Subject: [PATCH 1/2] Add note about P/Invoke signature. --- docs/standard/native-interop/best-practices.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/standard/native-interop/best-practices.md b/docs/standard/native-interop/best-practices.md index f0872908032c8..93edb285533c0 100644 --- a/docs/standard/native-interop/best-practices.md +++ b/docs/standard/native-interop/best-practices.md @@ -13,6 +13,7 @@ The guidance in this section applies to all interop scenarios. - ✔️ DO use `[LibraryImport]`, if possible, when targeting .NET 7+. - There are cases when using `[DllImport]` is appropriate. A code analyzer with ID [SYSLIB1054](../../fundamentals/syslib-diagnostics/syslib1050-1069.md) tells you when that's the case. +- ✔️ DO define P/Invoke signatures that match the C export's argument count and use types that accurately represent each native type. - ✔️ DO use the same naming and capitalization for your methods and parameters as the native method you want to call. - ✔️ CONSIDER using the same naming and capitalization for constant values. - ✔️ DO use .NET types that map closest to the native type. For example, in C#, use `uint` when the native type is `unsigned int`. From e849f67a9e0a142bae9b0377db9f4e5a441bfe02 Mon Sep 17 00:00:00 2001 From: Aaron R Robinson Date: Wed, 9 Jul 2025 15:32:39 -0700 Subject: [PATCH 2/2] Feedback --- docs/standard/native-interop/best-practices.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/standard/native-interop/best-practices.md b/docs/standard/native-interop/best-practices.md index 93edb285533c0..58f890fb397f3 100644 --- a/docs/standard/native-interop/best-practices.md +++ b/docs/standard/native-interop/best-practices.md @@ -1,7 +1,7 @@ --- title: Native interoperability best practices - .NET description: Learn the best practices for interfacing with native components in .NET. -ms.date: 04/08/2024 +ms.date: 07/09/2025 --- # Native interoperability best practices @@ -13,9 +13,9 @@ The guidance in this section applies to all interop scenarios. - ✔️ DO use `[LibraryImport]`, if possible, when targeting .NET 7+. - There are cases when using `[DllImport]` is appropriate. A code analyzer with ID [SYSLIB1054](../../fundamentals/syslib-diagnostics/syslib1050-1069.md) tells you when that's the case. -- ✔️ DO define P/Invoke signatures that match the C export's argument count and use types that accurately represent each native type. - ✔️ DO use the same naming and capitalization for your methods and parameters as the native method you want to call. - ✔️ CONSIDER using the same naming and capitalization for constant values. +- ✔️ DO define P/Invoke and function pointer signatures that match the C function's arguments. - ✔️ DO use .NET types that map closest to the native type. For example, in C#, use `uint` when the native type is `unsigned int`. - ✔️ DO prefer expressing higher level native types using .NET structs rather than classes. - ✔️ DO prefer using function pointers, as opposed to `Delegate` types, when passing callbacks to unmanaged functions in C#.