Runtime: Properly factor DllImports into Common\src\Interop

Created on 30 Nov 2018  路  18Comments  路  Source: dotnet/runtime

We now have a bunch of libraries with their own DllImports, whereas the expected approach is that all DllImports in product src should be included from src\Common\src\Interop, appropriately factored by platform and library, and then those source files included into the relevant assemblies that need them.

  • [ ] Microsoft.Diagnostics.Tracing.EventSource.Redist
  • [ ] System.Data.SqlClient
  • [x] System.Diagnostics.EventLog
  • [ ] System.DirectoryServices
  • [ ] System.DirectoryServices.AccountManagement
  • [ ] System.DirectoryServices.Protocols
  • [ ] System.Drawing.Common
  • [x] System.IO.Compression
  • [ ] System.IO.Compression.Brotli
  • [ ] System.IO.Ports
  • [x] System.Management
  • [ ] System.Reflection.Metadata
  • [x] System.Runtime.Caching
  • [x] System.Security.Cryptography.Csp
  • [x] System.Security.Cryptography.Pkcs
  • [ ] System.Security.Cryptography.X509Certificates

This all needs to be refactored and dedup'd.

https://github.com/dotnet/corefx/blob/master/Documentation/coding-guidelines/interop-guidelines.md

area-Meta easy enhancement up-for-grabs

Most helpful comment

Thanks, @Marusyk! That looks very good. Please go ahead and create a PR for it so it can be properly reviewed and merged.

should it be like one big PR or a few small ones?

One per library (e.g. EventLog) or family of libraries (e.g. System.DirectoryServices.*) would be good.

All 18 comments

Hello, I will try to prepare PR for this, if nobody is against it

@Marusyk, absolutely, thanks. I'd suggest starting with either EventLog, System.DirectoryServices., or System.Security..

Hello @stephentoub,

Could you please check if I am on the right way?
I start with EventLog

And one more question: should it be like one big PR or a few small ones?

Thanks, @Marusyk! That looks very good. Please go ahead and create a PR for it so it can be properly reviewed and merged.

should it be like one big PR or a few small ones?

One per library (e.g. EventLog) or family of libraries (e.g. System.DirectoryServices.*) would be good.

Can I take System.DirectoryServices?

I have a couple of questions. As I can see there are a lot of [DllImport] for functions that are currently presented in src/Common/Interop, e.g. LoadLibrary, GetProcAddress from "kernel32.dll" are duplicated. Also there is an internal class LoadLibrarySafeHandle which is a duplicate of Common\CoreLib\Microsoft\Win32\SafeHandles\SafeLibraryHandle.cs
So our steps are:

  1. Remove duplicates from System.DirectoryServices and use methods and classes from src/Common.
  2. Add non-existing methods to src/Common and remove them from System.DirectoryServices
  3. If current DllImport method from src/Common/Interop doesn't have SetLastError = true and System.DirectoryServices needs it, we have to modify Interop\Windows\kernel32\Interop.GetProcAddress.cs (because it is used to throw an exception e.g. in GetProcAddress with GetLastWin32Error)

If I am right with these assumptions, could you please take a look at Refactored LoadLibrary, FreeLibrary, GetProcAddress
using CommonInterop = Interop is a temporary fix while we have Interop namespace in the project. I will remove it later.

And another question: what are the differences between Common/src/Interop/Windows and Common/src/CoreLib/Interop/Windows?
Some methods are duplicated: Interop.FreeLibrary in Common/src/Interop/Windows and Interop.FreeLibrary in Common/src/CoreLib/Interop/Windows Which one should we use: from Common/src/Interop/Windows?
And what if there is no method in Common/src/Interop/Windows, but it exists in Common/src/CoreLib/Interop/Windows (e.g. CloseHandle) Can we reference this file?

Common/src/CoreLib/Interop/Windows are mirrored from src/System.Private.Corelib/shared/Interop/Windows in the CoreCLR repo. Files there are in CoreCLR presumably because some code in System.Private.Corelib needs them. We mirror all of src/System.Private.Corelib/shared from the CoreCLR repo so that any code in CoreFX can reuse it if it needs to. This falls in this category -- if there is duplication, then generally we would want to keep the corelib copy, and change CoreFX code to use that one, and delete the CoreFX copy. You can see examples of using files out of CoreFX's `Common/src/CoreLib in various project files in CoreFX.

Hi! Can I take a crack at System.DirectoryServices.Protocols? There's a lot of them (~65), but it looks like it's mostly elbow grease.

@frbncis sure - but we should take care, as our test coverage of that is mostly manual IIRC.

Hello there. Can I take System.Security.*?

Also it would be nice to update the checkboxes in the issue to mark what was already done.

Ping @danmosemsft, @stephentoub

@satano thanks for the offer, please do. Are checkboxes correct now?

I did not look into the sources, but according to the merged PR names, the checkboxes are correct. So I will look into System.Security.Cryptography.X509Certificates.

OK!

@stephentoub I would like to contribute to this issue. Will you please guide me about where to start? What is pending?

hi all, @stephentoub! can I take System.Reflection.Metadata & System.Drawing.Common?

Sure go ahead!

Will you please guide me about where to start? What is pending?

I've not recently audited progress here to see what's outstanding. Basically you'd be looking for anywhere there are DllImports in product src not under the common interop folders. Contributions are welcome :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

matty-hall picture matty-hall  路  3Comments

jkotas picture jkotas  路  3Comments

noahfalk picture noahfalk  路  3Comments

bencz picture bencz  路  3Comments

btecu picture btecu  路  3Comments