Skip to content

Commit 826583f

Browse files
Add documentation about DefaultCharSetAttribute (#26039)
Co-authored-by: Genevieve Warren <[email protected]>
1 parent f7f6399 commit 826583f

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed

docs/standard/native-interop/best-practices.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ The guidance in this section applies to all interop scenarios.
2525
|---------|---------|----------------|---------|
2626
| <xref:System.Runtime.InteropServices.DllImportAttribute.PreserveSig> | `true` | keep default | When this is explicitly set to false, failed HRESULT return values will be turned into exceptions (and the return value in the definition becomes null as a result).|
2727
| <xref:System.Runtime.InteropServices.DllImportAttribute.SetLastError> | `false` | depends on the API | Set this to true if the API uses GetLastError and use Marshal.GetLastWin32Error to get the value. If the API sets a condition that says it has an error, get the error before making other calls to avoid inadvertently having it overwritten.|
28-
| <xref:System.Runtime.InteropServices.DllImportAttribute.CharSet> | `CharSet.None`, which falls back to `CharSet.Ansi` behavior | Explicitly use `CharSet.Unicode` or `CharSet.Ansi` when strings or characters are present in the definition | This specifies marshaling behavior of strings and what `ExactSpelling` does when `false`. Note that `CharSet.Ansi` is actually UTF8 on Unix. _Most_ of the time Windows uses Unicode while Unix uses UTF8. See more information on the [documentation on charsets](./charset.md). |
28+
| <xref:System.Runtime.InteropServices.DllImportAttribute.CharSet> | Compiler-defined (specified in the [charset documentation](./charset.md)) | Explicitly use `CharSet.Unicode` or `CharSet.Ansi` when strings or characters are present in the definition | This specifies marshaling behavior of strings and what `ExactSpelling` does when `false`. Note that `CharSet.Ansi` is actually UTF8 on Unix. _Most_ of the time Windows uses Unicode while Unix uses UTF8. See more information on the [documentation on charsets](./charset.md). |
2929
| <xref:System.Runtime.InteropServices.DllImportAttribute.ExactSpelling> | `false` | `true` | Set this to true and gain a slight perf benefit as the runtime will not look for alternate function names with either an "A" or "W" suffix depending on the value of the `CharSet` setting ("A" for `CharSet.Ansi` and "W" for `CharSet.Unicode`). |
3030

3131
## String parameters

docs/standard/native-interop/charset.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ ms.date: 01/18/2019
66

77
# Charsets and marshaling
88

9-
The way `char` values, `string` objects, and `System.Text.StringBuilder` objects are marshaled depends on the value of the `CharSet` field on either the P/Invoke or structure. You can set the `CharSet` of a P/Invoke by setting the <xref:System.Runtime.InteropServices.DllImportAttribute.CharSet?displayProperty=nameWithType> field when declaring your P/Invoke. To set the `CharSet` for a type, set the <xref:System.Runtime.InteropServices.StructLayoutAttribute.CharSet?displayProperty=nameWithType> field on your class or struct declaration. When these attribute fields are not set, it is up to the language compiler to determine which `CharSet` to use. C# and Visual Basic use the <xref:System.Runtime.InteropServices.CharSet.Ansi> charset by default.
9+
The way `char` values, `string` objects, and `System.Text.StringBuilder` objects are marshaled depends on the value of the `CharSet` field on either the P/Invoke or structure. You can set the `CharSet` of a P/Invoke by setting the <xref:System.Runtime.InteropServices.DllImportAttribute.CharSet?displayProperty=nameWithType> field when declaring your P/Invoke. To set the `CharSet` for a type, set the <xref:System.Runtime.InteropServices.StructLayoutAttribute.CharSet?displayProperty=nameWithType> field on your class or struct declaration. When these attribute fields are not set, it is up to the language compiler to determine which `CharSet` to use. C#, Visual Basic, and F# use the <xref:System.Runtime.InteropServices.CharSet.None> charset by default, which has the same behavior as the <xref:System.Runtime.InteropServices.CharSet.Ansi> charset.
10+
11+
If the <xref:System.Runtime.InteropServices.DefaultCharSetAttribute?displayProperty=nameWithType> is applied on the module in C# or Visual Basic code, then the C# or Visual Basic compiler will emit the provided `CharSet` by default instead of using `CharSet.None`. F# does not support the `DefaultCharSetAttribute`, and always emits `CharSet.None` by default.
1012

1113
The following table shows a mapping between each charset and how a character or string is represented when marshaled with that charset:
1214

0 commit comments

Comments
 (0)