Skip to content

Commit

Permalink
Added nullability to NdfApi
Browse files Browse the repository at this point in the history
  • Loading branch information
dahall committed Sep 13, 2023
1 parent 26a8280 commit aa34fe9
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 33 deletions.
2 changes: 1 addition & 1 deletion PInvoke/Kernel32/WinNT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1024,7 +1024,7 @@ public struct MESSAGE_RESOURCE_ENTRY
/// </summary>
public byte Text;

public static unsafe string GetText([In] MESSAGE_RESOURCE_ENTRY* mre) => mre->Flags == 0x0001 ? Marshal.PtrToStringUni(((IntPtr)(void*)mre).Offset(TextOffset.Value))! : Marshal.PtrToStringAnsi(((IntPtr)(void*)mre).Offset(TextOffset.Value))!;
internal static unsafe string GetText([In] MESSAGE_RESOURCE_ENTRY* mre) => mre->Flags == 0x0001 ? Marshal.PtrToStringUni(((IntPtr)(void*)mre).Offset(TextOffset.Value))! : Marshal.PtrToStringAnsi(((IntPtr)(void*)mre).Offset(TextOffset.Value))!;

private static Lazy<long> TextOffset => new(() => Marshal.OffsetOf(typeof(MESSAGE_RESOURCE_ENTRY), nameof(Text)).ToInt64());
}
Expand Down
4 changes: 2 additions & 2 deletions PInvoke/NdfApi/NdAttrib.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1427,7 +1427,7 @@ public struct ShellCommandInfo
/// </para>
/// </summary>
[MarshalAs(UnmanagedType.LPWStr)]
public string pwszOperation;
public string? pwszOperation;

/// <summary>
/// <para>Type: <c>[string] LPWSTR</c></para>
Expand All @@ -1449,7 +1449,7 @@ public struct ShellCommandInfo
/// </para>
/// </summary>
[MarshalAs(UnmanagedType.LPWStr)]
public string pwszParameters;
public string? pwszParameters;

/// <summary>
/// <para>Type: <c>[string] LPWSTR</c></para>
Expand Down
22 changes: 11 additions & 11 deletions PInvoke/NdfApi/NdHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public interface INetDiagExtensibleHelper
/// <summary/>
[PreserveSig]
HRESULT ResolveAttributes([In] uint celt, [In, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)] HELPER_ATTRIBUTE[] rgKeyAttributes,
out uint pcelt, [Out, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 2)] out HELPER_ATTRIBUTE[] prgMatchValues);
out uint pcelt, [Out, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 2)] out HELPER_ATTRIBUTE[]? prgMatchValues);
}

/// <summary>
Expand Down Expand Up @@ -281,7 +281,7 @@ public interface INetDiagHelper
// https://docs.microsoft.com/en-us/windows/win32/api/ndhelper/nf-ndhelper-inetdiaghelper-getkeyattributes HRESULT GetKeyAttributes(
// [out] ULONG *pcelt, [out] HELPER_ATTRIBUTE **pprgAttributes );
[PreserveSig]
HRESULT GetKeyAttributes(out uint pcelt, [Out, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)] out HELPER_ATTRIBUTE[] pprgAttributes);
HRESULT GetKeyAttributes(out uint pcelt, [Out, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)] out HELPER_ATTRIBUTE[]? pprgAttributes);

/// <summary>
/// The <c>LowHealth</c> method enables the Helper Class Extension to check whether the component being diagnosed is healthy.
Expand Down Expand Up @@ -348,7 +348,7 @@ public interface INetDiagHelper
// pwszInstanceDescription, [out] LPWSTR *ppwszDescription, [out] long *pDeferredTime, [out] DIAGNOSIS_STATUS *pStatus );
[PreserveSig]
HRESULT LowHealth([Optional, MarshalAs(UnmanagedType.LPWStr)] string? pwszInstanceDescription,
[MarshalAs(UnmanagedType.LPWStr)] out string ppwszDescription, out long pDeferredTime, out DIAGNOSIS_STATUS pStatus);
[MarshalAs(UnmanagedType.LPWStr)] out string? ppwszDescription, out long pDeferredTime, out DIAGNOSIS_STATUS pStatus);

/// <summary>
/// The <c>HighUtilization</c> method enables the Helper Class Extension to check whether the corresponding component is highly utilized.
Expand Down Expand Up @@ -404,7 +404,7 @@ HRESULT LowHealth([Optional, MarshalAs(UnmanagedType.LPWStr)] string? pwszInstan
// [in] LPCWSTR pwszInstanceDescription, [out] LPWSTR *ppwszDescription, [out] long *pDeferredTime, [out] DIAGNOSIS_STATUS *pStatus );
[PreserveSig]
HRESULT HighUtilization([Optional, MarshalAs(UnmanagedType.LPWStr)] string? pwszInstanceDescription,
[MarshalAs(UnmanagedType.LPWStr)] out string ppwszDescription, out long pDeferredTime, out DIAGNOSIS_STATUS pStatus);
[MarshalAs(UnmanagedType.LPWStr)] out string? ppwszDescription, out long pDeferredTime, out DIAGNOSIS_STATUS pStatus);

/// <summary>
/// The <c>GetLowerHypotheses</c> method asks the Helper Class Extension to generate hypotheses for possible causes of low health in
Expand Down Expand Up @@ -449,7 +449,7 @@ HRESULT HighUtilization([Optional, MarshalAs(UnmanagedType.LPWStr)] string? pwsz
// https://docs.microsoft.com/en-us/windows/win32/api/ndhelper/nf-ndhelper-inetdiaghelper-getlowerhypotheses HRESULT
// GetLowerHypotheses( [out] ULONG *pcelt, [out] HYPOTHESIS **pprgHypotheses );
[PreserveSig]
HRESULT GetLowerHypotheses(out uint pcelt, [Out, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)] out HYPOTHESIS[] pprgHypotheses);
HRESULT GetLowerHypotheses(out uint pcelt, [Out, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)] out HYPOTHESIS[]? pprgHypotheses);

/// <summary>
/// The <c>GetDownStreamHypotheses</c> method asks the Helper Class Extension to generate hypotheses for possible causes of low
Expand Down Expand Up @@ -494,7 +494,7 @@ HRESULT HighUtilization([Optional, MarshalAs(UnmanagedType.LPWStr)] string? pwsz
// https://docs.microsoft.com/en-us/windows/win32/api/ndhelper/nf-ndhelper-inetdiaghelper-getdownstreamhypotheses HRESULT
// GetDownStreamHypotheses( [out] ULONG *pcelt, [out] HYPOTHESIS **pprgHypotheses );
[PreserveSig]
HRESULT GetDownStreamHypotheses(out uint pcelt, [Out, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)] out HYPOTHESIS[] pprgHypotheses);
HRESULT GetDownStreamHypotheses(out uint pcelt, [Out, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)] out HYPOTHESIS[]? pprgHypotheses);

/// <summary>
/// The <c>GetHigherHypotheses</c> method asks the Helper Class Extension to generate hypotheses for possible causes of high
Expand Down Expand Up @@ -539,7 +539,7 @@ HRESULT HighUtilization([Optional, MarshalAs(UnmanagedType.LPWStr)] string? pwsz
// https://docs.microsoft.com/en-us/windows/win32/api/ndhelper/nf-ndhelper-inetdiaghelper-gethigherhypotheses HRESULT
// GetHigherHypotheses( [out] ULONG *pcelt, [out] HYPOTHESIS **pprgHypotheses );
[PreserveSig]
HRESULT GetHigherHypotheses(out uint pcelt, [Out, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)] out HYPOTHESIS[] pprgHypotheses);
HRESULT GetHigherHypotheses(out uint pcelt, [Out, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)] out HYPOTHESIS[]? pprgHypotheses);

/// <summary>
/// The <c>GetUpStreamHypotheses</c> method asks the Helper Class Extension to generate hypotheses for possible causes of high
Expand Down Expand Up @@ -584,7 +584,7 @@ HRESULT HighUtilization([Optional, MarshalAs(UnmanagedType.LPWStr)] string? pwsz
// https://docs.microsoft.com/en-us/windows/win32/api/ndhelper/nf-ndhelper-inetdiaghelper-getupstreamhypotheses HRESULT
// GetUpStreamHypotheses( [out] ULONG *pcelt, [out] HYPOTHESIS **pprgHypotheses );
[PreserveSig]
HRESULT GetUpStreamHypotheses(out uint pcelt, [Out, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)] out HYPOTHESIS[] pprgHypotheses);
HRESULT GetUpStreamHypotheses(out uint pcelt, [Out, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)] out HYPOTHESIS[]? pprgHypotheses);

/// <summary>The <c>Repair</c> method performs a repair specified by the input parameter.</summary>
/// <param name="pInfo">A pointer to a RepairInfo structure.</param>
Expand Down Expand Up @@ -731,7 +731,7 @@ HRESULT HighUtilization([Optional, MarshalAs(UnmanagedType.LPWStr)] string? pwsz
// https://docs.microsoft.com/en-us/windows/win32/api/ndhelper/nf-ndhelper-inetdiaghelper-getrepairinfo HRESULT GetRepairInfo( [in]
// PROBLEM_TYPE problem, [out] ULONG *pcelt, [out] RepairInfo **ppInfo );
[PreserveSig]
HRESULT GetRepairInfo([In] PROBLEM_TYPE problem, out uint pcelt, [Out, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)] out RepairInfo[] ppInfo);
HRESULT GetRepairInfo([In] PROBLEM_TYPE problem, out uint pcelt, [Out, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)] out RepairInfo[]? ppInfo);

/// <summary>The <c>GetLifeTime</c> method retrieves the lifetime of the Helper Class Extension instance.</summary>
/// <param name="pLifeTime">A pointer to a LIFE_TIME structure.</param>
Expand Down Expand Up @@ -951,7 +951,7 @@ HRESULT HighUtilization([Optional, MarshalAs(UnmanagedType.LPWStr)] string? pwsz
// https://docs.microsoft.com/en-us/windows/win32/api/ndhelper/nf-ndhelper-inetdiaghelper-getattributes HRESULT GetAttributes( [out]
// ULONG *pcelt, [out] HELPER_ATTRIBUTE **pprgAttributes );
[PreserveSig]
HRESULT GetAttributes(out uint pcelt, [Out, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)] out HELPER_ATTRIBUTE[] pprgAttributes);
HRESULT GetAttributes(out uint pcelt, [Out, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)] out HELPER_ATTRIBUTE[]? pprgAttributes);

/// <summary>The <c>Cancel</c> method cancels an ongoing diagnosis or repair.</summary>
/// <returns>
Expand Down Expand Up @@ -1128,7 +1128,7 @@ public interface INetDiagHelperInfo
// https://docs.microsoft.com/en-us/windows/win32/api/ndhelper/nf-ndhelper-inetdiaghelperinfo-getattributeinfo HRESULT
// GetAttributeInfo( [out] ULONG *pcelt, [out] HelperAttributeInfo **pprgAttributeInfos );
[PreserveSig]
HRESULT GetAttributeInfo(out uint pcelt, [Out, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)] out HelperAttributeInfo[] pprgAttributeInfos);
HRESULT GetAttributeInfo(out uint pcelt, [Out, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)] out HelperAttributeInfo[]? pprgAttributeInfos);
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion PInvoke/NdfApi/NdfApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -849,7 +849,7 @@ public static extern HRESULT NdfCreatePnrpIncident([MarshalAs(UnmanagedType.LPWS
[DllImport(Lib_Ndfapi, SetLastError = false, ExactSpelling = true)]
[PInvokeData("ndfapi.h", MSDNShortId = "NF:ndfapi.NdfCreateWebIncidentEx")]
public static extern HRESULT NdfCreateWebIncidentEx([MarshalAs(UnmanagedType.LPWStr)] string url, [MarshalAs(UnmanagedType.Bool)] bool useWinHTTP,
[MarshalAs(UnmanagedType.LPWStr)] string moduleName, out SafeNDFHANDLE handle);
[MarshalAs(UnmanagedType.LPWStr)] string? moduleName, out SafeNDFHANDLE handle);

/// <summary>The <c>NdfCreateWinSockIncident</c> function provides access to the Winsock Helper Class provided by Microsoft.</summary>
/// <param name="sock">
Expand Down
3 changes: 3 additions & 0 deletions UnitTests/PInvoke/NdfApi/NdfApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
<PropertyGroup>
<AssemblyName>UnitTest.PInvoke.NdfApi</AssemblyName>
<RegisterForComInterop>false</RegisterForComInterop>
<TargetFramework>net6.0-windows</TargetFramework>
<EnableComHosting>true</EnableComHosting>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\PInvoke\NdfApi\Vanara.PInvoke.NdfApi.csproj" />
<PackageReference Include="dSPACE.Runtime.InteropServices" Version="1.1.3" />
</ItemGroup>
</Project>
36 changes: 18 additions & 18 deletions UnitTests/PInvoke/NdfApi/NdfApiTests.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using NUnit.Framework;
using NUnit.Framework.Internal;
using System;
using System.Runtime.InteropServices;
using dSPACE.Runtime.InteropServices;
using Vanara.PInvoke;
using Vanara.PInvoke.Tests;
using static Vanara.PInvoke.AdvApi32;
using static Vanara.PInvoke.Kernel32;
using static Vanara.PInvoke.NdfApi;
using dSPACE.Runtime.InteropServices.ComTypes;

namespace NdfApi;

Expand All @@ -25,7 +25,7 @@ public void ConnectivtyTest()
Assert.That(NdfCreateConnectivityIncident(out SafeNDFHANDLE hNDF), ResultIs.Successful);
Assert.That(hNDF, ResultIs.ValidHandle);
Assert.That(NdfExecuteDiagnosis(hNDF), ResultIs.Successful);
Assert.That(() => hNDF.Dispose(), Throws.Nothing);
Assert.That(hNDF.Dispose, Throws.Nothing);
}

[Test]
Expand All @@ -34,7 +34,7 @@ public void DNSTest()
Assert.That(NdfCreateDNSIncident("microsoft.com", 0, out SafeNDFHANDLE hNDF), ResultIs.Successful);
Assert.That(hNDF, ResultIs.ValidHandle);
Assert.That(NdfExecuteDiagnosis(hNDF), ResultIs.Successful);
Assert.That(() => hNDF.Dispose(), Throws.Nothing);
Assert.That(hNDF.Dispose, Throws.Nothing);
}

[Test]
Expand All @@ -43,7 +43,7 @@ public void NetConnectionTest()
Assert.That(NdfCreateNetConnectionIncident(out SafeNDFHANDLE hNDF), ResultIs.Successful);
Assert.That(hNDF, ResultIs.ValidHandle);
Assert.That(NdfExecuteDiagnosis(hNDF), ResultIs.Successful);
Assert.That(() => hNDF.Dispose(), Throws.Nothing);
Assert.That(hNDF.Dispose, Throws.Nothing);
}

[Test]
Expand All @@ -52,7 +52,7 @@ public void WebTest()
Assert.That(NdfCreateWebIncident("https://www.microsoft.com", out SafeNDFHANDLE hNDF), ResultIs.Successful);
Assert.That(hNDF, ResultIs.ValidHandle);
Assert.That(NdfExecuteDiagnosis(hNDF), ResultIs.Successful);
Assert.That(() => hNDF.Dispose(), Throws.Nothing);
Assert.That(hNDF.Dispose, Throws.Nothing);
}
}

Expand All @@ -61,7 +61,7 @@ public void WebTest()
public class SimpleFileHelperClass : INetDiagHelper
{
public static readonly Guid ID_LowHealthRepair = new("A9DF3AF6-6729-40E1-85CC-494F258E21A2");
private string m_pwszTestFile;
private string? m_pwszTestFile;
private static int cookie;
private const string coRegKey = @"CurrentControlSet\Control\NetDiagFx\VanaraTest";
private const string regKey = $@"{coRegKey}\HostDLLs\{nameof(SimpleFileHelperClass)}\HelperClasses\{nameof(SimpleFileHelperClass)}";
Expand Down Expand Up @@ -91,7 +91,7 @@ public static void Unregister()

HRESULT INetDiagHelper.Cleanup() => HRESULT.E_NOTIMPL;

HRESULT INetDiagHelper.GetAttributes(out uint pcelt, out HELPER_ATTRIBUTE[] pprgAttributes)
HRESULT INetDiagHelper.GetAttributes(out uint pcelt, out HELPER_ATTRIBUTE[]? pprgAttributes)
{
pcelt = default; pprgAttributes = default; return HRESULT.E_NOTIMPL;
}
Expand All @@ -106,17 +106,17 @@ unsafe HRESULT INetDiagHelper.GetDiagnosticsInfo(out DiagnosticsInfo* ppInfo)
ppInfo = default; return HRESULT.E_NOTIMPL;
}

HRESULT INetDiagHelper.GetDownStreamHypotheses(out uint pcelt, out HYPOTHESIS[] pprgHypotheses)
HRESULT INetDiagHelper.GetDownStreamHypotheses(out uint pcelt, out HYPOTHESIS[]? pprgHypotheses)
{
pcelt = default; pprgHypotheses = default; return HRESULT.E_NOTIMPL;
}

HRESULT INetDiagHelper.GetHigherHypotheses(out uint pcelt, out HYPOTHESIS[] pprgHypotheses)
HRESULT INetDiagHelper.GetHigherHypotheses(out uint pcelt, out HYPOTHESIS[]? pprgHypotheses)
{
pcelt = default; pprgHypotheses = default; return HRESULT.E_NOTIMPL;
}

HRESULT INetDiagHelper.GetKeyAttributes(out uint pcelt, out HELPER_ATTRIBUTE[] pprgAttributes)
HRESULT INetDiagHelper.GetKeyAttributes(out uint pcelt, out HELPER_ATTRIBUTE[]? pprgAttributes)
{
pcelt = default; pprgAttributes = default; return HRESULT.E_NOTIMPL;
}
Expand All @@ -126,12 +126,12 @@ HRESULT INetDiagHelper.GetLifeTime(out LIFE_TIME pLifeTime)
pLifeTime = default; return HRESULT.E_NOTIMPL;
}

HRESULT INetDiagHelper.GetLowerHypotheses(out uint pcelt, out HYPOTHESIS[] pprgHypotheses)
HRESULT INetDiagHelper.GetLowerHypotheses(out uint pcelt, out HYPOTHESIS[]? pprgHypotheses)
{
pcelt = default; pprgHypotheses = default; return HRESULT.E_NOTIMPL;
}

HRESULT INetDiagHelper.GetRepairInfo(PROBLEM_TYPE problem, out uint pcelt, out RepairInfo[] ppInfo)
HRESULT INetDiagHelper.GetRepairInfo(PROBLEM_TYPE problem, out uint pcelt, out RepairInfo[]? ppInfo)
{
RepairInfo pRepair = default;

Expand All @@ -155,12 +155,12 @@ HRESULT INetDiagHelper.GetRepairInfo(PROBLEM_TYPE problem, out uint pcelt, out R
return HRESULT.S_OK;
}

HRESULT INetDiagHelper.GetUpStreamHypotheses(out uint pcelt, out HYPOTHESIS[] pprgHypotheses)
HRESULT INetDiagHelper.GetUpStreamHypotheses(out uint pcelt, out HYPOTHESIS[]? pprgHypotheses)
{
pcelt = default; pprgHypotheses = default; return HRESULT.E_NOTIMPL;
}

HRESULT INetDiagHelper.HighUtilization(string pwszInstanceDescription, out string ppwszDescription, out long pDeferredTime, out DIAGNOSIS_STATUS pStatus)
HRESULT INetDiagHelper.HighUtilization(string? pwszInstanceDescription, out string? ppwszDescription, out long pDeferredTime, out DIAGNOSIS_STATUS pStatus)
{ ppwszDescription = default; pDeferredTime = default; pStatus = default; return HRESULT.E_NOTIMPL; }

HRESULT INetDiagHelper.Initialize(uint celt, HELPER_ATTRIBUTE[] rgAttributes)
Expand All @@ -186,11 +186,11 @@ HRESULT INetDiagHelper.Initialize(uint celt, HELPER_ATTRIBUTE[] rgAttributes)
}
}

HRESULT INetDiagHelper.LowHealth(string pwszInstanceDescription, out string ppwszDescription,
HRESULT INetDiagHelper.LowHealth(string? pwszInstanceDescription, out string? ppwszDescription,
out long pDeferredTime, out DIAGNOSIS_STATUS pStatus)
{
// does the file already exist?
using SafeHFILE hFile = CreateFile(m_pwszTestFile,
using SafeHFILE hFile = CreateFile(m_pwszTestFile!,
FileAccess.GENERIC_READ,
0,
default,
Expand Down Expand Up @@ -219,7 +219,7 @@ HRESULT INetDiagHelper.Repair(in RepairInfo pInfo, out long pDeferredTime, out R
//verify expected repair was requested
if (ID_LowHealthRepair == pInfo.guid)
{
using SafeHFILE hFile = CreateFile(m_pwszTestFile,
using SafeHFILE hFile = CreateFile(m_pwszTestFile!,
FileAccess.GENERIC_WRITE,
0,
default,
Expand Down

0 comments on commit aa34fe9

Please sign in to comment.