-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Code Quality: Migrate DllImportAttribute
to LibraryImportAttribute
at NativeFindStorageItemHelper.cs
#15056
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
Closed
gumbarros
wants to merge
4
commits into
files-community:main
from
gumbarros:15000-native-find-storage-item-helper
Closed
Code Quality: Migrate DllImportAttribute
to LibraryImportAttribute
at NativeFindStorageItemHelper.cs
#15056
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a232631
Migrate `DllImportAttribute` to `LibraryImport`
gumbarros 90b5766
Merge branch 'main' into 15000-native-find-storage-item-helper
gumbarros 0ee9ae0
Custom marshalling for Win32FindData
gumbarros c347192
Merge branch '15000-native-find-storage-item-helper' of https://githu…
gumbarros File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
using System.Runtime.CompilerServices; | ||
|
||
[assembly:DisableRuntimeMarshalling] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using System.Runtime.InteropServices; | ||
using System.Runtime.InteropServices.ComTypes; | ||
using System.Runtime.InteropServices.Marshalling; | ||
|
||
namespace Files.Core.Helpers; | ||
|
||
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] | ||
[NativeMarshalling(typeof(Win32FindDataMarshaller))] | ||
public struct WIN32_FIND_DATA | ||
{ | ||
public uint dwFileAttributes; | ||
|
||
public FILETIME ftCreationTime; | ||
public FILETIME ftLastAccessTime; | ||
public FILETIME ftLastWriteTime; | ||
public uint nFileSizeHigh; | ||
public uint nFileSizeLow; | ||
public uint dwReserved0; | ||
public uint dwReserved1; | ||
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)] | ||
public string cFileName; | ||
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 14)] | ||
public string cAlternateFileName; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
using System.Runtime.InteropServices.Marshalling; | ||
|
||
namespace Files.Core.Helpers; | ||
|
||
[CustomMarshaller(typeof(WIN32_FIND_DATA), MarshalMode.ManagedToUnmanagedOut, typeof(Win32FindDataMarshaller))] | ||
internal static unsafe class Win32FindDataMarshaller | ||
{ | ||
public struct WIN32_FIND_DATA_UNMANAGED | ||
{ | ||
public SystemIO.FileAttributes dwFileAttributes; | ||
public uint ftCreationTime_dwLowDateTime; | ||
public uint ftCreationTime_dwHighDateTime; | ||
public uint ftLastAccessTime_dwLowDateTime; | ||
public uint ftLastAccessTime_dwHighDateTime; | ||
public uint ftLastWriteTime_dwLowDateTime; | ||
public uint ftLastWriteTime_dwHighDateTime; | ||
public uint nFileSizeHigh; | ||
public uint nFileSizeLow; | ||
public uint dwReserved0; | ||
public uint dwReserved1; | ||
public fixed ushort cFileName[256]; | ||
public fixed ushort cAlternateFileName[14]; | ||
} | ||
|
||
|
||
public static WIN32_FIND_DATA ConvertToManaged(WIN32_FIND_DATA_UNMANAGED unmanaged) | ||
{ | ||
var managed = new WIN32_FIND_DATA(); | ||
managed.dwFileAttributes = (uint)unmanaged.dwFileAttributes; | ||
managed.ftCreationTime.dwLowDateTime = (int)unmanaged.ftCreationTime_dwLowDateTime; | ||
managed.ftCreationTime.dwHighDateTime = (int)unmanaged.ftCreationTime_dwHighDateTime; | ||
managed.ftLastAccessTime.dwLowDateTime = (int)unmanaged.ftLastAccessTime_dwLowDateTime; | ||
managed.ftLastAccessTime.dwHighDateTime = (int)unmanaged.ftLastAccessTime_dwHighDateTime; | ||
managed.ftLastWriteTime.dwLowDateTime = (int)unmanaged.ftLastWriteTime_dwLowDateTime; | ||
managed.ftLastWriteTime.dwHighDateTime = (int)unmanaged.ftLastWriteTime_dwHighDateTime; | ||
managed.nFileSizeHigh = unmanaged.nFileSizeHigh; | ||
managed.nFileSizeLow = unmanaged.nFileSizeLow; | ||
|
||
managed.cFileName = GetStringFromBuffer(unmanaged.cFileName); | ||
managed.cAlternateFileName = GetStringFromBuffer(unmanaged.cAlternateFileName); | ||
return managed; | ||
} | ||
|
||
public static WIN32_FIND_DATA_UNMANAGED ConvertToUnmanaged(WIN32_FIND_DATA managed) | ||
{ | ||
WIN32_FIND_DATA_UNMANAGED unmanaged = new WIN32_FIND_DATA_UNMANAGED(); | ||
unmanaged.dwFileAttributes = (SystemIO.FileAttributes)managed.dwFileAttributes; | ||
unmanaged.ftCreationTime_dwLowDateTime = (uint)managed.ftCreationTime.dwLowDateTime; | ||
unmanaged.ftCreationTime_dwHighDateTime = (uint)managed.ftCreationTime.dwHighDateTime; | ||
unmanaged.ftLastAccessTime_dwLowDateTime = (uint)managed.ftLastAccessTime.dwLowDateTime; | ||
unmanaged.ftLastAccessTime_dwHighDateTime = (uint)managed.ftLastAccessTime.dwHighDateTime; | ||
unmanaged.ftLastWriteTime_dwLowDateTime = (uint)managed.ftLastWriteTime.dwLowDateTime; | ||
unmanaged.ftLastWriteTime_dwHighDateTime = (uint)managed.ftLastWriteTime.dwHighDateTime; | ||
unmanaged.nFileSizeHigh = managed.nFileSizeHigh; | ||
unmanaged.nFileSizeLow = managed.nFileSizeLow; | ||
|
||
CopyStringToBuffer(managed.cFileName, unmanaged.cFileName); | ||
CopyStringToBuffer(managed.cAlternateFileName, unmanaged.cAlternateFileName); | ||
return unmanaged; | ||
} | ||
|
||
private static void CopyStringToBuffer(string source, ushort* destination) | ||
{ | ||
int index = 0; | ||
foreach (char c in source) | ||
{ | ||
destination[index++] = c; | ||
if (index >= source.Length) | ||
break; | ||
} | ||
|
||
destination[index] = '\0'; // Null-terminate the string | ||
} | ||
|
||
private static string GetStringFromBuffer(ushort* source) | ||
{ | ||
int index = 0; | ||
char[] chars = new char[256]; // Assuming max buffer size for file name | ||
while (source[index] != '\0' && index < 256) | ||
{ | ||
chars[index] = (char)source[index]; | ||
index++; | ||
} | ||
|
||
return new string(chars, 0, index); | ||
} | ||
|
||
public static void Free(WIN32_FIND_DATA_UNMANAGED unmanaged) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need help with someone with Win32 knowledge to know if this
CharSet
property is important, the only equivalent isStringMarshalling