You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Defining how types should be marshaled via a P/Invoke or COM interop is difficult to get correct and many bad practices have been developed over the years. The Native interoperability best practices page should be converted into an analyzer to help developers do the right thing.
There is an existing P/Invoke analyzer, which could be a fitting home for some of this.
Some marshalling-related items that could be reasonable rules from the guidance doc:
Specify CharSet on DllImport when strings/characters are present
CA2101 seems to partly cover this (does string and StringBuilder, not char), although it is under Globalization(?) and the doc talks about security / partial trust
Prefer ExactSpelling=true on DllImport (Performance)
Do not use [Out] string (Interoperability)
Avoid StringBuilder parameters (Performance)
Specify marshalling for booleans (Interoperability)
CA1414 exists, but is disabled for Core (also only for p/invokes, so won't hit fields on types used in p/invokes)
Specify SizeConst when marshalling field as ByValArray (Interoperability)
Do not use Delegate or MulticastDelegate fields in marshalled structs (Interoperability).
Activity
danmoseley commentedon Apr 10, 2020
There may be more in
https://github.com/dotnet/runtime/blob/master/docs/coding-guidelines/interop-guidelines.md
https://github.com/dotnet/runtime/blob/master/docs/coding-guidelines/interop-pinvokes.md (which I'll merge into the former)
Of course, anything unique in those would be candidates for adding to the official doc as well.
elinor-fung commentedon Apr 21, 2020
There is an existing P/Invoke analyzer, which could be a fitting home for some of this.
Some marshalling-related items that could be reasonable rules from the guidance doc:
CharSetonDllImportwhen strings/characters are presentstringandStringBuilder, notchar), although it is under Globalization(?) and the doc talks about security / partial trustExactSpelling=trueonDllImport(Performance)[Out] string(Interoperability)StringBuilderparameters (Performance)SizeConstwhen marshalling field asByValArray(Interoperability)DelegateorMulticastDelegatefields in marshalled structs (Interoperability).AaronRobinsonMSFT commentedon Apr 23, 2020
@elinor-fung Here is another issue that should get a rule: #9944
JeremyKuhne commentedon May 5, 2020
Some initial thoughts:
[ComVisible]and[ComImport]on an interface should be incorrect, right?[MarshalAs]forstringandobjectas they are often notBSTRandVARIANTAaronRobinsonMSFT commentedon May 11, 2020
@elinor-fung Is this aggregated issue being handled by the various other issues being filed or should we keep this one open?
elinor-fung commentedon May 27, 2020
Closing this and using #37039 as the tracking issue that will point to the separate issues we file for each rule.