Skip to content
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

Code Quality: Replace Vanara with CsWin32 #15000

Open
gumbarros opened this issue Mar 19, 2024 · 9 comments · May be fixed by #17002
Open

Code Quality: Replace Vanara with CsWin32 #15000

gumbarros opened this issue Mar 19, 2024 · 9 comments · May be fixed by #17002
Labels
help wanted Extra attention is needed
Milestone

Comments

@gumbarros
Copy link
Contributor

gumbarros commented Mar 19, 2024

Description

We're working on utilizing Native Aot in the application, however, runtime marshaler is one of the blocker against this. Vanara uses loads of runtime marshaler via DllImport and ComImport and we're working on replacing it with CsWin32, ultimately removing its reference completely.

Concerned code

  • Vanara references
  • DllImport we explicitly use within the codebase

Gains

  • AoT compliant codebase
  • Less 3rd party references

Requirements

  • Replace DllImport with CsWin32

Comments

CsWin32 requires you to add interfaces, functions, constants, enums, snd etc in NativeMethods.txt placed in Files.App.CsWin32. #define'd constants also need to be written in that text file and use like "PInvoke.CMF_DEFAULT" (They don't get placed in a particular namespace).
For more info, download ILSpy and win32metadata.winmd in order to see the full projections.

@gumbarros
Copy link
Contributor Author

https://learn.microsoft.com/en-us/dotnet/standard/native-interop/pinvoke-source-generation

Another reference explaining the benefits. I think this is an important issue.

@0x5bfa
Copy link
Member

0x5bfa commented Mar 26, 2024

I like this idea. Feel free to work on it with @yaira2’s approval.

@yaira2
Copy link
Member

yaira2 commented Mar 26, 2024

Sounds good, it might be worth splitting this into multiple PRs to make it easier to test and review.

@yaira2 yaira2 moved this from 🆕 New to 📋 Planning stage in Files task board Mar 26, 2024
@yaira2 yaira2 moved this from 📋 Planning stage to 🔖 Ready to build in Files task board Mar 26, 2024
@0x5bfa

This comment was marked as outdated.

@gumbarros
Copy link
Contributor Author

Just checked about CsWin32, looks promising because of source generator usage like LibraryImportAttribute. Will make a try tonight at NativeFindStorageItemHelper.cs

@gumbarros gumbarros changed the title Code Quality: SYSLIB1054 - Use LibraryImportAttribute instead of DllImportAttribute to generate p/invoke marshalling code at compile time. Code Quality: SYSLIB1054 - Use CsWin32 instead of DllImportAttribute to generate p/invoke marshalling code at compile time. Mar 28, 2024
@gumbarros
Copy link
Contributor Author

Don't know if this is expected behavior when migrating:
microsoft/CsWin32#1167

@0x5bfa

This comment was marked as outdated.

@0x5bfa

This comment was marked as outdated.

@Josh65-2201 Josh65-2201 moved this from 🔖 Ready to build to 🏗 In progress in Files task board Apr 7, 2024
@Josh65-2201 Josh65-2201 changed the title Code Quality: SYSLIB1054 - Use CsWin32 instead of DllImportAttribute to generate p/invoke marshalling code at compile time. Code Quality: Replace Vanara with CsWin32 Jul 6, 2024
@0x5bfa
Copy link
Member

0x5bfa commented Oct 10, 2024

Important

  • Please Use CsWin32 instead of replacing with LibraryImport. This Win32PInvoke class should be going to contain undocumented native functions defined through LibraryImport or DllImport when possible.
  • Don't use "*FromApp" functions. They are intended to be used on UWP. Instead, use native ones.
  • E is easy, M is medium, D is difficult. If anyone else can take a look, it'd be good to start from ones marked as E.

Documented native methods (targets to convert to CsWin32)

  • M: RmRegisterResources
  • M: RmStartSession
  • E: RmEndSession
  • D: RmGetList
  • E: SetPropW
  • E: CreateEvent
  • E: SetEvent
  • E: GetDpiForWindow
  • M: CoWaitForMultipleObjects
  • E: SetWindowLongPtr32
  • E: SetWindowLongPtr64
  • E: SHBrowseForFolder
  • E: SHGetPathFromIDList
  • E: CloseHandle
  • M: GetOverlappedResult
  • E: CancelIo
  • E: CancelIoEx
  • M: WaitForMultipleObjectsEx
  • E: ResetEvent
  • E: WaitForSingleObjectEx
  • M: ReadDirectoryChangesW
  • E: CreateFileFromAppW
  • M: DeviceIoControl
  • M: ToUnicode
  • M: ToUnicodeEx
  • E: GetKeyboardState
  • E: GetKeyboardLayout
  • E: MapVirtualKey
  • M: TranslateMessage
  • E: CreateFileFromApp
  • E: GetFileAttributesExFromApp
  • E: SetFileAttributesFromApp
  • E: SetFilePointer
  • M: ReadFile
  • M: WriteFile
  • M: WriteFileEx
  • M: GetFileTime
  • M: SetFileTime
  • E: GetFileInformationByHandleEx
  • E: GetFileInformationByHandleEx
  • E: FindFirstStreamW
  • E: FindNextStreamW
  • E: GetDpiForMonitor
  • E: OpenProcessToken
  • E: GetCurrentProcess
  • E: GetTokenInformation
  • E: GetLengthSid
  • M: CryptUnprotectData
  • E: IsWow64Process2
  • E: FindNextFile
  • E: FindClose
  • M: FileTimeToSystemTime
  • E: FindFirstFileExFromApp
  • E: CompareStringEx
  • E: SHCreateStreamOnFileEx
  • M: SHCreateItemFromParsingName
  • M: CoCreateInstance
  • E: RegisterApplicationRestart
  • M: SHGetKnownFolderPath

Undocumented native functions

  • IsElevationRequired
  • SHUpdateRecycleBinIcon

Sorry, something went wrong.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
Status: 🏗 In progress
Development

Successfully merging a pull request may close this issue.

3 participants