Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.92 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 (pDomain != IntPtr.Zero)
  26.                 {
  27.                     Win32.NetApiBufferFree(pDomain);
  28.                 }
  29.                 if (result == Win32.ErrorSuccess &&
  30.                     status == Win32.NetJoinStatus.NetSetupWorkgroupName)
  31.                 {
  32.                     domain = Marshal.PtrToStringAuto(pDomain);
  33.                 }
  34.             }
  35.             finally
  36.             {
  37.                 if (pDomain != IntPtr.Zero) Win32.NetApiBufferFree(pDomain);
  38.             }
  39.             if (domain == null) domain = "";
  40.             return domain;
  41.         }
  42.     }
  43.     internal class Win32
  44.     {
  45.         public const int ErrorSuccess = 0;
  46.  
  47.         [DllImport("Netapi32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
  48.         public static extern int NetGetJoinInformation(string server, out IntPtr domain, out NetJoinStatus status);
  49.  
  50.         [DllImport("Netapi32.dll")]
  51.         public static extern int NetApiBufferFree(IntPtr Buffer);
  52.  
  53.         public enum NetJoinStatus
  54.         {
  55.             NetSetupUnknownStatus = 0,
  56.             NetSetupUnjoined,
  57.             NetSetupWorkgroupName,
  58.             NetSetupDomainName
  59.         }
  60.  
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement