Skip to content

Commit 44fdcd8

Browse files
committed
Replace with CsWin32 in AccessControlPrincipal
1 parent ad0cd74 commit 44fdcd8

File tree

3 files changed

+30
-18
lines changed

3 files changed

+30
-18
lines changed

src/Files.App/Data/Items/AccessControlPrincipal.cs

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// Copyright (c) 2024 Files Community
22
// Licensed under the MIT License. See the LICENSE.
33

4-
using System.Text;
5-
using Vanara.PInvoke;
6-
using static Vanara.PInvoke.AdvApi32;
4+
using Windows.Win32;
5+
using Windows.Win32.Foundation;
6+
using Windows.Win32.Security;
77

88
namespace Files.App.Data.Items
99
{
@@ -66,26 +66,37 @@ public string? FullNameHumanized
6666
public string FullNameHumanizedWithBrackes
6767
=> string.IsNullOrEmpty(Domain) ? string.Empty : $"({Domain}\\{Name})";
6868

69-
public AccessControlPrincipal(string sid)
69+
public unsafe AccessControlPrincipal(string sid)
7070
{
7171
if (string.IsNullOrEmpty(sid))
7272
return;
7373

7474
Sid = sid;
75-
var lpSid = ConvertStringSidToSid(sid);
75+
PSID lpSid = default;
76+
SID_NAME_USE snu = default;
7677

77-
StringBuilder lpName = new(), lpDomain = new();
78-
int cchName = 0, cchDomainName = 0;
78+
fixed (char* cSid = sid)
79+
{
80+
// Get PSID object from string sid
81+
PInvoke.ConvertStringSidToSid(new PCWSTR(cSid), &lpSid);
82+
}
83+
84+
PWSTR lpName = default;
85+
PWSTR lpDomain = default;
86+
uint cchName = 0, cchDomainName = 0;
7987

8088
// Get size of account name and domain name
81-
bool bResult = LookupAccountSid(null, lpSid, lpName, ref cchName, lpDomain, ref cchDomainName, out _);
89+
bool bResult = PInvoke.LookupAccountSid(new PCWSTR(), lpSid, lpName, &cchName, lpDomain, &cchDomainName, null);
8290

8391
// Ensure requested capacity
84-
lpName.EnsureCapacity(cchName);
85-
lpDomain.EnsureCapacity(cchDomainName);
92+
fixed (char* cName = new char[cchName])
93+
lpName = new(cName);
94+
95+
fixed (char* cDomain = new char[cchDomainName])
96+
lpDomain = new(cDomain);
8697

8798
// Get account name and domain
88-
bResult = LookupAccountSid(null, lpSid, lpName, ref cchName, lpDomain, ref cchDomainName, out var snu);
99+
bResult = PInvoke.LookupAccountSid(new PCWSTR(), lpSid, lpName, &cchName, lpDomain, &cchDomainName, &snu);
89100
if(!bResult)
90101
return;
91102

@@ -106,14 +117,14 @@ var x when
106117
_ => AccessControlPrincipalType.Unknown
107118
};
108119

109-
lpDomain.Clear();
110-
111120
// Replace domain name with computer name if the account type is user or alias type
112121
if (snu == SID_NAME_USE.SidTypeUser || snu == SID_NAME_USE.SidTypeAlias)
113122
{
114-
lpDomain = new(256, 256);
115-
uint size = (uint)lpDomain.Capacity;
116-
bResult = Kernel32.GetComputerName(lpDomain, ref size);
123+
uint size = 256;
124+
fixed (char* cDomain = new char[size])
125+
lpDomain = new(cDomain);
126+
127+
bResult = PInvoke.GetComputerName(lpDomain, ref size);
117128
if (!bResult)
118129
return;
119130
}

src/Files.App/NativeMethods.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,5 @@ ACL_SIZE_INFORMATION
8282
DeleteAce
8383
EXPLICIT_ACCESS
8484
ACCESS_ALLOWED_ACE
85-
ACCESS_MASK
85+
LookupAccountSid
86+
GetComputerName

src/Files.App/ViewModels/Properties/SecurityAdvancedViewModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ private void LoadAccessControlEntry()
187187
ErrorMessage =
188188
"SecurityUnableToDisplayPermissions".GetLocalizedResource() +
189189
"\r\n\r\n" +
190-
error.FormatMessage();
190+
error.ToString();
191191
}
192192
}
193193
else

0 commit comments

Comments
 (0)