diff --git a/Net/DistributedRoutingTable.cs b/Net/DistributedRoutingTable.cs index a0a042ea..f71daa9b 100644 --- a/Net/DistributedRoutingTable.cs +++ b/Net/DistributedRoutingTable.cs @@ -233,7 +233,7 @@ public class DistributedRoutingTable evt = CreateEvent(null, false, false); DrtOpen(pSettings, evt, default, out HDRT h).ThrowIfFailed(); hDrt = new((IntPtr)h); - selfPin = GCHandle.Alloc(this).AddrOfPinnedObject(); + selfPin = (IntPtr)GCHandle.Alloc(this, GCHandleType.Normal); Win32Error.ThrowLastErrorIfFalse(RegisterWaitForSingleObject(out drtWaitEvent, evt, DrtEventCallback, selfPin, INFINITE, WT.WT_EXECUTEDEFAULT)); } diff --git a/PInvoke/Drt/Drt.cs b/PInvoke/Drt/Drt.cs index 91382e9c..06f1dfbc 100644 --- a/PInvoke/Drt/Drt.cs +++ b/PInvoke/Drt/Drt.cs @@ -13,6 +13,8 @@ namespace Vanara.PInvoke public static partial class Drt { private const string Lib_Drt = "drt.dll"; + private const string Lib_DrtProv = "drtprov.dll"; + private const string Lib_DrtTrans = "drttransport.dll"; /// public const uint DRT_PAYLOAD_REVOKED = (1 << 0); @@ -585,7 +587,7 @@ namespace Vanara.PInvoke /// // https://docs.microsoft.com/en-us/windows/win32/api/drt/nf-drt-drtcreatederivedkey HRESULT DrtCreateDerivedKey( PCCERT_CONTEXT // pLocalCert, DRT_DATA *pKey ); - [DllImport(Lib_Drt, SetLastError = false, ExactSpelling = true)] + [DllImport(Lib_DrtProv, SetLastError = false, ExactSpelling = true)] [PInvokeData("drt.h", MSDNShortId = "NF:drt.DrtCreateDerivedKey")] public static extern HRESULT DrtCreateDerivedKey(PCCERT_CONTEXT pLocalCert, out DRT_DATA pKey); @@ -629,7 +631,7 @@ namespace Vanara.PInvoke // https://docs.microsoft.com/en-us/windows/win32/api/drt/nf-drt-drtcreatederivedkeysecurityprovider HRESULT // DrtCreateDerivedKeySecurityProvider( PCCERT_CONTEXT pRootCert, PCCERT_CONTEXT pLocalCert, DRT_SECURITY_PROVIDER // **ppSecurityProvider ); - [DllImport(Lib_Drt, SetLastError = false, ExactSpelling = true)] + [DllImport(Lib_DrtProv, SetLastError = false, ExactSpelling = true)] [PInvokeData("drt.h", MSDNShortId = "NF:drt.DrtCreateDerivedKeySecurityProvider")] public static extern HRESULT DrtCreateDerivedKeySecurityProvider(PCCERT_CONTEXT pRootCert, PCCERT_CONTEXT pLocalCert, out IntPtr ppSecurityProvider); @@ -660,7 +662,7 @@ namespace Vanara.PInvoke /// // https://docs.microsoft.com/en-us/windows/win32/api/drt/nf-drt-drtcreatednsbootstrapresolver HRESULT // DrtCreateDnsBootstrapResolver( USHORT port, PCWSTR pwszAddress, DRT_BOOTSTRAP_PROVIDER **ppModule ); - [DllImport(Lib_Drt, SetLastError = false, ExactSpelling = true)] + [DllImport(Lib_DrtProv, SetLastError = false, ExactSpelling = true)] [PInvokeData("drt.h", MSDNShortId = "NF:drt.DrtCreateDnsBootstrapResolver")] public static extern HRESULT DrtCreateDnsBootstrapResolver(ushort port, [MarshalAs(UnmanagedType.LPWStr)] string pwszAddress, out IntPtr ppModule); @@ -724,7 +726,7 @@ namespace Vanara.PInvoke /// // https://docs.microsoft.com/en-us/windows/win32/api/drt/nf-drt-drtcreateipv6udptransport HRESULT DrtCreateIpv6UdpTransport( // DRT_SCOPE scope, ULONG dwScopeId, ULONG dwLocalityThreshold, USHORT *pwPort, HDRT_TRANSPORT *phTransport ); - [DllImport(Lib_Drt, SetLastError = false, ExactSpelling = true)] + [DllImport(Lib_DrtTrans, SetLastError = false, ExactSpelling = true)] [PInvokeData("drt.h", MSDNShortId = "NF:drt.DrtCreateIpv6UdpTransport")] public static extern HRESULT DrtCreateIpv6UdpTransport(DRT_SCOPE scope, uint dwScopeId, uint dwLocalityThreshold, ref ushort pwPort, out HDRT_TRANSPORT phTransport); @@ -752,7 +754,7 @@ namespace Vanara.PInvoke /// // https://docs.microsoft.com/en-us/windows/win32/api/drt/nf-drt-drtcreatenullsecurityprovider HRESULT // DrtCreateNullSecurityProvider( DRT_SECURITY_PROVIDER **ppSecurityProvider ); - [DllImport(Lib_Drt, SetLastError = false, ExactSpelling = true)] + [DllImport(Lib_DrtProv, SetLastError = false, ExactSpelling = true)] [PInvokeData("drt.h", MSDNShortId = "NF:drt.DrtCreateNullSecurityProvider")] public static extern HRESULT DrtCreateNullSecurityProvider(out IntPtr ppSecurityProvider); @@ -808,7 +810,7 @@ namespace Vanara.PInvoke // https://docs.microsoft.com/en-us/windows/win32/api/drt/nf-drt-drtcreatepnrpbootstrapresolver HRESULT // DrtCreatePnrpBootstrapResolver( BOOL fPublish, PCWSTR pwzPeerName, PCWSTR pwzCloudName, PCWSTR pwzPublishingIdentity, // DRT_BOOTSTRAP_PROVIDER **ppResolver ); - [DllImport(Lib_Drt, SetLastError = false, ExactSpelling = true)] + [DllImport(Lib_DrtProv, SetLastError = false, ExactSpelling = true)] [PInvokeData("drt.h", MSDNShortId = "NF:drt.DrtCreatePnrpBootstrapResolver")] public static extern HRESULT DrtCreatePnrpBootstrapResolver([MarshalAs(UnmanagedType.Bool)] bool fPublish, [MarshalAs(UnmanagedType.LPWStr)] string pwzPeerName, [Optional, MarshalAs(UnmanagedType.LPWStr)] string pwzCloudName, [Optional, MarshalAs(UnmanagedType.LPWStr)] string pwzPublishingIdentity, @@ -821,7 +823,7 @@ namespace Vanara.PInvoke /// None // https://docs.microsoft.com/en-us/windows/win32/api/drt/nf-drt-drtdeletederivedkeysecurityprovider void // DrtDeleteDerivedKeySecurityProvider( DRT_SECURITY_PROVIDER *pSecurityProvider ); - [DllImport(Lib_Drt, SetLastError = false, ExactSpelling = true)] + [DllImport(Lib_DrtProv, SetLastError = false, ExactSpelling = true)] [PInvokeData("drt.h", MSDNShortId = "NF:drt.DrtDeleteDerivedKeySecurityProvider")] public static extern void DrtDeleteDerivedKeySecurityProvider(in DRT_SECURITY_PROVIDER pSecurityProvider); @@ -832,7 +834,7 @@ namespace Vanara.PInvoke /// None // https://docs.microsoft.com/en-us/windows/win32/api/drt/nf-drt-drtdeletederivedkeysecurityprovider void // DrtDeleteDerivedKeySecurityProvider( DRT_SECURITY_PROVIDER *pSecurityProvider ); - [DllImport(Lib_Drt, SetLastError = false, ExactSpelling = true)] + [DllImport(Lib_DrtProv, SetLastError = false, ExactSpelling = true)] [PInvokeData("drt.h", MSDNShortId = "NF:drt.DrtDeleteDerivedKeySecurityProvider")] public static extern void DrtDeleteDerivedKeySecurityProvider([In] IntPtr pSecurityProvider); @@ -841,7 +843,7 @@ namespace Vanara.PInvoke /// None // https://docs.microsoft.com/en-us/windows/win32/api/drt/nf-drt-drtdeletednsbootstrapresolver void DrtDeleteDnsBootstrapResolver( // DRT_BOOTSTRAP_PROVIDER *pResolver ); - [DllImport(Lib_Drt, SetLastError = false, ExactSpelling = true)] + [DllImport(Lib_DrtProv, SetLastError = false, ExactSpelling = true)] [PInvokeData("drt.h", MSDNShortId = "NF:drt.DrtDeleteDnsBootstrapResolver")] public static extern void DrtDeleteDnsBootstrapResolver(in DRT_BOOTSTRAP_PROVIDER pResolver); @@ -850,7 +852,7 @@ namespace Vanara.PInvoke /// None // https://docs.microsoft.com/en-us/windows/win32/api/drt/nf-drt-drtdeletednsbootstrapresolver void DrtDeleteDnsBootstrapResolver( // DRT_BOOTSTRAP_PROVIDER *pResolver ); - [DllImport(Lib_Drt, SetLastError = false, ExactSpelling = true)] + [DllImport(Lib_DrtProv, SetLastError = false, ExactSpelling = true)] [PInvokeData("drt.h", MSDNShortId = "NF:drt.DrtDeleteDnsBootstrapResolver")] public static extern void DrtDeleteDnsBootstrapResolver([In] IntPtr pResolver); @@ -884,7 +886,7 @@ namespace Vanara.PInvoke /// // https://docs.microsoft.com/en-us/windows/win32/api/drt/nf-drt-drtdeleteipv6udptransport HRESULT DrtDeleteIpv6UdpTransport( // HDRT_TRANSPORT hTransport ); - [DllImport(Lib_Drt, SetLastError = false, ExactSpelling = true)] + [DllImport(Lib_DrtTrans, SetLastError = false, ExactSpelling = true)] [PInvokeData("drt.h", MSDNShortId = "NF:drt.DrtDeleteIpv6UdpTransport")] public static extern HRESULT DrtDeleteIpv6UdpTransport(HDRT_TRANSPORT hTransport); @@ -893,7 +895,7 @@ namespace Vanara.PInvoke /// None // https://docs.microsoft.com/en-us/windows/win32/api/drt/nf-drt-drtdeletenullsecurityprovider void DrtDeleteNullSecurityProvider( // DRT_SECURITY_PROVIDER *pSecurityProvider ); - [DllImport(Lib_Drt, SetLastError = false, ExactSpelling = true)] + [DllImport(Lib_DrtProv, SetLastError = false, ExactSpelling = true)] [PInvokeData("drt.h", MSDNShortId = "NF:drt.DrtDeleteNullSecurityProvider")] public static extern void DrtDeleteNullSecurityProvider(in DRT_SECURITY_PROVIDER pSecurityProvider); @@ -902,7 +904,7 @@ namespace Vanara.PInvoke /// None // https://docs.microsoft.com/en-us/windows/win32/api/drt/nf-drt-drtdeletenullsecurityprovider void DrtDeleteNullSecurityProvider( // DRT_SECURITY_PROVIDER *pSecurityProvider ); - [DllImport(Lib_Drt, SetLastError = false, ExactSpelling = true)] + [DllImport(Lib_DrtProv, SetLastError = false, ExactSpelling = true)] [PInvokeData("drt.h", MSDNShortId = "NF:drt.DrtDeleteNullSecurityProvider")] public static extern void DrtDeleteNullSecurityProvider([In] IntPtr pSecurityProvider); @@ -913,7 +915,7 @@ namespace Vanara.PInvoke /// None // https://docs.microsoft.com/en-us/windows/win32/api/drt/nf-drt-drtdeletepnrpbootstrapresolver void DrtDeletePnrpBootstrapResolver( // DRT_BOOTSTRAP_PROVIDER *pResolver ); - [DllImport(Lib_Drt, SetLastError = false, ExactSpelling = true)] + [DllImport(Lib_DrtProv, SetLastError = false, ExactSpelling = true)] [PInvokeData("drt.h", MSDNShortId = "NF:drt.DrtDeletePnrpBootstrapResolver")] public static extern void DrtDeletePnrpBootstrapResolver(in DRT_BOOTSTRAP_PROVIDER pResolver); @@ -924,7 +926,7 @@ namespace Vanara.PInvoke /// None // https://docs.microsoft.com/en-us/windows/win32/api/drt/nf-drt-drtdeletepnrpbootstrapresolver void DrtDeletePnrpBootstrapResolver( // DRT_BOOTSTRAP_PROVIDER *pResolver ); - [DllImport(Lib_Drt, SetLastError = false, ExactSpelling = true)] + [DllImport(Lib_DrtProv, SetLastError = false, ExactSpelling = true)] [PInvokeData("drt.h", MSDNShortId = "NF:drt.DrtDeletePnrpBootstrapResolver")] public static extern void DrtDeletePnrpBootstrapResolver([In] IntPtr pResolver); diff --git a/PInvoke/Drt/Vanara.PInvoke.Drt.csproj b/PInvoke/Drt/Vanara.PInvoke.Drt.csproj index 97518f2f..f1206e4f 100644 --- a/PInvoke/Drt/Vanara.PInvoke.Drt.csproj +++ b/PInvoke/Drt/Vanara.PInvoke.Drt.csproj @@ -1,7 +1,7 @@  - Drt.dll + drt.dll;drtprov.dll;drttransport.dll PInvoke API (methods, structures and constants) imported from Windows Drt.dll for the Distributed Routing Table (DRT) API. diff --git a/UnitTests/Net/Net.csproj b/UnitTests/Net/Net.csproj index fb211020..06978e0f 100644 --- a/UnitTests/Net/Net.csproj +++ b/UnitTests/Net/Net.csproj @@ -4,5 +4,6 @@ + \ No newline at end of file diff --git a/UnitTests/Net/NetTests.cs b/UnitTests/Net/NetTests.cs index 7be0e12b..226e5b2b 100644 --- a/UnitTests/Net/NetTests.cs +++ b/UnitTests/Net/NetTests.cs @@ -2,12 +2,16 @@ using NUnit.Framework; using NUnit.Framework.Internal; using System; +using System.Collections.Generic; +using System.Diagnostics; using System.Runtime.InteropServices; using System.Threading; using System.Threading.Tasks; using System.Xml.Linq; +using Vanara.InteropServices; using Vanara.Net; using static Vanara.PInvoke.Dhcp; +using static Vanara.PInvoke.FirewallApi; namespace Vanara.PInvoke.Tests; @@ -71,4 +75,63 @@ public class NetTests client.Dispose(); } } + + [Test] + public void TestDrt() + { + using var fw = new Firewall(); + fw.AddApp(Process.GetCurrentProcess().ProcessName, Process.GetCurrentProcess().MainModule.FileName); + try + { + DistributedRoutingTable drt = new(null, DrtBootstrapProvider.CreateDnsBootstrapResolver(null, 0)); + + } + finally + { + try { fw.RemoveApp(Process.GetCurrentProcess().MainModule.FileName); } catch { } + } + } + + private class Firewall : IDisposable + { + Dictionary> newApps = new(); + ComReleaser pMgr; + ComReleaser pPol; + ComReleaser pProf; + ComReleaser pSet; + + public Firewall() + { + pMgr = ComReleaserFactory.Create(new INetFwMgr()); + Assert.IsNotNull(pMgr.Item); + pPol = ComReleaserFactory.Create(pMgr.Item.LocalPolicy); + Assert.IsNotNull(pPol.Item); + pProf = ComReleaserFactory.Create(pPol.Item.CurrentProfile); + Assert.IsNotNull(pProf.Item); + pSet = ComReleaserFactory.Create(pProf.Item.AuthorizedApplications); + Assert.IsNotNull(pSet.Item); + } + + public void AddApp(string name, string exePath) + { + var pNewApp = ComReleaserFactory.Create(new INetFwAuthorizedApplication()); + pNewApp.Item.Name = name; + pNewApp.Item.ProcessImageFileName = exePath; + pNewApp.Item.Enabled = true; + pSet.Item.Add(pNewApp.Item); + newApps.Add(exePath, pNewApp); + } + + public void Dispose() + { + pSet.Dispose(); + pProf.Dispose(); + pPol.Dispose(); + pMgr.Dispose(); + foreach (var app in newApps.Values) + app.Dispose(); + } + + public void RemoveApp(string exePath) => pSet.Item.Remove(exePath); + } } \ No newline at end of file