Skip to content

Commit 0714b2a

Browse files
committed
Fix up some unnecessary marshaling within NativeMethods class.
1 parent a749886 commit 0714b2a

20 files changed

Lines changed: 14 additions & 15 deletions

src/PSADT/PSADT.Interop/NativeMethods.cs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -914,7 +914,7 @@ internal static WIN32_ERROR GetNamedSecurityInfo(string pObjectName, SE_OBJECT_T
914914
/// leave the SACL unchanged.</param>
915915
/// <returns>A <see cref="WIN32_ERROR"/> value indicating the result of the operation. Returns <see
916916
/// cref="WIN32_ERROR.ERROR_SUCCESS"/> if the operation succeeds.</returns>
917-
internal static WIN32_ERROR SetNamedSecurityInfo(string pObjectName, SE_OBJECT_TYPE ObjectType, OBJECT_SECURITY_INFORMATION SecurityInfo, SafeNoReleaseHandle? psidOwner, SafeNoReleaseHandle? psidGroup, [Optional] LocalFreeSafeHandle? pDacl, [Optional] LocalFreeSafeHandle? pSacl)
917+
internal static WIN32_ERROR SetNamedSecurityInfo(string pObjectName, SE_OBJECT_TYPE ObjectType, OBJECT_SECURITY_INFORMATION SecurityInfo, SafeNoReleaseHandle? psidOwner, SafeNoReleaseHandle? psidGroup, LocalFreeSafeHandle? pDacl = null, LocalFreeSafeHandle? pSacl = null)
918918
{
919919
ArgumentException.ThrowIfNullOrWhiteSpace(pObjectName);
920920
bool psidOwnerAddRef = false;
@@ -1005,7 +1005,7 @@ internal static WIN32_ERROR SetNamedSecurityInfo(string pObjectName, SE_OBJECT_T
10051005
/// <param name="Args">An optional pointer to additional arguments passed to the progress callback function.</param>
10061006
/// <returns>A <see cref="WIN32_ERROR"/> value indicating the result of the operation. Returns <see
10071007
/// cref="WIN32_ERROR.ERROR_SUCCESS"/> if the operation completes successfully.</returns>
1008-
internal static WIN32_ERROR TreeResetNamedSecurityInfo(string pObjectName, SE_OBJECT_TYPE ObjectType, OBJECT_SECURITY_INFORMATION SecurityInfo, SafeNoReleaseHandle? pOwner, SafeNoReleaseHandle? pGroup, [Optional] LocalFreeSafeHandle? pDacl, [Optional] LocalFreeSafeHandle? pSacl, BOOL KeepExplicit, FN_PROGRESS? fnProgress, PROG_INVOKE_SETTING ProgressInvokeSetting, [Optional] nint? Args)
1008+
internal static WIN32_ERROR TreeResetNamedSecurityInfo(string pObjectName, SE_OBJECT_TYPE ObjectType, OBJECT_SECURITY_INFORMATION SecurityInfo, SafeNoReleaseHandle? pOwner, SafeNoReleaseHandle? pGroup, [Optional] LocalFreeSafeHandle? pDacl, [Optional] LocalFreeSafeHandle? pSacl, BOOL KeepExplicit, FN_PROGRESS? fnProgress, PROG_INVOKE_SETTING ProgressInvokeSetting, nint Args = 0)
10091009
{
10101010
ArgumentException.ThrowIfNullOrWhiteSpace(pObjectName);
10111011
bool pOwnerAddRef = false;
@@ -1022,7 +1022,7 @@ internal static WIN32_ERROR TreeResetNamedSecurityInfo(string pObjectName, SE_OB
10221022
{
10231023
fixed (char* pObjectNameLocal = pObjectName)
10241024
{
1025-
return PInvoke.TreeResetNamedSecurityInfo(pObjectNameLocal, ObjectType, SecurityInfo, pOwner is not null ? (PSID)pOwner.DangerousGetHandle() : (PSID)null, pGroup is not null ? (PSID)pGroup.DangerousGetHandle() : (PSID)null, pDacl is not null ? (ACL*)pDacl.DangerousGetHandle() : (ACL*)null, pSacl is not null ? (ACL*)pSacl.DangerousGetHandle() : (ACL*)null, KeepExplicit, fnProgress, ProgressInvokeSetting, Args is not null ? (void*)Args.Value : null).ThrowOnFailure();
1025+
return PInvoke.TreeResetNamedSecurityInfo(pObjectNameLocal, ObjectType, SecurityInfo, pOwner is not null ? (PSID)pOwner.DangerousGetHandle() : (PSID)null, pGroup is not null ? (PSID)pGroup.DangerousGetHandle() : (PSID)null, pDacl is not null ? (ACL*)pDacl.DangerousGetHandle() : (ACL*)null, pSacl is not null ? (ACL*)pSacl.DangerousGetHandle() : (ACL*)null, KeepExplicit, fnProgress, ProgressInvokeSetting, (void*)Args).ThrowOnFailure();
10261026
}
10271027
}
10281028
}
@@ -2018,8 +2018,7 @@ internal static BOOL ProcessIdToSessionId(uint dwProcessId, out uint pSessionId)
20182018
/// langword="false"/>.</returns>
20192019
[DllImport("kernel32.dll", SetLastError = false, ExactSpelling = true), DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
20202020
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2021-
[return: MarshalAs(UnmanagedType.Bool)]
2022-
internal static extern bool TermsrvAppInstallMode();
2021+
internal static extern BOOL TermsrvAppInstallMode();
20232022

20242023
/// <summary>
20252024
/// Retrieves system firmware table data for the specified firmware table provider and table ID.
@@ -2331,7 +2330,7 @@ internal static BOOL QueryFullProcessImageName(SafeHandle hProcess, PROCESS_NAME
23312330
/// require arguments.</param>
23322331
/// <returns>The number of characters stored in the output buffer, excluding the terminating null character.</returns>
23332332
/// <exception cref="Win32Exception">Thrown if the message formatting operation fails.</exception>
2334-
internal static uint FormatMessage(FORMAT_MESSAGE_OPTIONS dwFlags, [Optional] FreeLibrarySafeHandle? lpSource, uint dwMessageId, Span<char> lpBuffer, uint dwLanguageId = 0, in nint Arguments = default)
2333+
internal static uint FormatMessage(FORMAT_MESSAGE_OPTIONS dwFlags, [Optional] FreeLibrarySafeHandle? lpSource, uint dwMessageId, Span<char> lpBuffer, uint dwLanguageId = 0, nint Arguments = default)
23352334
{
23362335
unsafe
23372336
{
@@ -2646,7 +2645,7 @@ internal static NTSTATUS NtQueryObject(SafeHandle? Handle, OBJECT_INFORMATION_CL
26462645
/// <exception cref="ArgumentNullException">Thrown if ProcessHandle is null or closed, or if StartRoutine is null, closed, or invalid.</exception>
26472646
internal static NTSTATUS NtCreateThreadEx(out SafeThreadHandle ThreadHandle, THREAD_ACCESS_RIGHTS DesiredAccess, SafeProcessHandle ProcessHandle, SafeVirtualAllocHandle StartRoutine, nint? Argument = null, THREAD_CREATE_FLAGS CreateFlags = 0, uint ZeroBits = 0, uint StackSize = 0, uint MaximumStackSize = 0)
26482647
{
2649-
[DllImport("ntdll.dll", ExactSpelling = true), DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
2648+
[DllImport("ntdll.dll", ExactSpelling = true), DefaultDllImportSearchPaths(DllImportSearchPath.System32)][MethodImpl(MethodImplOptions.AggressiveInlining)]
26502649
static extern NTSTATUS NtCreateThreadEx(out nint ThreadHandle, THREAD_ACCESS_RIGHTS DesiredAccess, nint ObjectAttributes, nint ProcessHandle, nint StartRoutine, nint Argument, THREAD_CREATE_FLAGS CreateFlags, uint ZeroBits, uint StackSize, uint MaximumStackSize, nint AttributeList);
26512650
ArgumentException.ThrowIfNullOrClosed(ProcessHandle);
26522651
ArgumentException.ThrowIfNullOrInvalid(StartRoutine);
@@ -2688,7 +2687,7 @@ internal static NTSTATUS NtCreateThreadEx(out SafeThreadHandle ThreadHandle, THR
26882687
/// <exception cref="ArgumentNullException">Thrown if ThreadHandle is null or has already been closed.</exception>
26892688
internal static NTSTATUS NtTerminateThread(SafeThreadHandle ThreadHandle, in NTSTATUS ExitStatus)
26902689
{
2691-
[DllImport("ntdll.dll", ExactSpelling = true), DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
2690+
[DllImport("ntdll.dll", ExactSpelling = true), DefaultDllImportSearchPaths(DllImportSearchPath.System32)][MethodImpl(MethodImplOptions.AggressiveInlining)]
26922691
static extern NTSTATUS NtTerminateThread(nint ThreadHandle, NTSTATUS ExitStatus);
26932692
ArgumentException.ThrowIfNullOrInvalid(ThreadHandle);
26942693
bool ThreadHandleAddRef = false;
@@ -2944,7 +2943,7 @@ internal static HRESULT SHQueryUserNotificationState(out Windows.Win32.UI.Shell.
29442943
/// interpretation depends on the values of wEventId and uFlags. This parameter is optional and may be
29452944
/// default if not required.</param>
29462945
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2947-
internal static void SHChangeNotify([MarshalAs(UnmanagedType.I4)] SHCNE_ID wEventId, SHCNF_FLAGS uFlags, [Optional] nint dwItem1, [Optional] nint dwItem2)
2946+
internal static void SHChangeNotify(SHCNE_ID wEventId, SHCNF_FLAGS uFlags, nint dwItem1 = 0, nint dwItem2 = 0)
29482947
{
29492948
unsafe
29502949
{
@@ -2966,7 +2965,7 @@ internal static void SHChangeNotify([MarshalAs(UnmanagedType.I4)] SHCNE_ID wEven
29662965
/// <returns>An HRESULT value indicating the success or failure of the operation.</returns>
29672966
internal static HRESULT SHGetStockIconInfo(SHSTOCKICONID siid, SHGSI_FLAGS uFlags, out SHSTOCKICONINFO psii)
29682967
{
2969-
[DllImport("shell32.dll", CharSet = CharSet.Unicode), DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
2968+
[DllImport("shell32.dll", CharSet = CharSet.Unicode), DefaultDllImportSearchPaths(DllImportSearchPath.System32)][MethodImpl(MethodImplOptions.AggressiveInlining)]
29702969
static extern HRESULT SHGetStockIconInfo(SHSTOCKICONID siid, SHGSI_FLAGS uFlags, ref SHSTOCKICONINFO psii);
29712970
psii = new() { cbSize = (uint)Marshal.SizeOf<SHSTOCKICONINFO>() };
29722971
HRESULT res = SHGetStockIconInfo(siid, uFlags, ref psii);
@@ -3089,7 +3088,7 @@ internal static HWND GetForegroundWindow()
30893088
/// exception is thrown if a Windows error code is set.</returns>
30903089
internal static int LoadString(SafeHandle hInstance, uint uID, out nint lpBuffer)
30913090
{
3092-
[DllImport("USER32.dll", ExactSpelling = true, EntryPoint = "LoadStringW", SetLastError = true), DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
3091+
[DllImport("USER32.dll", ExactSpelling = true, EntryPoint = "LoadStringW", SetLastError = true), DefaultDllImportSearchPaths(DllImportSearchPath.System32)][MethodImpl(MethodImplOptions.AggressiveInlining)]
30933092
static extern int LoadString(HINSTANCE hInstance, uint uID, out nint lpBuffer, int cchBufferMax);
30943093
ArgumentException.ThrowIfNullOrInvalid(hInstance);
30953094
bool hInstanceAddRef = false;
@@ -3229,7 +3228,7 @@ internal static uint GetWindowThreadProcessId(HWND hWnd, out uint lpdwProcessId)
32293228
ArgumentNullException.ThrowIfNull(hWnd.Value, nameof(hWnd));
32303229
fixed (uint* p = &lpdwProcessId)
32313230
{
3232-
[DllImport("USER32.dll", ExactSpelling = true, SetLastError = true), DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
3231+
[DllImport("USER32.dll", ExactSpelling = true, SetLastError = true), DefaultDllImportSearchPaths(DllImportSearchPath.System32)][MethodImpl(MethodImplOptions.AggressiveInlining)]
32333232
static extern uint GetWindowThreadProcessId(HWND hWnd, uint* lpdwProcessId);
32343233
if ((res = GetWindowThreadProcessId(hWnd, p)) == 0)
32353234
{
@@ -3257,7 +3256,7 @@ internal static uint GetWindowThreadProcessId(HWND hWnd, out uint lpdwProcessId)
32573256
/// mechanisms were successfully attached or detached; otherwise, <see langword="false"/>.</returns>
32583257
internal static BOOL AttachThreadInput(uint idAttach, uint idAttachTo, bool fAttach)
32593258
{
3260-
[DllImport("USER32.dll", ExactSpelling = true, SetLastError = true), DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
3259+
[DllImport("USER32.dll", ExactSpelling = true, SetLastError = true), DefaultDllImportSearchPaths(DllImportSearchPath.System32)][MethodImpl(MethodImplOptions.AggressiveInlining)]
32613260
static extern BOOL AttachThreadInput(uint idAttach, uint idAttachTo, BOOL fAttach);
32623261
BOOL res = AttachThreadInput(idAttach, idAttachTo, fAttach);
32633262
return !res ? throw ExceptionUtilities.GetExceptionForLastWin32Error() : res;
@@ -3544,7 +3543,7 @@ internal static uint GetDpiForWindow(HWND hwnd)
35443543
/// <returns>A <see cref="MESSAGEBOX_RESULT"/> value indicating the user's response to the message box.</returns>
35453544
internal static MESSAGEBOX_RESULT MessageBoxTimeout(HWND? hWnd, string lpText, string lpCaption, MESSAGEBOX_STYLE uType, ushort wLanguageId, uint dwTimeout)
35463545
{
3547-
[DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true), DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
3546+
[DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true), DefaultDllImportSearchPaths(DllImportSearchPath.System32)][MethodImpl(MethodImplOptions.AggressiveInlining)]
35483547
static extern MESSAGEBOX_RESULT MessageBoxTimeoutW(HWND hWnd, string lpText, string lpCaption, MESSAGEBOX_STYLE uType, ushort wLanguageId, uint dwMilliseconds);
35493548
ArgumentException.ThrowIfNullOrWhiteSpace(lpText); ArgumentException.ThrowIfNullOrWhiteSpace(lpCaption);
35503549
MESSAGEBOX_RESULT res = MessageBoxTimeoutW(hWnd ?? default, lpText, lpCaption, uType, wLanguageId, dwTimeout);
@@ -4249,7 +4248,7 @@ internal static HRESULT SHGetKnownFolderPath(in Guid rfid, KNOWN_FOLDER_FLAG dwF
42494248
/// other codes indicate errors or special conditions.</returns>
42504249
internal static NTSTATUS RtlExpandEnvironmentStrings_U(SafeEnvironmentBlockHandle Environment, in UNICODE_STRING SourceString, ref UNICODE_STRING DestinationString, out uint RequiredBytes)
42514250
{
4252-
[DllImport("ntdll.dll", ExactSpelling = true), DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
4251+
[DllImport("ntdll.dll", ExactSpelling = true), DefaultDllImportSearchPaths(DllImportSearchPath.System32)][MethodImpl(MethodImplOptions.AggressiveInlining)]
42534252
static extern NTSTATUS RtlExpandEnvironmentStrings_U(IntPtr Environment, in UNICODE_STRING SourceString, ref UNICODE_STRING DestinationString, out uint RequiredBytes);
42544253
ArgumentException.ThrowIfNullOrInvalid(Environment); ArgumentException.ThrowIfNullOrInvalid(SourceString); ArgumentException.ThrowIfInvalid(DestinationString);
42554254
bool EnvironmentAddRef = false;
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)