Advertisement
Guest User

Untitled

a guest
May 22nd, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.78 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Runtime.InteropServices;
  6.  
  7. namespace GetWorkGroup
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             Console.WriteLine("Using method 5: {0}", GetJoinedWorkGroup()); //returns the correct workgroup name
  14.             Console.ReadKey();
  15.         }
  16.         public static string GetJoinedWorkGroup()
  17.         {
  18.             int result = 0;
  19.             string domain = null;
  20.             IntPtr pDomain = IntPtr.Zero;
  21.             Win32.NetJoinStatus status = Win32.NetJoinStatus.NetSetupUnknownStatus;
  22.             try
  23.             {
  24.                 result = Win32.NetGetJoinInformation(null, out pDomain, out status);
  25.                 if (result == Win32.ErrorSuccess &&
  26.                     status == Win32.NetJoinStatus.NetSetupWorkgroupName)
  27.                 {
  28.                     domain = Marshal.PtrToStringAuto(pDomain);
  29.                 }
  30.             }
  31.             finally
  32.             {
  33.                 if (pDomain != IntPtr.Zero) Win32.NetApiBufferFree(pDomain);
  34.             }
  35.             if (domain == null) domain = "";
  36.             return domain;
  37.         }
  38.     }
  39.     internal class Win32
  40.     {
  41.         public const int ErrorSuccess = 0;
  42.  
  43.         [DllImport("Netapi32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
  44.         public static extern int NetGetJoinInformation(string server, out IntPtr domain, out NetJoinStatus status);
  45.  
  46.         [DllImport("Netapi32.dll")]
  47.         public static extern int NetApiBufferFree(IntPtr Buffer);
  48.  
  49.         public enum NetJoinStatus
  50.         {
  51.             NetSetupUnknownStatus = 0,
  52.             NetSetupUnjoined,
  53.             NetSetupWorkgroupName,
  54.             NetSetupDomainName
  55.         }
  56.  
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement