How to obtain the name of the current netbios workgroup?

Windows specific forum
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

How to obtain the name of the current netbios workgroup?

Post by Mistrel »

How can I obtain information about the current workgroup and domain for the current user?
Marc56us
Addict
Addict
Posts: 1479
Joined: Sat Feb 08, 2014 3:26 pm

Re: How to obtain the name of the current netbios workgroup?

Post by Marc56us »

Mistrel wrote:How can I obtain information about the current workgroup and domain for the current user?

Code: Select all

RunProgram("wmic", "computersystem get workgroup, domain", "")
See also API

:wink:
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Re: How to obtain the name of the current netbios workgroup?

Post by Mistrel »

I mean with Win32 and library calls. Not the output from a console program.
User avatar
Shardik
Addict
Addict
Posts: 1989
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: How to obtain the name of the current netbios workgroup?

Post by Shardik »

Tested succcessfully on Window 10 x64 V1809 with PB 5.46 in ASCII and Unicode mode and PB 5.71 (with 32-bit and 64-bit compilation):

Code: Select all

EnableExplicit

#ComputerNameDnsDomain = 2

Enumeration NETSETUP_JOIN_STATUS
  #NetSetupUnknownStatus
  #NetSetupUnjoined
  #NetSetupWorkgroupName
  #NetSetupDomainName
EndEnumeration

Import ""
  GetComputerNameExA(NameType.I, *Buffer, *BufferSize)
EndImport

Prototype.I NetGetJoinInformation(Host.P-Unicode, *Buffer, BufferType.I)

Define BufferSize.I
Define *DomainBuffer
Define Info.S
Define JoinInfo.NetGetJoinInformation
Define JoinStatus.I
Define *NameBuffer

If InitNetwork()
  If OpenLibrary(0, "NetAPI32.DLL")
    JoinInfo = GetFunction(0, "NetGetJoinInformation")
    
    If JoinInfo
      If JoinInfo(ComputerName(), @*NameBuffer, @JoinStatus) = 0
        Select JoinStatus
          Case #NetSetupUnknownStatus
            Info = "Unknown network status!"
          Case #NetSetupUnjoined
            Info = "Not joined to any workgroup or domain!"
          Case #NetSetupWorkgroupName
            Info = "Joined to the workgroup " +
              PeekS(*NameBuffer, -1, #PB_Unicode)
          Case #NetSetupDomainName
            ; ----- Doesn't display top level domain, for example local in
            ;       xxx.local
            ; Info = "Joined to the domain " +
            ; PeekS(*NameBuffer, -1, #PB_Unicode)

            ; ----- Display second level domain plus top level domain
            GetComputerNameExA(#ComputerNameDnsDomain, 0, @BufferSize)
            
            If BufferSize
              *DomainBuffer = AllocateMemory(BufferSize)
              
              If GetComputerNameExA(#ComputerNameDnsDomain, *DomainBuffer,
                @BufferSize)
                Info = "Joined to the domain " +
                  PeekS(*DomainBuffer, -1, #PB_Ascii)
              EndIf
              
              FreeMemory(*DomainBuffer)
            EndIf
        EndSelect
      EndIf
    EndIf
    
    NetApiBufferFree_(*NameBuffer)
    CloseLibrary(0)
    
    MessageRequester("Detect if joined to domain or workgroup",
      Info,
      #MB_ICONINFORMATION)
  EndIf
EndIf
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Re: How to obtain the name of the current netbios workgroup?

Post by Mistrel »

That's it. Thank you. :)
Post Reply