Skip to content

Add snippet and add snippet to rule #31402

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions docs/fundamentals/code-analysis/quality-rules/ca2101.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ f1_keywords:
helpviewer_keywords:
- CA2101
- SpecifyMarshalingForPInvokeStringArguments
author: gewarren
ms.author: gewarren
dev_langs:
- CSharp
- VB
---
# CA2101: Specify marshalling for P/Invoke string arguments

Expand Down Expand Up @@ -46,3 +47,5 @@ Do not suppress a warning from this rule.
The following example shows a method that violates this rule, and then shows how to fix the violation.

:::code language="csharp" source="snippets/csharp/all-rules/ca2101.cs" id="snippet1":::

:::code language="vb" source="snippets/vb/all-rules/ca2101.vb" id="snippet2":::
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Imports System
Imports System.Runtime.InteropServices

Namespace ca2101
'<snippet2>
Friend Class NativeMethods
' Violates rule: SpecifyMarshalingForPInvokeStringArguments.
<DllImport("advapi32.dll", CharSet:=CharSet.Auto)>
Friend Shared Function RegCreateKey(ByVal key As IntPtr, ByVal subKey As String, <Out> ByRef result As IntPtr) As Integer
End Function

' Satisfies rule: SpecifyMarshalingForPInvokeStringArguments.
<DllImport("advapi32.dll", CharSet:=CharSet.Unicode)>
Friend Shared Function RegCreateKey2(ByVal key As IntPtr, ByVal subKey As String, <Out> ByRef result As IntPtr) As Integer
End Function
End Class
'</snippet2>
End Namespace