-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5633 from MicrosoftDocs/master637314671443344459
For protected CLA branch, push strategy should use PR and merge to target branch method to work around git push error
- Loading branch information
Showing
7 changed files
with
70 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
--- | ||
title: "CA1417: Do not use OutAttribute on string parameters for P/Invokes" | ||
ms.date: 07/20/2020 | ||
ms.topic: reference | ||
f1_keywords: | ||
- "DoNotUseOutAttributeStringPInvokeParameters" | ||
- "CA1417" | ||
helpviewer_keywords: | ||
- "DoNotUseOutAttributeStringPInvokeParameters" | ||
- "CA1417" | ||
author: elinor-fung | ||
ms.author: elfung | ||
manager: jeffschw | ||
ms.workload: | ||
- "multiple" | ||
--- | ||
# CA1417: Do not use `OutAttribute` on string parameters for P/Invokes | ||
|
||
|Item|Value| | ||
|-|-| | ||
|CheckId|CA1417| | ||
|Category|Microsoft.Interoperability| | ||
|Breaking change|Non-breaking| | ||
|
||
## Cause | ||
|
||
A [P/Invoke](/dotnet/standard/native-interop/pinvoke) string parameter is passed by value and marked with | ||
<xref:System.Runtime.InteropServices.OutAttribute>. | ||
|
||
## Rule description | ||
|
||
The .NET runtime automatically performs [string interning](/dotnet/api/system.string.intern#remarks). If an interned string marked with <xref:System.Runtime.InteropServices.OutAttribute> is passed by value to a P/Invoke, the runtime can be destabilized. | ||
|
||
## How to fix violations | ||
|
||
If marshaling of modified string data back to the caller is required, pass the string by reference instead. Otherwise, the <xref:System.Runtime.InteropServices.OutAttribute> can be removed without any other changes. | ||
|
||
```csharp | ||
// Violation | ||
[DllImport("MyLibrary")] | ||
private static extern void Foo([Out] string s); | ||
|
||
// Fixed: passed by reference | ||
[DllImport("MyLibrary")] | ||
private static extern void Foo(out string s); | ||
|
||
// Fixed: marshaling data back to caller is not required | ||
[DllImport("MyLibrary")] | ||
private static extern void Foo(string s); | ||
``` | ||
|
||
## When to suppress warnings | ||
|
||
It is not safe to suppress a warning from this rule. | ||
|
||
## See also | ||
|
||
- [Interoperability warnings](interoperability-warnings.md) | ||
- [Native interoperability best practices](/dotnet/standard/native-interop/best-practices) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters