Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Runtime.InteropServices;
- namespace GetWorkGroup
- {
- class Program
- {
- static void Main(string[] args)
- {
- Console.WriteLine("Using method 5: {0}", GetJoinedWorkGroup()); //returns the correct workgroup name
- Console.ReadKey();
- }
- public static string GetJoinedWorkGroup()
- {
- int result = 0;
- string domain = null;
- IntPtr pDomain = IntPtr.Zero;
- Win32.NetJoinStatus status = Win32.NetJoinStatus.NetSetupUnknownStatus;
- try
- {
- result = Win32.NetGetJoinInformation(null, out pDomain, out status);
- if (pDomain != IntPtr.Zero)
- {
- Win32.NetApiBufferFree(pDomain);
- }
- if (result == Win32.ErrorSuccess &&
- status == Win32.NetJoinStatus.NetSetupWorkgroupName)
- {
- domain = Marshal.PtrToStringAuto(pDomain);
- }
- }
- finally
- {
- if (pDomain != IntPtr.Zero) Win32.NetApiBufferFree(pDomain);
- }
- if (domain == null) domain = "";
- return domain;
- }
- }
- internal class Win32
- {
- public const int ErrorSuccess = 0;
- [DllImport("Netapi32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
- public static extern int NetGetJoinInformation(string server, out IntPtr domain, out NetJoinStatus status);
- [DllImport("Netapi32.dll")]
- public static extern int NetApiBufferFree(IntPtr Buffer);
- public enum NetJoinStatus
- {
- NetSetupUnknownStatus = 0,
- NetSetupUnjoined,
- NetSetupWorkgroupName,
- NetSetupDomainName
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement