1
1
using System ;
2
2
using System . Runtime . InteropServices ;
3
+ using Microsoft . Win32 . SafeHandles ;
3
4
using Windows . Win32 . Foundation ;
5
+ using Windows . Win32 . Security ;
4
6
using Windows . Win32 . Security . Credentials ;
5
7
using Windows . Win32 . Storage . FileSystem ;
6
8
using Windows . Win32 . UI . Shell ;
@@ -22,6 +24,48 @@ public static unsafe int LoadString(SafeHandle hInstance, uint uID, out PCWSTR l
22
24
}
23
25
}
24
26
27
+ /// <inheritdoc cref="CreateFile(PCWSTR, uint, FILE_SHARE_MODE, SECURITY_ATTRIBUTES*, FILE_CREATION_DISPOSITION, FILE_FLAGS_AND_ATTRIBUTES, HANDLE)"/>
28
+ public static unsafe SafeFileHandle CreateFile (
29
+ in ReadOnlySpan < char > lpFileName ,
30
+ uint dwDesiredAccess ,
31
+ FILE_SHARE_MODE dwShareMode ,
32
+ SECURITY_ATTRIBUTES ? lpSecurityAttributes ,
33
+ FILE_CREATION_DISPOSITION dwCreationDisposition ,
34
+ FILE_FLAGS_AND_ATTRIBUTES dwFlagsAndAttributes ,
35
+ SafeHandle hTemplateFile )
36
+ {
37
+ bool hTemplateFileAddRef = false ;
38
+ try
39
+ {
40
+ fixed ( char * lpFileNameLocal = lpFileName )
41
+ {
42
+ SECURITY_ATTRIBUTES lpSecurityAttributesLocal = lpSecurityAttributes ?? default ( SECURITY_ATTRIBUTES ) ;
43
+ HANDLE hTemplateFileLocal ;
44
+ if ( hTemplateFile is object )
45
+ {
46
+ hTemplateFile . DangerousAddRef ( ref hTemplateFileAddRef ) ;
47
+ hTemplateFileLocal = ( HANDLE ) hTemplateFile . DangerousGetHandle ( ) ;
48
+ }
49
+ else
50
+ hTemplateFileLocal = ( HANDLE ) new IntPtr ( 0L ) ;
51
+ HANDLE __result = CorePInvoke . CreateFile (
52
+ lpFileName : lpFileNameLocal ,
53
+ dwDesiredAccess : dwDesiredAccess ,
54
+ dwShareMode : dwShareMode ,
55
+ lpSecurityAttributes : lpSecurityAttributes . HasValue ? & lpSecurityAttributesLocal : null ,
56
+ dwCreationDisposition : dwCreationDisposition ,
57
+ dwFlagsAndAttributes : dwFlagsAndAttributes ,
58
+ hTemplateFile : hTemplateFileLocal ) ;
59
+ return new SafeFileHandle ( __result , ownsHandle : true ) ;
60
+ }
61
+ }
62
+ finally
63
+ {
64
+ if ( hTemplateFileAddRef )
65
+ hTemplateFile . DangerousRelease ( ) ;
66
+ }
67
+ }
68
+
25
69
/// <inheritdoc cref="CredDelete(PCWSTR, CRED_TYPE, uint)" />
26
70
public static unsafe bool CredDelete ( string TargetName , CRED_TYPE type , CRED_FLAGS flags )
27
71
{
@@ -43,6 +87,36 @@ public static unsafe SafeCredentialHandle CredRead(string TargetName, CRED_TYPE
43
87
return new ( ( nint ) credential , true ) ;
44
88
}
45
89
90
+ /// <inheritdoc cref="GetFileInformationByHandleEx(HANDLE, FILE_INFO_BY_HANDLE_CLASS, void*, uint)"/>
91
+ public static unsafe BOOL GetFileInformationByHandleEx < T > ( SafeHandle hFile , FILE_INFO_BY_HANDLE_CLASS FileInformationClass , out T value ) where T : unmanaged
92
+ {
93
+ fixed ( T * valueLocal = & value )
94
+ {
95
+ return GetFileInformationByHandleEx ( hFile , FileInformationClass , valueLocal , ( uint ) Marshal . SizeOf < T > ( ) ) ;
96
+ }
97
+ }
98
+
99
+ /// <inheritdoc cref="GetFinalPathNameByHandle(HANDLE, PWSTR, uint, GETFINALPATHNAMEBYHANDLE_FLAGS)"/>
100
+ public static unsafe partial uint GetFinalPathNameByHandle ( SafeHandle hFile , Span < char > lpszFilePath , GETFINALPATHNAMEBYHANDLE_FLAGS dwFlags )
101
+ {
102
+ fixed ( char * lpszFilePathLocal = lpszFilePath )
103
+ {
104
+ return GetFinalPathNameByHandle ( hFile , lpszFilePathLocal , ( uint ) lpszFilePath . Length , dwFlags ) ;
105
+ }
106
+ }
107
+
108
+ /// <inheritdoc cref="PathCchCanonicalizeEx(PWSTR, nuint, PCWSTR, PATHCCH_OPTIONS)"/>
109
+ public static unsafe HRESULT PathCchCanonicalizeEx ( ref Span < char > pszPathOut , string pszPathIn , PATHCCH_OPTIONS dwFlags )
110
+ {
111
+ fixed ( char * ppszPathOut = pszPathOut )
112
+ {
113
+ PWSTR wstrpszPathOut = ppszPathOut ;
114
+ HRESULT __result = CorePInvoke . PathCchCanonicalizeEx ( wstrpszPathOut , ( nuint ) pszPathOut . Length , pszPathIn , dwFlags ) ;
115
+ pszPathOut = pszPathOut . Slice ( 0 , wstrpszPathOut . Length ) ;
116
+ return __result ;
117
+ }
118
+ }
119
+
46
120
/// <inheritdoc cref="SHCreateAssociationRegistration(Guid*, object)"/>
47
121
public static unsafe HRESULT SHCreateAssociationRegistration < T > ( out T ppv ) where T : class
48
122
{
0 commit comments