From 8a0edb47756d9f6cec22a9d9180de202f9653107 Mon Sep 17 00:00:00 2001 From: David Hall Date: Fri, 1 Sep 2023 17:59:54 -0600 Subject: [PATCH] Added nullability to Mpr --- PInvoke/Mpr/Winnetwk.Ext.cs | 3 ++- PInvoke/Mpr/Winnetwk.cs | 30 ++++++++++++++++++------------ UnitTests/PInvoke/Mpr/WinNetWkTests.cs | 11 ++--------- 3 files changed, 22 insertions(+), 22 deletions(-) diff --git a/PInvoke/Mpr/Winnetwk.Ext.cs b/PInvoke/Mpr/Winnetwk.Ext.cs index 763fe60a..808685a6 100644 --- a/PInvoke/Mpr/Winnetwk.Ext.cs +++ b/PInvoke/Mpr/Winnetwk.Ext.cs @@ -124,7 +124,8 @@ public static partial class Mpr /// /// if set to [recurse containers]. /// The enumeration results. The results are returned as a list of NETRESOURCE structures. - public static IEnumerable WNetEnumResources([Optional] NETRESOURCE root, NETRESOURCEScope dwScope = NETRESOURCEScope.RESOURCE_GLOBALNET, NETRESOURCEType dwType = NETRESOURCEType.RESOURCETYPE_ANY, NETRESOURCEUsage dwUsage = 0, bool recurseContainers = false) + public static IEnumerable WNetEnumResources([Optional] NETRESOURCE? root, NETRESOURCEScope dwScope = NETRESOURCEScope.RESOURCE_GLOBALNET, + NETRESOURCEType dwType = NETRESOURCEType.RESOURCETYPE_ANY, NETRESOURCEUsage dwUsage = 0, bool recurseContainers = false) { var err = WNetOpenEnum(dwScope, dwType, dwUsage, root, out var h); if (err == Win32Error.ERROR_NOT_CONTAINER || err == Win32Error.ERROR_NO_NETWORK) diff --git a/PInvoke/Mpr/Winnetwk.cs b/PInvoke/Mpr/Winnetwk.cs index 7ba1e8d4..3e018b43 100644 --- a/PInvoke/Mpr/Winnetwk.cs +++ b/PInvoke/Mpr/Winnetwk.cs @@ -172,9 +172,11 @@ public static partial class Mpr public enum INFO_LEVEL { /// The function stores a UNIVERSAL_NAME_INFO structure in the buffer. + [CorrespondingType(typeof(UNIVERSAL_NAME_INFO))] UNIVERSAL_NAME_INFO_LEVEL = 0x00000001, /// The function stores a REMOTE_NAME_INFO structure in the buffer. + [CorrespondingType(typeof(REMOTE_NAME_INFO))] REMOTE_NAME_INFO_LEVEL = 0x00000002 } @@ -1644,7 +1646,7 @@ public static partial class Mpr // DWORD WNetGetConnection( _In_ LPCTSTR lpLocalName, _Out_ LPTSTR lpRemoteName, _Inout_ LPDWORD lpnLength); https://msdn.microsoft.com/en-us/library/windows/desktop/aa385453(v=vs.85).aspx [DllImport(Lib.Mpr, SetLastError = true, CharSet = CharSet.Auto)] [PInvokeData("Winnetwk.h", MSDNShortId = "aa385453")] - public static extern Win32Error WNetGetConnection(string lpLocalName, StringBuilder lpRemoteName, ref uint lpnLength); + public static extern Win32Error WNetGetConnection(string lpLocalName, StringBuilder? lpRemoteName, ref uint lpnLength); /// /// The WNetGetLastError function retrieves the most recent extended error code set by a WNet function. The network provider @@ -1753,7 +1755,7 @@ public static partial class Mpr // DWORD WNetGetProviderName( _In_ DWORD dwNetType, _Out_ LPTSTR lpProviderName, _Inout_ LPDWORD lpBufferSize); https://msdn.microsoft.com/en-us/library/windows/desktop/aa385464(v=vs.85).aspx [DllImport(Lib.Mpr, SetLastError = false, CharSet = CharSet.Auto)] [PInvokeData("Winnetwk.h", MSDNShortId = "aa385464")] - public static extern Win32Error WNetGetProviderName(WNNC_NET dwNetType, StringBuilder lpProviderName, ref uint lpBufferSize); + public static extern Win32Error WNetGetProviderName(WNNC_NET dwNetType, StringBuilder? lpProviderName, ref uint lpBufferSize); /// /// When provided with a remote path to a network resource, the WNetGetResourceInformation function identifies the network @@ -1841,7 +1843,7 @@ public static partial class Mpr // LPTSTR *lplpSystem); https://msdn.microsoft.com/en-us/library/windows/desktop/aa385469(v=vs.85).aspx [DllImport(Lib.Mpr, SetLastError = true, CharSet = CharSet.Auto)] [PInvokeData("Winnetwk.h", MSDNShortId = "aa385469")] - public static extern Win32Error WNetGetResourceInformation(NETRESOURCE lpNetResource, IntPtr lpBuffer, ref uint lpcbBuffer, out IntPtr lplpSystem); + public static extern Win32Error WNetGetResourceInformation(NETRESOURCE lpNetResource, IntPtr lpBuffer, ref uint lpcbBuffer, out StrPtrAuto lplpSystem); /// /// @@ -2100,7 +2102,7 @@ public static partial class Mpr // DWORD WNetGetUser( _In_ LPCTSTR lpName, _Out_ LPTSTR lpUserName, _Inout_ LPDWORD lpnLength); https://msdn.microsoft.com/en-us/library/windows/desktop/aa385476(v=vs.85).aspx [DllImport(Lib.Mpr, SetLastError = true, CharSet = CharSet.Auto)] [PInvokeData("Winnetwk.h", MSDNShortId = "aa385476")] - public static extern Win32Error WNetGetUser([Optional] string? lpName, StringBuilder lpUserName, ref uint lpnLength); + public static extern Win32Error WNetGetUser([Optional] string? lpName, StringBuilder? lpUserName, ref uint lpnLength); /// /// The WNetOpenEnum function starts an enumeration of network resources or existing connections. You can continue the @@ -2259,7 +2261,7 @@ public static partial class Mpr // lphEnum); https://msdn.microsoft.com/en-us/library/windows/desktop/aa385478(v=vs.85).aspx [DllImport(Lib.Mpr, SetLastError = true, CharSet = CharSet.Auto)] [PInvokeData("Winnetwk.h", MSDNShortId = "aa385478")] - public static extern Win32Error WNetOpenEnum(NETRESOURCEScope dwScope, NETRESOURCEType dwType, NETRESOURCEUsage dwUsage, [Optional] NETRESOURCE lpNetResource, out SafeWNetEnumHandle lphEnum); + public static extern Win32Error WNetOpenEnum(NETRESOURCEScope dwScope, NETRESOURCEType dwType, NETRESOURCEUsage dwUsage, [Optional] NETRESOURCE? lpNetResource, out SafeWNetEnumHandle lphEnum); /// /// Sets extended error information. Network providers should call this function instead of SetLastError. @@ -2305,7 +2307,7 @@ public static partial class Mpr /// Extension method for WNet function results that handles provider errors (ERROR_EXTENDED_ERROR). /// The error produced by a WNet function. /// The message. - public static void WNetThrowIfFailed(this Win32Error err, string message = null) + public static void WNetThrowIfFailed(this Win32Error err, string? message = null) { if (err == Win32Error.ERROR_EXTENDED_ERROR) { @@ -2589,7 +2591,8 @@ public static partial class Mpr // _In_ DWORD dwFlags, _Out_ LPTSTR lpAccessName, _Inout_ LPDWORD lpBufferSize, _Out_ LPDWORD lpResult); https://msdn.microsoft.com/en-us/library/windows/desktop/aa385482(v=vs.85).aspx [DllImport(Lib.Mpr, SetLastError = true, CharSet = CharSet.Auto)] [PInvokeData("Winnetwk.h", MSDNShortId = "aa385482")] - public static extern Win32Error WNetUseConnection([Optional] HWND hwndOwner, NETRESOURCE lpNetResource, [Optional] string? lpPassword, [Optional] string? lpUserID, [Optional] CONNECT dwFlags, [Optional] StringBuilder lpAccessName, ref uint lpBufferSize, out CONNECT lpResult); + public static extern Win32Error WNetUseConnection([Optional] HWND hwndOwner, NETRESOURCE lpNetResource, [Optional] string? lpPassword, + [Optional] string? lpUserID, [Optional] CONNECT dwFlags, [Optional] StringBuilder? lpAccessName, ref uint lpBufferSize, out CONNECT lpResult); /// /// The CONNECTDLGSTRUCT structure is used by the WNetConnectionDialog1 function to establish browsing dialog box parameters. @@ -2725,7 +2728,7 @@ public static partial class Mpr /// resource redirected from lpLocalName is disconnected. /// /// - public string lpRemoteName; + public string? lpRemoteName; /// /// Type: DWORD @@ -3137,7 +3140,7 @@ public static partial class Mpr [Serializable] public class NetworkProviderException : Exception { - private NetworkProviderException() { } + private NetworkProviderException() { Description = Provider = string.Empty; } /// Initializes a new instance of the class. /// @@ -3149,12 +3152,12 @@ public static partial class Mpr /// protected NetworkProviderException(SerializationInfo info, StreamingContext context) : base(info, context) { - Description = info.GetString("Description"); - Provider = info.GetString("Provider"); + Description = info.GetString("Description") ?? string.Empty; + Provider = info.GetString("Provider") ?? string.Empty; ProviderErrorCode = info.GetUInt32("ProviderErrorCode"); } - internal NetworkProviderException(uint provErr, string message, string description, string provider) : + internal NetworkProviderException(uint provErr, string? message, string description, string provider) : base(message, new Win32Exception(unchecked((int)Win32Error.ERROR_EXTENDED_ERROR))) { ProviderErrorCode = provErr; @@ -3164,14 +3167,17 @@ public static partial class Mpr /// Gets the provider's description of the error. /// The description. + [DefaultValue("")] public string Description { get; } /// Gets the network provider's name. /// The provider. + [DefaultValue("")] public string Provider { get; } /// Gets the error code reported by the provider. /// The provider error code. + [DefaultValue(0)] public uint ProviderErrorCode { get; } /// diff --git a/UnitTests/PInvoke/Mpr/WinNetWkTests.cs b/UnitTests/PInvoke/Mpr/WinNetWkTests.cs index e206d649..c22a952f 100644 --- a/UnitTests/PInvoke/Mpr/WinNetWkTests.cs +++ b/UnitTests/PInvoke/Mpr/WinNetWkTests.cs @@ -1,11 +1,4 @@ using NUnit.Framework; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Runtime.InteropServices; -using System.Text; -using System.Threading.Tasks; -using Vanara.InteropServices; using static Vanara.PInvoke.Mpr; namespace Vanara.PInvoke.Tests; @@ -114,7 +107,7 @@ public class WinNetWkTests ptr.Size = (int)sz; WNetGetResourceInformation(lnr, (IntPtr)ptr, ref sz, out var sys).ThrowIfFailed(); - var rnr = ptr.ToStructure(); + var rnr = ptr.ToStructure()!; Assert.That((int)rnr.dwUsage, Is.Not.Zero); Assert.That(rnr.lpRemoteName, Is.EqualTo(remSh)); Assert.That(sys, Is.Not.EqualTo(IntPtr.Zero)); @@ -130,7 +123,7 @@ public class WinNetWkTests ptr.Size = (int)sz; WNetGetResourceParent(lnr, (IntPtr)ptr, ref sz).ThrowIfFailed(); - var nrp = ptr.ToStructure(); + var nrp = ptr.ToStructure()!; Assert.That((int)nrp.dwUsage, Is.Not.Zero); Assert.That(nrp.lpRemoteName, Is.EqualTo(remSh)); }