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
Open

Code Quality: Replace Vanara with CsWin32 #15000

gumbarros opened this issue Mar 19, 2024 · 9 comments

Comments

@gumbarros
Copy link
Contributor

gumbarros commented Mar 19, 2024

Description

Related to https://learn.microsoft.com/en-us/dotnet/fundamentals/syslib-diagnostics/syslib1050-1069.

Concerned code

Example:

[DllImport("api-ms-win-core-timezone-l1-1-0.dll", SetLastError = true)]
public static extern bool FileTimeToSystemTime(
	ref FILETIME lpFileTime,
	out SYSTEMTIME lpSymTime);stemTime);

to

[LibraryImport("api-ms-win-core-timezone-l1-1-0.dll", SetLastError = true)]
public static extern bool FileTimeToSystemTime(
	ref FILETIME lpFileTime,
	out SYSTEMTIME lpSystemTime);

Gains

  • Better performance using source generators instead of reflection

Requirements

  • Running R# to generate the report of variables and fields that can be converted
  • Applying the change file by file

Comments

No response

@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.

@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 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: 🏗 In progress
Development

No branches or pull requests

3 participants