|
| 1 | +using System; |
| 2 | +using PSADT.LibraryInterfaces.SafeHandles; |
| 3 | +using PSADT.LibraryInterfaces.Utilities; |
| 4 | +using Windows.Win32; |
| 5 | +using Windows.Win32.Foundation; |
| 6 | + |
| 7 | +namespace PSADT.LibraryInterfaces |
| 8 | +{ |
| 9 | + /// <summary> |
| 10 | + /// Provides interop methods for querying network join information on Windows systems. |
| 11 | + /// </summary> |
| 12 | + /// <remarks>This class contains static methods that wrap native Windows networking APIs. It is intended |
| 13 | + /// for internal use and is not thread-safe. Callers are responsible for managing any unmanaged resources returned |
| 14 | + /// by these methods, such as releasing buffer handles to prevent memory leaks.</remarks> |
| 15 | + internal static class NetApi32 |
| 16 | + { |
| 17 | + /// <summary> |
| 18 | + /// Retrieves the join status and name of the domain or workgroup for the specified computer. |
| 19 | + /// </summary> |
| 20 | + /// <remarks>The caller must release the buffer referenced by lpNameBuffer to avoid memory leaks. |
| 21 | + /// This method throws an exception if the underlying Windows API call fails.</remarks> |
| 22 | + /// <param name="lpServer">The name of the remote server to query, or null to specify the local computer. The name must begin with \\ |
| 23 | + /// if specified.</param> |
| 24 | + /// <param name="lpNameBuffer">When this method returns, contains a handle to a buffer that receives the name of the domain or workgroup. |
| 25 | + /// The caller is responsible for releasing this handle.</param> |
| 26 | + /// <param name="BufferType">When this method returns, contains a value that indicates the join status of the computer.</param> |
| 27 | + /// <returns>A WIN32_ERROR value that indicates the result of the operation. Returns NERR_Success if successful.</returns> |
| 28 | + internal static WIN32_ERROR NetGetJoinInformation(string? lpServer, out SafeNetApiBufferFreeHandle lpNameBuffer, out Windows.Win32.NetworkManagement.NetManagement.NETSETUP_JOIN_STATUS BufferType) |
| 29 | + { |
| 30 | + WIN32_ERROR res = (WIN32_ERROR)PInvoke.NetGetJoinInformation(lpServer, out PWSTR lpNameBufferLocal, out BufferType); |
| 31 | + if (res != PInvoke.NERR_Success) |
| 32 | + { |
| 33 | + throw ExceptionUtilities.GetExceptionForLastWin32Error(res); |
| 34 | + } |
| 35 | + unsafe |
| 36 | + { |
| 37 | + lpNameBuffer = new((IntPtr)lpNameBufferLocal.Value, lpNameBufferLocal.Length, true); |
| 38 | + } |
| 39 | + return res; |
| 40 | + } |
| 41 | + |
| 42 | + /// <summary> |
| 43 | + /// Retrieves information about the join status of the local computer to a domain or workgroup. |
| 44 | + /// </summary> |
| 45 | + /// <param name="lpNameBuffer">When this method returns, contains a handle to a buffer that receives the name of the domain or workgroup. |
| 46 | + /// The caller is responsible for freeing this buffer.</param> |
| 47 | + /// <param name="BufferType">When this method returns, contains a value that specifies the join status of the local computer.</param> |
| 48 | + /// <returns>A WIN32_ERROR value that indicates the result of the operation. Returns ERROR_SUCCESS if the information is |
| 49 | + /// retrieved successfully; otherwise, returns a system error code.</returns> |
| 50 | + internal static WIN32_ERROR NetGetJoinInformation(out SafeNetApiBufferFreeHandle lpNameBuffer, out Windows.Win32.NetworkManagement.NetManagement.NETSETUP_JOIN_STATUS BufferType) |
| 51 | + { |
| 52 | + return NetGetJoinInformation(null, out lpNameBuffer, out BufferType); |
| 53 | + } |
| 54 | + } |
| 55 | +} |
0 commit comments