global using System; global using System.Runtime.InteropServices; global using static Vanara.PInvoke.Ws2_32; global using FILETIME = System.Runtime.InteropServices.ComTypes.FILETIME; namespace Vanara.PInvoke; /// Items from the Network Diagnostic Framework (NdfApi.dll). public static partial class NdfApi { private const string Lib_Ndfapi = "ndfapi.dll"; /// Available flags for NdfDiagnoseIncident [Flags] public enum NDF_DIAG : uint { /// /// Turns on network tracing during diagnosis. Diagnostic results will be included in the Event Trace Log (ETL) file returned by NdfGetTraceFile. /// NDF_ADD_CAPTURE_TRACE = 0x0001, /// /// Applies filtering to the returned root causes so that they are consistent with the in-box scripted diagnostics behavior. Without /// this flag, root causes will not be filtered. This flag must be set by the caller, so existing callers will not see a change in /// behavior unless they explicitly specify this flag. /// NDF_APPLY_INCLUSION_LIST_FILTER = 0x0002, } /// Flags for NdfCreateInboundIncident. [PInvokeData("ndfapi.h", MSDNShortId = "NF:ndfapi.NdfCreateInboundIncident")] [Flags] public enum NDF_INBOUND_FLAG : uint { /// /// Indicates that a configuration should be considered unhealthy if both gateways on the local network are within the private access /// range, rather than only considering the configuration to be healthy if both gateways are Internet Gateway Devices (IGDs). /// NDF_INBOUND_FLAG_EDGETRAVERSAL = 0x00001, /// /// Ends the session immediately if no inbound traffic problems are diagnosed, instead of turning on detailed tracing and allowing /// the user to reproduce their problem for a second diagnosis. /// NDF_INBOUND_FLAG_HEALTHCHECK = 0x00002, } /// /// The NdfCancelIncident function is used to cancel unneeded functions which have been previously called on an existing incident. /// /// /// Type: NDFHANDLE /// Handle to the Network Diagnostics Framework incident. This handle should match the handle of an existing incident. /// /// /// Type: HRESULT /// Possible return values include, but are not limited to, the following. /// /// /// Return code /// Description /// /// /// S_OK /// The operation succeeded. /// /// /// Any result other than S_OK should be interpreted as an error. /// /// /// Before using this API, an application must call an incident creation function such as NdfCreateWebIncident. /// /// NdfCancelIncident is primarily used to cancel calls to functions such as NdfDiagnoseIncident or NdfRepairIncident which have /// been previously called, but are no longer needed. When NdfCancelIncident is called, NDF will stop the diagnosis/repair as soon /// as possible rather than calling the other functions (unless results have already been returned from those functions, in which case /// NdfCancelIncident will have no effect). /// /// /// NdfCloseIncident should be used to close an incident once it has been resolved, as NdfCancelIncident does not actually close /// the incident itself. /// /// // https://docs.microsoft.com/en-us/windows/win32/api/ndfapi/nf-ndfapi-ndfcancelincident HRESULT NdfCancelIncident( [in] NDFHANDLE Handle ); [DllImport(Lib_Ndfapi, SetLastError = false, ExactSpelling = true)] [PInvokeData("ndfapi.h", MSDNShortId = "NF:ndfapi.NdfCancelIncident")] public static extern HRESULT NdfCancelIncident([In] NDFHANDLE Handle); /// The NdfCloseIncident function is used to close an Network Diagnostics Framework (NDF) incident following its resolution. /// /// Type: NDFHANDLE /// Handle to the NDF incident that is being closed. /// /// /// Type: HRESULT /// Possible return values include, but are not limited to, the following. /// /// /// Return code /// Description /// /// /// S_OK /// The operation succeeded. /// /// /// // https://docs.microsoft.com/en-us/windows/win32/api/ndfapi/nf-ndfapi-ndfcloseincident HRESULT NdfCloseIncident( [in] NDFHANDLE handle ); [DllImport(Lib_Ndfapi, SetLastError = false, ExactSpelling = true)] [PInvokeData("ndfapi.h", MSDNShortId = "NF:ndfapi.NdfCloseIncident")] public static extern HRESULT NdfCloseIncident([In] NDFHANDLE handle); /// The NdfCreateConnectivityIncident function diagnoses generic Internet connectivity problems. /// /// Type: NDFHANDLE* /// Handle to the Network Diagnostics Framework incident. /// /// /// Type: HRESULT /// Possible return values include, but are not limited to, the following. /// /// /// Return code /// Description /// /// /// S_OK /// The operation succeeded. /// /// /// E_ABORT /// The underlying diagnosis or repair operation has been canceled. /// /// /// E_OUTOFMEMORY /// There is not enough memory available to complete this operation. /// /// /// NDF_E_BAD_PARAM /// The handle is invalid. /// /// /// // https://docs.microsoft.com/en-us/windows/win32/api/ndfapi/nf-ndfapi-ndfcreateconnectivityincident HRESULT // NdfCreateConnectivityIncident( [out] NDFHANDLE *handle ); [DllImport(Lib_Ndfapi, SetLastError = false, ExactSpelling = true)] [PInvokeData("ndfapi.h", MSDNShortId = "NF:ndfapi.NdfCreateConnectivityIncident")] public static extern HRESULT NdfCreateConnectivityIncident(out SafeNDFHANDLE handle); /// The NdfCreateDNSIncident function diagnoses name resolution issues in resolving a specific host name. /// /// Type: LPCWSTR /// The host name with which there is a name resolution issue. /// /// /// Type: WORD /// /// The numeric representation of the type of record that was queried when the issue occurred. For more information and a complete /// listing of record set types and their numeric representations, see the windns.h header file. /// /// This parameter should be set to DNS_TYPE_ZERO for generic DNS resolution diagnosis. /// /// /// Type: NDFHANDLE* /// Handle to the Network Diagnostics Framework incident. /// /// /// Type: HRESULT /// Possible return values include, but are not limited to, the following. /// /// /// Return code /// Description /// /// /// S_OK /// The operation succeeded. /// /// /// E_ABORT /// The underlying diagnosis or repair operation has been canceled. /// /// /// E_OUTOFMEMORY /// There is not enough memory available to complete this operation. /// /// /// NDF_E_BAD_PARAM /// One or more parameters are invalid. /// /// /// // https://docs.microsoft.com/en-us/windows/win32/api/ndfapi/nf-ndfapi-ndfcreatednsincident HRESULT NdfCreateDNSIncident( [in] LPCWSTR // hostname, WORD queryType, [out] NDFHANDLE *handle ); [DllImport(Lib_Ndfapi, SetLastError = false, ExactSpelling = true)] [PInvokeData("ndfapi.h", MSDNShortId = "NF:ndfapi.NdfCreateDNSIncident")] public static extern HRESULT NdfCreateDNSIncident([MarshalAs(UnmanagedType.LPWStr)] string hostname, ushort queryType, out SafeNDFHANDLE handle); /// The NdfCreateGroupingIncident function creates a session to diagnose peer-to-peer grouping functionality issues. /// /// Type: LPCWSTR /// /// The name of the Peer Name Resolution Protocol (PNRP) cloud where the group is created. If NULL, the session will not attempt /// to diagnose issues related to PNRP. /// /// /// /// Type: LPCWSTR /// The name of the group to be diagnosed. If NULL, the session will not attempt to diagnose issues related to group availability. /// /// /// Type: LPCWSTR /// /// The identity that a peer uses to access the group. If NULL, the session will not attempt to diagnose issues related to the /// group's ability to register in PNRP. /// /// /// /// Type: LPCWSTR /// /// An XML invitation granted by another peer. An invitation is created when the inviting peer calls PeerGroupCreateInvitation or /// PeerGroupIssueCredentials. If this value is present, the invitation will be checked to ensure its format and expiration are valid. /// /// /// /// Type: SOCKET_ADDRESS_LIST* /// /// Optional list of addresses of the peers to which the application is trying to connect. If this parameter is used, the helper class /// will diagnose connectivity to these addresses. /// /// /// /// Type: LPCWSTR /// Application ID for the calling application. /// /// /// Type: NDFHANDLE* /// Handle to the Network Diagnostics Framework incident. /// /// /// Type: HRESULT /// Possible return values include, but are not limited to, the following. /// /// /// Return code /// Description /// /// /// S_OK /// The operation succeeded. /// /// /// NDF_E_BAD_PARAM /// One or more parameters has not been provided correctly. /// /// /// /// /// The level of diagnosis performed depends on the parameters supplied. /// /// /// /// If no parameters are specified, NDF will validate the grouping service status, the status of peer-to-peer services (PNRP and Identity /// Manager), and Windows clock synchronization. /// /// /// /// If CloudName is specified, NDF will validate grouping functionality in that cloud. /// /// /// /// If GroupName is specified, NDF will validate that the name can be resolved in PNRP (or invoke the PNRP helper class if the /// name cannot be resolved) and validate the firewall settings for grouping. /// /// /// /// /// If Identity is specified, NDF will validate PNRP's ability to register the GroupName with this Identity. If this fails, /// the PNRP helper class will be invoked. /// /// /// /// /// If Invitation is specified, the GroupName will be derived from the Invitation (if a GroupName was not also /// specified) and NDF will validate the invitation's format and status. /// /// /// /// If Addresses is specified, NDF will validate whether Windows can connect to up to three of these addresses. /// /// /// // https://docs.microsoft.com/en-us/windows/win32/api/ndfapi/nf-ndfapi-ndfcreategroupingincident HRESULT NdfCreateGroupingIncident( [in, // optional] LPCWSTR CloudName, [in, optional] LPCWSTR GroupName, [in, optional] LPCWSTR Identity, [in, optional] LPCWSTR Invitation, // [in, optional] SOCKET_ADDRESS_LIST *Addresses, [in, optional] LPCWSTR appId, [out] NDFHANDLE *handle ); [DllImport(Lib_Ndfapi, SetLastError = false, ExactSpelling = true)] [PInvokeData("ndfapi.h", MSDNShortId = "NF:ndfapi.NdfCreateGroupingIncident")] public static extern HRESULT NdfCreateGroupingIncident([Optional, MarshalAs(UnmanagedType.LPWStr)] string CloudName, [Optional, MarshalAs(UnmanagedType.LPWStr)] string GroupName, [Optional, MarshalAs(UnmanagedType.LPWStr)] string Identity, [Optional, MarshalAs(UnmanagedType.LPWStr)] string Invitation, [In, Optional] IntPtr Addresses, [Optional, MarshalAs(UnmanagedType.LPWStr)] string appId, out SafeNDFHANDLE handle); /// The NdfCreateGroupingIncident function creates a session to diagnose peer-to-peer grouping functionality issues. /// /// Type: LPCWSTR /// /// The name of the Peer Name Resolution Protocol (PNRP) cloud where the group is created. If NULL, the session will not attempt /// to diagnose issues related to PNRP. /// /// /// /// Type: LPCWSTR /// The name of the group to be diagnosed. If NULL, the session will not attempt to diagnose issues related to group availability. /// /// /// Type: LPCWSTR /// /// The identity that a peer uses to access the group. If NULL, the session will not attempt to diagnose issues related to the /// group's ability to register in PNRP. /// /// /// /// Type: LPCWSTR /// /// An XML invitation granted by another peer. An invitation is created when the inviting peer calls PeerGroupCreateInvitation or /// PeerGroupIssueCredentials. If this value is present, the invitation will be checked to ensure its format and expiration are valid. /// /// /// /// Type: SOCKET_ADDRESS_LIST* /// /// Optional list of addresses of the peers to which the application is trying to connect. If this parameter is used, the helper class /// will diagnose connectivity to these addresses. /// /// /// /// Type: LPCWSTR /// Application ID for the calling application. /// /// /// Type: NDFHANDLE* /// Handle to the Network Diagnostics Framework incident. /// /// /// Type: HRESULT /// Possible return values include, but are not limited to, the following. /// /// /// Return code /// Description /// /// /// S_OK /// The operation succeeded. /// /// /// NDF_E_BAD_PARAM /// One or more parameters has not been provided correctly. /// /// /// /// /// The level of diagnosis performed depends on the parameters supplied. /// /// /// /// If no parameters are specified, NDF will validate the grouping service status, the status of peer-to-peer services (PNRP and Identity /// Manager), and Windows clock synchronization. /// /// /// /// If CloudName is specified, NDF will validate grouping functionality in that cloud. /// /// /// /// If GroupName is specified, NDF will validate that the name can be resolved in PNRP (or invoke the PNRP helper class if the /// name cannot be resolved) and validate the firewall settings for grouping. /// /// /// /// /// If Identity is specified, NDF will validate PNRP's ability to register the GroupName with this Identity. If this fails, /// the PNRP helper class will be invoked. /// /// /// /// /// If Invitation is specified, the GroupName will be derived from the Invitation (if a GroupName was not also /// specified) and NDF will validate the invitation's format and status. /// /// /// /// If Addresses is specified, NDF will validate whether Windows can connect to up to three of these addresses. /// /// /// // https://docs.microsoft.com/en-us/windows/win32/api/ndfapi/nf-ndfapi-ndfcreategroupingincident HRESULT NdfCreateGroupingIncident( [in, // optional] LPCWSTR CloudName, [in, optional] LPCWSTR GroupName, [in, optional] LPCWSTR Identity, [in, optional] LPCWSTR Invitation, // [in, optional] SOCKET_ADDRESS_LIST *Addresses, [in, optional] LPCWSTR appId, [out] NDFHANDLE *handle ); [DllImport(Lib_Ndfapi, SetLastError = false, ExactSpelling = true)] [PInvokeData("ndfapi.h", MSDNShortId = "NF:ndfapi.NdfCreateGroupingIncident")] public static extern HRESULT NdfCreateGroupingIncident([Optional, MarshalAs(UnmanagedType.LPWStr)] string CloudName, [Optional, MarshalAs(UnmanagedType.LPWStr)] string GroupName, [Optional, MarshalAs(UnmanagedType.LPWStr)] string Identity, [Optional, MarshalAs(UnmanagedType.LPWStr)] string Invitation, in SOCKET_ADDRESS_LIST Addresses, [Optional, MarshalAs(UnmanagedType.LPWStr)] string appId, out SafeNDFHANDLE handle); /// /// The NdfCreateInboundIncident function creates a session to diagnose inbound connectivity for a specific application or service. /// /// /// Type: LPCWSTR /// The fully qualified path to the application receiving the inbound traffic. /// /// /// Type: LPCWSTR /// The Windows service receiving the inbound traffic. /// dll,-28502 (File/Print Sharing) /// dll,-28752 (Remote Desktop) /// dll,-32752 (Network Discovery) /// /// /// Type: SID* /// The SID for the application receiving the traffic. If NULL, the caller's SID is automatically used. /// /// /// Type: const SOCKADDR_STORAGE /// /// A SOCKADDR_STORAGE structure which limits the diagnosis to traffic to a specific IP address. If NULL, all traffic will be /// included in the diagnosis. /// /// /// /// Type: IPPROTO /// The protocol which should be diagnosed. For example, IPPROTO_TCP would be used to indicate the TCP/IP protocol. /// /// /// Type: DWORD /// Possible values: /// NDF_INBOUND_FLAG_EDGETRAVERSAL /// /// Indicates that a configuration should be considered unhealthy if both gateways on the local network are within the private access /// range, rather than only considering the configuration to be healthy if both gateways are Internet Gateway Devices (IGDs). /// /// NDF_INBOUND_FLAG_HEALTHCHECK /// /// Ends the session immediately if no inbound traffic problems are diagnosed, instead of turning on detailed tracing and allowing the /// user to reproduce their problem for a second diagnosis. /// /// /// /// Type: NDFHANDLE* /// Pointer to a handle to the Network Diagnostics Framework incident. /// /// /// Type: HRESULT /// Possible return values include, but are not limited to, the following. /// /// /// Return code /// Description /// /// /// S_OK /// The operation succeeded. /// /// /// E_INVALIDARG /// One or more parameters has not been provided correctly. /// /// /// /// Either applicationID or serviceID must be specified, but not both. // https://docs.microsoft.com/en-us/windows/win32/api/ndfapi/nf-ndfapi-ndfcreateinboundincident HRESULT NdfCreateInboundIncident( [in, // optional] LPCWSTR applicationID, [in, optional] LPCWSTR serviceID, [in, optional] SID *userID, [in, optional] const SOCKADDR_STORAGE // *localTarget, IPPROTO protocol, DWORD dwFlags, [out] NDFHANDLE *handle ); [DllImport(Lib_Ndfapi, SetLastError = false, ExactSpelling = true)] [PInvokeData("ndfapi.h", MSDNShortId = "NF:ndfapi.NdfCreateInboundIncident")] public static extern HRESULT NdfCreateInboundIncident([Optional, MarshalAs(UnmanagedType.LPWStr)] string applicationID, [Optional, MarshalAs(UnmanagedType.LPWStr)] string serviceID, [In, Optional] PSID userID, in SOCKADDR_STORAGE localTarget, IPPROTO protocol, NDF_INBOUND_FLAG dwFlags, out SafeNDFHANDLE handle); /// /// The NdfCreateInboundIncident function creates a session to diagnose inbound connectivity for a specific application or service. /// /// /// Type: LPCWSTR /// The fully qualified path to the application receiving the inbound traffic. /// /// /// Type: LPCWSTR /// The Windows service receiving the inbound traffic. /// dll,-28502 (File/Print Sharing) /// dll,-28752 (Remote Desktop) /// dll,-32752 (Network Discovery) /// /// /// Type: SID* /// The SID for the application receiving the traffic. If NULL, the caller's SID is automatically used. /// /// /// Type: const SOCKADDR_STORAGE /// /// A SOCKADDR_STORAGE structure which limits the diagnosis to traffic to a specific IP address. If NULL, all traffic will be /// included in the diagnosis. /// /// /// /// Type: IPPROTO /// The protocol which should be diagnosed. For example, IPPROTO_TCP would be used to indicate the TCP/IP protocol. /// /// /// Type: DWORD /// Possible values: /// NDF_INBOUND_FLAG_EDGETRAVERSAL /// /// Indicates that a configuration should be considered unhealthy if both gateways on the local network are within the private access /// range, rather than only considering the configuration to be healthy if both gateways are Internet Gateway Devices (IGDs). /// /// NDF_INBOUND_FLAG_HEALTHCHECK /// /// Ends the session immediately if no inbound traffic problems are diagnosed, instead of turning on detailed tracing and allowing the /// user to reproduce their problem for a second diagnosis. /// /// /// /// Type: NDFHANDLE* /// Pointer to a handle to the Network Diagnostics Framework incident. /// /// /// Type: HRESULT /// Possible return values include, but are not limited to, the following. /// /// /// Return code /// Description /// /// /// S_OK /// The operation succeeded. /// /// /// E_INVALIDARG /// One or more parameters has not been provided correctly. /// /// /// /// Either applicationID or serviceID must be specified, but not both. // https://docs.microsoft.com/en-us/windows/win32/api/ndfapi/nf-ndfapi-ndfcreateinboundincident HRESULT NdfCreateInboundIncident( [in, // optional] LPCWSTR applicationID, [in, optional] LPCWSTR serviceID, [in, optional] SID *userID, [in, optional] const SOCKADDR_STORAGE // *localTarget, IPPROTO protocol, DWORD dwFlags, [out] NDFHANDLE *handle ); [DllImport(Lib_Ndfapi, SetLastError = false, ExactSpelling = true)] [PInvokeData("ndfapi.h", MSDNShortId = "NF:ndfapi.NdfCreateInboundIncident")] public static extern HRESULT NdfCreateInboundIncident([Optional, MarshalAs(UnmanagedType.LPWStr)] string applicationID, [Optional, MarshalAs(UnmanagedType.LPWStr)] string serviceID, [In, Optional] PSID userID, [In, Optional] IntPtr localTarget, IPPROTO protocol, NDF_INBOUND_FLAG dwFlags, out SafeNDFHANDLE handle); /// /// The NdfCreateIncident function is used internally by application developers to test the NDF functionality incorporated into /// their application. /// /// /// Type: LPCWSTR /// The name of the helper class to be used in the diagnoses of the incident. /// /// /// Type: ULONG /// A count of elements in the attributes array. /// /// /// Type: HELPER_ATTRIBUTE* /// The applicable HELPER_ATTRIBUTE structure. /// /// /// Type: NDFHANDLE* /// A handle to the Network Diagnostics Framework incident. /// /// /// Type: HRESULT /// Possible return values include, but are not limited to, the following. /// /// /// Return code /// Description /// /// /// S_OK /// The operation succeeded. /// /// /// E_OUTOFMEMORY /// There is not enough memory available to complete this operation. /// /// /// NDF_E_BAD_PARAM /// One or more parameters are invalid. /// /// /// NDF_E_NOHELPERCLASS /// helperClassName is NULL. /// /// /// // https://docs.microsoft.com/en-us/windows/win32/api/ndfapi/nf-ndfapi-ndfcreateincident HRESULT NdfCreateIncident( [in] LPCWSTR // helperClassName, ULONG celt, [in] HELPER_ATTRIBUTE *attributes, [out] NDFHANDLE *handle ); [DllImport(Lib_Ndfapi, SetLastError = false, ExactSpelling = true)] [PInvokeData("ndfapi.h", MSDNShortId = "NF:ndfapi.NdfCreateIncident")] public static extern HRESULT NdfCreateIncident([MarshalAs(UnmanagedType.LPWStr)] string helperClassName, uint celt, [In, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)] HELPER_ATTRIBUTE[] attributes, out SafeNDFHANDLE handle); /// The NdfCreateNetConnectionIncident function diagnoses connectivity issues using the NetConnection helper class. /// /// Type: NDFHANDLE* /// Handle to the Network Diagnostics Framework incident. /// /// /// Type: GUID /// Identifier of the network interface that the caller would like to create the incident for. /// /// The NULL GUID {00000000-0000-0000-0000-000000000000} may be used if the caller does not want to specify an interface. The system will /// attempt to determine the most appropriate interface based on the current state of the system. /// /// /// /// Type: HRESULT /// Possible return values include, but are not limited to, the following. /// /// /// Return code /// Description /// /// /// S_OK /// The operation succeeded. /// /// /// E_ABORT /// The underlying diagnosis or repair operation has been canceled. /// /// /// E_OUTOFMEMORY /// There is not enough memory available to complete this operation. /// /// /// NDF_E_BAD_PARAM /// The handle is invalid. /// /// /// // https://docs.microsoft.com/en-us/windows/win32/api/ndfapi/nf-ndfapi-ndfcreatenetconnectionincident HRESULT // 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, [In, Optional] Guid id); /// /// The NdfCreatePnrpIncident function creates a session to diagnose issues with the Peer Name Resolution Protocol (PNRP) service. /// /// /// Type: LPCWSTR /// The name of the cloud to be diagnosed. /// /// /// Type: LPCWSTR /// Optional name of a peer node which PNRP can attempt to resolve. The results will be used to help diagnose any problems. /// /// /// Type: BOOL /// /// Specifies whether the helper class should verify that the node can publish IDs. If FALSE, this diagnostic step will be skipped. /// /// /// /// Type: LPCWSTR /// Application ID for the calling application. /// /// /// Type: NDFHANDLE* /// Handle to the Network Diagnostics Framework incident. /// /// /// Type: HRESULT /// Possible return values include, but are not limited to, the following. /// /// /// Return code /// Description /// /// /// S_OK /// The operation succeeded. /// /// /// NDF_E_BAD_PARAM /// One or more parameters has not been provided correctly. /// /// /// /// /// /// The level of diagnosis performed depends on the parameters supplied. The availability of the PNRP service and the availability of the /// IPv6 networking class will be diagnosed, and additional diagnosis will be performed if certain parameters are supplied. /// /// /// /// If peername is specified, NDF will validate the availability of that peer in the PNRP network. /// /// /// If diagnosePublish is specified, NDF will validate the ability to publish a name in PNRP. /// /// /// // https://docs.microsoft.com/en-us/windows/win32/api/ndfapi/nf-ndfapi-ndfcreatepnrpincident HRESULT NdfCreatePnrpIncident( [in] LPCWSTR // cloudname, [in, optional] LPCWSTR peername, [in] BOOL diagnosePublish, [in, optional] LPCWSTR appId, [out] NDFHANDLE *handle ); [DllImport(Lib_Ndfapi, SetLastError = false, ExactSpelling = true)] [PInvokeData("ndfapi.h", MSDNShortId = "NF:ndfapi.NdfCreatePnrpIncident")] public static extern HRESULT NdfCreatePnrpIncident([MarshalAs(UnmanagedType.LPWStr)] string cloudname, [Optional, MarshalAs(UnmanagedType.LPWStr)] string peername, [MarshalAs(UnmanagedType.Bool)] bool diagnosePublish, [Optional, MarshalAs(UnmanagedType.LPWStr)] string appId, out SafeNDFHANDLE handle); /// The NdfCreateSharingIncident function diagnoses network problems in accessing a specific network share. /// /// Type: LPCWSTR /// The full UNC string (for example, "\server\folder\file.ext") for the shared asset with which there is a connectivity issue. /// /// /// Type: NDFHANDLE* /// Handle to the Network Diagnostics Framework incident. /// /// /// Type: HRESULT /// Possible return values include, but are not limited to, the following. /// /// /// Return code /// Description /// /// /// S_OK /// The operation succeeded. /// /// /// E_ABORT /// The underlying diagnosis or repair operation has been canceled. /// /// /// E_OUTOFMEMORY /// There is not enough memory available to complete this operation. /// /// /// NDF_E_BAD_PARAM /// One or more parameters are invalid. /// /// /// // https://docs.microsoft.com/en-us/windows/win32/api/ndfapi/nf-ndfapi-ndfcreatesharingincident HRESULT NdfCreateSharingIncident( [in] // LPCWSTR UNCPath, [out] NDFHANDLE *handle ); [DllImport(Lib_Ndfapi, SetLastError = false, ExactSpelling = true)] [PInvokeData("ndfapi.h", MSDNShortId = "NF:ndfapi.NdfCreateSharingIncident")] public static extern HRESULT NdfCreateSharingIncident([MarshalAs(UnmanagedType.LPWStr)] string UNCPath, out SafeNDFHANDLE handle); /// The NdfCreateWebIncident function diagnoses web connectivity problems concerning a specific URL. /// /// Type: LPCWSTR /// The URL with which there is a connectivity issue. /// /// /// Type: NDFHANDLE* /// Handle to the Network Diagnostics Framework incident. /// /// /// Type: HRESULT /// Possible return values include, but are not limited to, the following. /// /// /// Return code /// Description /// /// /// S_OK /// The operation succeeded. /// /// /// E_ABORT /// The underlying diagnosis or repair operation has been canceled. /// /// /// E_OUTOFMEMORY /// There is not enough memory available to complete this operation. /// /// /// NDF_E_BAD_PARAM /// One or more parameters are invalid. /// /// /// // https://docs.microsoft.com/en-us/windows/win32/api/ndfapi/nf-ndfapi-ndfcreatewebincident HRESULT NdfCreateWebIncident( [in] LPCWSTR // url, [out] NDFHANDLE *handle ); [DllImport(Lib_Ndfapi, SetLastError = false, ExactSpelling = true)] [PInvokeData("ndfapi.h", MSDNShortId = "NF:ndfapi.NdfCreateWebIncident")] public static extern HRESULT NdfCreateWebIncident([MarshalAs(UnmanagedType.LPWStr)] string url, out SafeNDFHANDLE handle); /// /// The NdfCreateWebIncidentEx function diagnoses web connectivity problems concerning a specific URL. This function allows for /// more control over the underlying diagnosis than the NdfCreateWebIncident function. /// /// /// Type: LPCWSTR /// The URL with which there is a connectivity issue. /// /// /// Type: BOOL /// TRUE if diagnosis will be performed using the WinHTTP APIs; FALSE if the WinInet APIs will be used. /// /// /// Type: LPWSTR /// /// The module name to use when checking against application-specific filtering rules (for example, "C:\Program Files\Internet /// Explorer\iexplorer.exe"). If NULL, the value is autodetected during the diagnosis. /// /// /// /// Type: NDFHANDLE* /// Handle to the Network Diagnostics Framework incident. /// /// /// Type: HRESULT /// Possible return values include, but are not limited to, the following. /// /// /// Return code /// Description /// /// /// S_OK /// The operation succeeded. /// /// /// E_ABORT /// The underlying diagnosis or repair operation has been canceled. /// /// /// E_OUTOFMEMORY /// There is not enough memory available to complete this operation. /// /// /// NDF_E_BAD_PARAM /// One or more parameters are invalid. /// /// /// // https://docs.microsoft.com/en-us/windows/win32/api/ndfapi/nf-ndfapi-ndfcreatewebincidentex HRESULT NdfCreateWebIncidentEx( [in] // LPCWSTR url, [in] BOOL useWinHTTP, [in] LPWSTR moduleName, [out] NDFHANDLE *handle ); [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); /// The NdfCreateWinSockIncident function provides access to the Winsock Helper Class provided by Microsoft. /// /// Type: SOCKET /// A descriptor identifying a connected socket. /// /// /// Type: LPCWSTR /// A pointer to the local host. /// /// /// Type: USHORT /// The port providing Winsock access. /// /// /// Type: LPCWSTR /// Unique identifier associated with the application. /// /// /// Type: SID* /// Unique identifier associated with the user. /// /// /// Type: NDFHANDLE* /// Handle to the Network Diagnostics Framework incident. /// /// /// Type: HRESULT /// Possible return values include, but are not limited to, the following. /// /// /// Return code /// Description /// /// /// S_OK /// The operation succeeded. /// /// /// E_OUTOFMEMORY /// There is not enough memory available to complete this operation. /// /// /// NDF_E_BAD_PARAM /// One or more parameters are invalid. /// /// /// E_INVALIDARG /// One or more parameters are invalid. /// /// /// // https://docs.microsoft.com/en-us/windows/win32/api/ndfapi/nf-ndfapi-ndfcreatewinsockincident HRESULT NdfCreateWinSockIncident( SOCKET // sock, [in, optional] LPCWSTR host, USHORT port, [in, optional] LPCWSTR appId, [in, optional] SID *userId, [out] NDFHANDLE *handle ); [DllImport(Lib_Ndfapi, SetLastError = false, ExactSpelling = true)] [PInvokeData("ndfapi.h", MSDNShortId = "NF:ndfapi.NdfCreateWinSockIncident")] public static extern HRESULT NdfCreateWinSockIncident(SOCKET sock, [Optional, MarshalAs(UnmanagedType.LPWStr)] string host, ushort port, [Optional, MarshalAs(UnmanagedType.LPWStr)] string appId, [In, Optional] PSID userId, out SafeNDFHANDLE handle); /// The NdfDiagnoseIncident function diagnoses the root cause of an incident without displaying a user interface. /// /// Type: NDFHANDLE /// A handle to the Network Diagnostics Framework incident. /// /// /// Type: ULONG* /// /// The number of root causes that could potentially have caused this incident. If diagnosis does not succeed, the contents of this /// parameter should be ignored. /// /// /// /// Type: RootCauseInfo** /// /// A collection of RootCauseInfo structures that contain a detailed description of the root cause. If diagnosis succeeds, this parameter /// contains both the leaf root causes identified in the diagnosis session and any non-leaf root causes that have an available repair. If /// diagnosis does not succeed, the contents of this parameter should be ignored. /// /// /// Memory allocated to these structures should later be freed. For an example of how to do this, see the Microsoft Windows Network /// Diagnostics Samples. /// /// /// /// Type: DWORD /// /// The length of time, in milliseconds, to wait before terminating the diagnostic routine. INFINITE may be passed to this parameter if /// no time-out is desired. /// /// /// /// Type: DWORD /// Possible values: /// /// /// Value /// Meaning /// /// /// NDF_ADD_CAPTURE_TRACE 0x0001 /// /// Turns on network tracing during diagnosis. Diagnostic results will be included in the Event Trace Log (ETL) file returned by NdfGetTraceFile. /// /// /// /// NDF_APPLY_INCLUSION_LIST_FILTER 0x0002 /// /// Applies filtering to the returned root causes so that they are consistent with the in-box scripted diagnostics behavior. Without this /// flag, root causes will not be filtered. This flag must be set by the caller, so existing callers will not see a change in behavior /// unless they explicitly specify this flag. /// /// /// /// /// /// Type: HRESULT /// Possible return values include, but are not limited to, the following. /// /// /// Return code /// Description /// /// /// S_OK /// The operation succeeded. /// /// /// E_HANDLE /// The NDF incident handle is not valid. /// /// /// WAIT_TIMEOUT /// The diagnostic routine has terminated because it has taken longer than the time-out specified in dwWait. /// /// /// /// /// /// This function is intended for use with scenarios where no user interface is shown, or where the standard Windows experience is not /// being used (as with Media Center and embedded applications). NdfExecuteDiagnosis will launch the diagnostics user interface, and /// should be used in scenarios using the standard Windows experience. You can call either NdfExecuteDiagnosis or /// NdfDiagnoseIncident, but not both. /// /// /// Before using this API, an application must call an incident creation function such as NdfCreateWebIncident to begin the NDF /// diagnostics process. The application then calls NdfDiagnoseIncident to diagnose the issue. If the diagnostics process /// identifies some possible repairs, the application can call NdfRepairIncident to repair the problem without displaying a user /// interface. NdfCancelIncident can optionally be called from a separate thread if the application wants to cancel an ongoing /// NdfDiagnoseIncident call. Finally, the application calls NdfCloseIncident. /// /// The following table shows some examples of root causes and their corresponding repairs. /// /// /// Root cause GUID /// Repair GUID /// Root cause description /// Repair description /// /// /// {4DA030B8-86E5-4b6a-A879-2FFF8443B527} /// {1296DFF0-D04E-4be1-A512-90F04DDFA3E6} /// A network cable is not properly plugged in or may be broken. /// /// Plug an Ethernet cable into this computer.\nAn Ethernet cable looks like a telephone cable but with larger connectors on the ends. /// Plug this cable into the opening on the back or side of the computer.\nMake sure the other end of the cable is plugged into the /// router. If that does not help, try using a different cable. /// /// /// /// {60372FD2-AD60-45c2-BD83-6B827FC438DF} /// {07d37f7b-fa5e-4443-bda7-ab107b29afb6} /// The %InterfaceName% adapter is disabled. /// Enable the %FriendlyInterfaceName% adapter. /// /// /// {245A9D66-AE9C-4518-A5B4-655752B0A5BD} /// {07d37f7b-fa5e-4443-bda7-ab107b29afb9} /// %InterfaceName%"" doesn't have a valid IP configuration. /// Reset the ""%InterfaceName%"" adapter.\nThis can sometimes resolve an intermittent problem. /// /// /// // https://docs.microsoft.com/en-us/windows/win32/api/ndfapi/nf-ndfapi-ndfdiagnoseincident HRESULT NdfDiagnoseIncident( [in] NDFHANDLE // Handle, [out] ULONG *RootCauseCount, [out] RootCauseInfo **RootCauses, DWORD dwWait, DWORD dwFlags ); [DllImport(Lib_Ndfapi, SetLastError = false, ExactSpelling = true)] [PInvokeData("ndfapi.h", MSDNShortId = "NF:ndfapi.NdfDiagnoseIncident")] public static extern HRESULT NdfDiagnoseIncident([In] NDFHANDLE Handle, out uint RootCauseCount, [Out, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)] out RootCauseInfo[] RootCauses, uint dwWait, NDF_DIAG dwFlags); /// The NdfExecuteDiagnosis function is used to diagnose the root cause of the incident that has occurred. /// /// Type: NDFHANDLE /// Handle to the Network Diagnostics Framework incident. /// /// /// Type: HWND /// /// Handle to the window that is intended to display the diagnostic information. If specified, the NDF UI is modal to the window. If /// NULL, the UI is non-modal. /// /// /// /// Type: HRESULT /// Possible return values include, but are not limited to, the following. /// /// /// Return code /// Description /// /// /// S_OK /// The operation succeeded. /// /// /// E_HANDLE /// handle is invalid. /// /// /// // https://docs.microsoft.com/en-us/windows/win32/api/ndfapi/nf-ndfapi-ndfexecutediagnosis HRESULT NdfExecuteDiagnosis( NDFHANDLE handle, // HWND hwnd ); [DllImport(Lib_Ndfapi, SetLastError = false, ExactSpelling = true)] [PInvokeData("ndfapi.h", MSDNShortId = "NF:ndfapi.NdfExecuteDiagnosis")] public static extern HRESULT NdfExecuteDiagnosis(NDFHANDLE handle, [In, Optional] HWND hwnd); /// /// The NdfGetTraceFile function is used to retrieve the path containing an Event Trace Log (ETL) file that contains Event Tracing /// for Windows (ETW) events from a diagnostic session. /// /// /// Type: NDFHANDLE /// Handle to a Network Diagnostics Framework incident. This handle should match the handle of an existing incident. /// /// /// Type: LPCWSTR* /// The location of the trace file. /// /// /// Type: HRESULT /// Possible return values include, but are not limited to, the following. /// /// /// Return code /// Description /// /// /// S_OK /// The operation succeeded. /// /// /// Any result other than S_OK should be interpreted as an error. /// /// /// This function cannot be called on an incident which has already been closed. /// /// ETL files contain information such as which components were diagnosed, component configuration information, and diagnosis results. /// For more information about ETL files, see Network Tracing in Windows 7. /// /// // https://docs.microsoft.com/en-us/windows/win32/api/ndfapi/nf-ndfapi-ndfgettracefile HRESULT NdfGetTraceFile( [in] NDFHANDLE Handle, // [out] LPCWSTR *TraceFileLocation ); [DllImport(Lib_Ndfapi, SetLastError = false, ExactSpelling = true)] [PInvokeData("ndfapi.h", MSDNShortId = "NF:ndfapi.NdfGetTraceFile")] public static extern HRESULT NdfGetTraceFile([In] NDFHANDLE Handle, [MarshalAs(UnmanagedType.LPWStr)] out string TraceFileLocation); /// The NdfRepairIncident function repairs an incident without displaying a user interface. /// /// Type: NDFHANDLE /// Handle to the Network Diagnostics Framework incident. This handle should match the handle passed to NdfDiagnoseIncident. /// /// /// Type: RepairInfoEx* /// A structure (obtained from NdfDiagnoseIncident) which indicates the particular repair to be performed. /// /// Memory allocated to these structures should later be freed. For an example of how to do this, see the Microsoft Windows Network /// Diagnostics Samples. /// /// /// /// Type: DWORD /// /// The length of time, in milliseconds, to wait before terminating the diagnostic routine. INFINITE may be passed to this parameter if /// no timeout is desired. /// /// /// /// Possible return values include, but are not limited to, the following. /// /// /// Return code /// Description /// /// /// S_OK /// Repair succeeded. /// /// /// NDF_E_VALIDATION /// /// The repair executed successfully, but NDF validation still found a connectivity problem. If this value is returned, the session /// should be closed by calling NdfCloseIncident and another session should be created to continue the diagnosis. /// /// /// /// E_HANDLE /// The NDF incident handle is not valid. /// /// /// WAIT_TIMEOUT /// The repair operation has terminated because it has taken longer than the time-out specified in dwWait. /// /// /// /// Other failure codes are returned if the repair failed to execute. In that case, the client can call NdfRepairIncident again /// with a different repair. /// /// /// /// /// NdfRepairIncident can only be called when NdfDiagnoseIncident is used for diagnostics. This is typically the case in scenarios /// where no user interface is shown, or where the standard Windows experience is not being used (as with Media Center and embedded /// applications). NdfRepairIncident should not be called when NdfExecuteDiagnosis is used. /// /// /// Before using this API, an application must call an incident creation function such as NdfCreateWebIncident to begin the NDF /// diagnostics process. The application then calls NdfDiagnoseIncident to diagnose the issue. If the diagnostics process identifies some /// possible repairs, the application can call NdfRepairIncident to repair the problem without displaying a user interface. /// NdfCancelIncident can optionally be called from a separate thread if the application wants to cancel an ongoing /// NdfRepairIncident call. Finally, the application calls NdfCloseIncident. /// /// // https://docs.microsoft.com/en-us/windows/win32/api/ndfapi/nf-ndfapi-ndfrepairincident HRESULT NdfRepairIncident( [in] NDFHANDLE // Handle, [in] RepairInfoEx *RepairEx, DWORD dwWait ); [DllImport(Lib_Ndfapi, SetLastError = false, ExactSpelling = true)] [PInvokeData("ndfapi.h", MSDNShortId = "NF:ndfapi.NdfRepairIncident")] public static extern HRESULT NdfRepairIncident([In] NDFHANDLE Handle, in RepairInfoEx RepairEx, uint dwWait); /// Provides a handle to a Network Diagnostic Framework incident. [StructLayout(LayoutKind.Sequential)] public struct NDFHANDLE : IHandle { private readonly IntPtr handle; /// Initializes a new instance of the struct. /// An object that represents the pre-existing handle to use. public NDFHANDLE(IntPtr preexistingHandle) => handle = preexistingHandle; /// Returns an invalid handle by instantiating a object with . public static NDFHANDLE NULL => new(IntPtr.Zero); /// Gets a value indicating whether this instance is a null handle. public bool IsNull => handle == IntPtr.Zero; /// Implements the operator !. /// The handle. /// The result of the operator. public static bool operator !(NDFHANDLE h1) => h1.IsNull; /// Performs an explicit conversion from to . /// The handle. /// The result of the conversion. public static explicit operator IntPtr(NDFHANDLE h) => h.handle; /// Performs an implicit conversion from to . /// The pointer to a handle. /// The result of the conversion. public static implicit operator NDFHANDLE(IntPtr h) => new(h); /// Implements the operator !=. /// The first handle. /// The second handle. /// The result of the operator. public static bool operator !=(NDFHANDLE h1, NDFHANDLE h2) => !(h1 == h2); /// Implements the operator ==. /// The first handle. /// The second handle. /// The result of the operator. public static bool operator ==(NDFHANDLE h1, NDFHANDLE h2) => h1.Equals(h2); /// public override bool Equals(object obj) => obj is NDFHANDLE h && handle == h.handle; /// public override int GetHashCode() => handle.GetHashCode(); /// public IntPtr DangerousGetHandle() => handle; } /// Provides a for that is disposed using . public class SafeNDFHANDLE : SafeHANDLE { /// Initializes a new instance of the class and assigns an existing handle. /// An object that represents the pre-existing handle to use. /// /// to reliably release the handle during the finalization phase; otherwise, (not recommended). /// public SafeNDFHANDLE(IntPtr preexistingHandle, bool ownsHandle = true) : base(preexistingHandle, ownsHandle) { } /// Initializes a new instance of the class. private SafeNDFHANDLE() : base() { } /// Performs an implicit conversion from to . /// The safe handle instance. /// The result of the conversion. public static implicit operator NDFHANDLE(SafeNDFHANDLE h) => h.handle; /// protected override bool InternalReleaseHandle() => NdfCloseIncident(handle).Succeeded; } }