More work on NdfApi

pull/328/head
dahall 2022-08-10 14:34:37 -06:00
parent 68c88f4508
commit 5c0dbc913b
6 changed files with 289 additions and 5 deletions

View File

@ -126,10 +126,10 @@ public static partial class NdfApi
RF_CONTACT_ADMIN = 0x20000,
/// <summary>Reserved for system use.</summary>
RF_RESERVED = 0x40000000,
DF_TRACELESS = 0x40000000,
/// <summary>Reserved for system use.</summary>
RF_RESERVED_CA = 0x80000000,
DF_IMPERSONATION = 0x80000000,
/// <summary>Reserved for system use.</summary>
RF_RESERVED_LNI = 0x10000,
@ -1194,7 +1194,7 @@ public static partial class NdfApi
public string pwszDescription;
/// <summary>One of the WELL_KNOWN_SID_TYPE if the repair requires certain user contexts or privileges.</summary>
public uint sidType;
public AdvApi32.WELL_KNOWN_SID_TYPE sidType;
/// <summary>The number of seconds required to perform the repair.</summary>
public long cost;

View File

@ -649,7 +649,7 @@ public static partial class NdfApi
// NdfCreateNetConnectionIncident( [out] NDFHANDLE *handle, GUID id );
[DllImport(Lib_Ndfapi, SetLastError = false, ExactSpelling = true)]
[PInvokeData("ndfapi.h", MSDNShortId = "NF:ndfapi.NdfCreateNetConnectionIncident")]
public static extern HRESULT NdfCreateNetConnectionIncident(out SafeNDFHANDLE handle, Guid id);
public static extern HRESULT NdfCreateNetConnectionIncident(out SafeNDFHANDLE handle, [In, Optional] Guid id);
/// <summary>
/// The <c>NdfCreatePnrpIncident</c> function creates a session to diagnose issues with the Peer Name Resolution Protocol (PNRP) service.
@ -1076,7 +1076,7 @@ public static partial class NdfApi
// HWND hwnd );
[DllImport(Lib_Ndfapi, SetLastError = false, ExactSpelling = true)]
[PInvokeData("ndfapi.h", MSDNShortId = "NF:ndfapi.NdfExecuteDiagnosis")]
public static extern HRESULT NdfExecuteDiagnosis(NDFHANDLE handle, HWND hwnd);
public static extern HRESULT NdfExecuteDiagnosis(NDFHANDLE handle, [In, Optional] HWND hwnd);
/// <summary>
/// The <c>NdfGetTraceFile</c> function is used to retrieve the path containing an Event Trace Log (ETL) file that contains Event Tracing

View File

@ -15,6 +15,7 @@
<ItemGroup>
<ProjectReference Include="..\..\Core\Vanara.Core.csproj" />
<ProjectReference Include="..\Shared\Vanara.PInvoke.Shared.csproj" />
<ProjectReference Include="..\Security\Vanara.PInvoke.Security.csproj" />
<ProjectReference Include="..\Ws2_32\Vanara.PInvoke.Ws2_32.csproj" />
</ItemGroup>
</Project>

View File

@ -9772,5 +9772,29 @@ namespace Vanara.PInvoke
/// <summary>Each index must have a minimum size of ten sectors.</summary>
public const int E_IMAPI_RAW_IMAGE_TRACK_INDEX_TOO_CLOSE_TO_OTHER_INDEX = unchecked((int)0x80AA0A0A);
/// <summary></summary>
public const int NDF_E_LENGTH_EXCEEDED = unchecked((int)0x8008F900);
/// <summary>Helper Class parameter not specified to NdfCreateIncident API.</summary>
public const int NDF_E_NOHELPERCLASS = unchecked((int)0x8008F901);
/// <summary></summary>
public const int NDF_E_CANCELLED = unchecked((int)0x8008F902);
/// <summary></summary>
public const int NDF_E_DISABLED = unchecked((int)0x8008F903);
/// <summary>Inavlid parameter.</summary>
public const int NDF_E_BAD_PARAM = unchecked((int)0x8008F905);
/// <summary>Diagnosis failed to resolve the problems.</summary>
public const int NDF_E_VALIDATION = unchecked((int)0x8008F906);
/// <summary>Diagnostics session result is unkown, the diagnostics phase did not complete.</summary>
public const int NDF_E_UNKNOWN = unchecked((int)0x8008F907);
/// <summary>Diagnostics session finished with problems still present.</summary>
public const int NDF_E_PROBLEM_PRESENT = unchecked((int)0x8008F908);
}
}

View File

@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<AssemblyName>UnitTest.PInvoke.NdfApi</AssemblyName>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<SignAssembly>True</SignAssembly>
<AssemblyOriginatorKeyFile>..\..\..\Vanara.snk</AssemblyOriginatorKeyFile>
<RegisterForComInterop>false</RegisterForComInterop>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\PInvoke\NdfApi\Vanara.PInvoke.NdfApi.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,247 @@
using NUnit.Framework;
using NUnit.Framework.Internal;
using System;
using System.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 FILETIME = System.Runtime.InteropServices.ComTypes.FILETIME;
namespace NdfApi;
[TestFixture]
public class NdfApiTests
{
[OneTimeSetUp]
public void _Setup() => SimpleFileHelperClass.Register();
[OneTimeTearDown]
public void _TearDown() => SimpleFileHelperClass.Unregister();
[Test]
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);
}
[Test]
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);
}
[Test]
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);
}
[Test]
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);
}
}
[ComVisible(true)]
[Guid("6A331432-4B34-41C5-BFB9-C6FFD2EE4E00")]
public class SimpleFileHelperClass : INetDiagHelper
{
public static readonly Guid ID_LowHealthRepair = new("A9DF3AF6-6729-40E1-85CC-494F258E21A2");
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)}";
public SimpleFileHelperClass() { }
public static void Register()
{
var svc = new RegistrationServices();
cookie = svc.RegisterTypeForComClients(typeof(SimpleFileHelperClass), RegistrationClassContext.InProcessServer, RegistrationConnectionType.MultipleUse);
using var regkey = Microsoft.Win32.Registry.LocalMachine.CreateSubKey(regKey);
regkey.SetValue("CLSID", typeof(SimpleFileHelperClass).GUID.ToString("B"));
regkey.SetValue("Published", 1U);
regkey.SetValue("Version", "1.0");
//regkey.SetValue("Parent", "");
}
public static void Unregister()
{
try { Microsoft.Win32.Registry.LocalMachine.DeleteSubKeyTree(coRegKey); } catch { }
var svc = new RegistrationServices();
svc.UnregisterTypeForComClients(cookie);
}
HRESULT INetDiagHelper.Cancel() => HRESULT.E_NOTIMPL;
HRESULT INetDiagHelper.Cleanup() => HRESULT.E_NOTIMPL;
HRESULT INetDiagHelper.GetAttributes(out uint pcelt, out HELPER_ATTRIBUTE[] pprgAttributes)
{
pcelt = default; pprgAttributes = default; return HRESULT.E_NOTIMPL;
}
HRESULT INetDiagHelper.GetCacheTime(out FILETIME pCacheTime)
{
pCacheTime = default; return HRESULT.E_NOTIMPL;
}
unsafe HRESULT INetDiagHelper.GetDiagnosticsInfo(out DiagnosticsInfo* ppInfo)
{
ppInfo = default; return HRESULT.E_NOTIMPL;
}
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)
{
pcelt = default; pprgHypotheses = default; return HRESULT.E_NOTIMPL;
}
HRESULT INetDiagHelper.GetKeyAttributes(out uint pcelt, out HELPER_ATTRIBUTE[] pprgAttributes)
{
pcelt = default; pprgAttributes = default; return HRESULT.E_NOTIMPL;
}
HRESULT INetDiagHelper.GetLifeTime(out LIFE_TIME pLifeTime)
{
pLifeTime = default; return HRESULT.E_NOTIMPL;
}
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)
{
RepairInfo pRepair = default;
// set the repair description and class name
pRepair.pwszClassName = nameof(SimpleFileHelperClass);
pRepair.pwszDescription = "Low Health Repair";
// set the resolution Guid and cost
pRepair.guid = ID_LowHealthRepair;
pRepair.cost = 0;
// set repair status flags
pRepair.sidType = WELL_KNOWN_SID_TYPE.WinWorldSid;
pRepair.scope = REPAIR_SCOPE.RS_SYSTEM;
pRepair.risk = REPAIR_RISK.RR_NORISK;
pRepair.flags |= REPAIR_FLAG.DF_IMPERSONATION; //impersonate the user when repairing
pRepair.UiInfo.type = UI_INFO_TYPE.UIT_NONE;
ppInfo = new[] { pRepair };
pcelt = 1; //number of repairs
return HRESULT.S_OK;
}
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)
{ ppwszDescription = default; pDeferredTime = default; pStatus = default; return HRESULT.E_NOTIMPL; }
HRESULT INetDiagHelper.Initialize(uint celt, HELPER_ATTRIBUTE[] rgAttributes)
{
if (celt < 1 || rgAttributes is null)
{
return HRESULT.E_INVALIDARG;
}
else
{
//verify the attribute is named as expected
if (string.Compare(rgAttributes[0].pwszName, "filename", true)==0)
{
//copy the attribute to member variable
m_pwszTestFile = rgAttributes[0].PWStr;
return 0;
}
else
{
//the attribute isn't named as expected
return HRESULT.E_INVALIDARG;
}
}
}
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,
FileAccess.GENERIC_READ,
0,
default,
System.IO.FileMode.Open,
FileFlagsAndAttributes.FILE_ATTRIBUTE_NORMAL,
default);
ppwszDescription = null;
pDeferredTime = 0;
pStatus = DIAGNOSIS_STATUS.DS_REJECTED;
if (hFile.IsInvalid)
{
ppwszDescription = "The file was deleted.";
pStatus = DIAGNOSIS_STATUS.DS_CONFIRMED;
}
return HRESULT.S_OK;
}
HRESULT INetDiagHelper.Repair(in RepairInfo pInfo, out long pDeferredTime, out REPAIR_STATUS pStatus)
{
pDeferredTime = 0;
pStatus = REPAIR_STATUS.RS_UNREPAIRED;
//verify expected repair was requested
if (ID_LowHealthRepair == pInfo.guid)
{
using SafeHFILE hFile = CreateFile(m_pwszTestFile,
FileAccess.GENERIC_WRITE,
0,
default,
System.IO.FileMode.Create,
FileFlagsAndAttributes.FILE_ATTRIBUTE_NORMAL,
default);
if (hFile.IsInvalid)
// repair ref failed pStatus = RS_UNREPAIRED;
return Win32Error.GetLastError().ToHRESULT();
pStatus = REPAIR_STATUS.RS_REPAIRED;
}
else
{
return HRESULT.E_INVALIDARG; //unkown repair passed in
}
return HRESULT.S_OK;
}
HRESULT INetDiagHelper.SetLifeTime(LIFE_TIME lifeTime) => HRESULT.E_NOTIMPL;
HRESULT INetDiagHelper.Validate(PROBLEM_TYPE problem, out long pDeferredTime, out REPAIR_STATUS pStatus)
{ pDeferredTime = default; pStatus = default; return HRESULT.E_NOTIMPL; }
}