Jump to content

[Solved] Get the WorkGroup name...


Recommended Posts

Well, there are many examples in this forum about how to obtain the name of the WorkGroup: some solutions are based on the command line using "nbtstat.exe-n", there are no elegant solutions...

NetGetJoinInformation: A Windows API located in "netapi32.dll" would be an excellent option, check the following links:

#238423

#301633

But none of them can return the name of the WorkGroup! Wow, how hard it is to know the name of the group (it seems that only the gurus know...), I still can not understand the "why" is so difficult.

Changing the code from the first link, is as follows:

MsgBox(4096, "teste", netGetJoinInfo())
;########################################################
; Fonction: netGetJoinInfo()
; status: prod
; require: win2k
; return: state of secure channel & "|" & domain/wrkgrp
; ------
;########################################################
Func netGetJoinInfo($wkst = @ComputerName, $deb = 0) ; computer, debug mode
    Local $err, $uname, $domStruct, $lpNameBuffer, $result, $domStruct, $bufferType, $typStruct
    Local Const $NetSetupUnknownStatus = 0
    Local Const $NetSetupUnjoined = 1
    Local Const $NetSetupWorkgroupName = 2
    Local Const $NetSetupDomainName = 3

    If StringLen($wkst) = 0 Then $wkst = @ComputerName

    $domStruct = DllStructCreate("char[256]")
    $lpNameBuffer = DllStructGetPtr($domStruct)
    $typStruct = DllStructCreate("int")
    $bufferType = DllStructGetPtr($typStruct)

    $err = DllCall("Netapi32.dll", "int", "NetGetJoinInformation", _
            "wstr", $wkst, _ ; LPCWSTR lpserver [in ] Pointer to a constant string that specifies the DNS or NetBIOS name of the computer on which to call the function. If this parameter is NULL, the local computer is used.
            "ptr", $lpNameBuffer, _ ; LPDWORD lpNameBuffer [OUT] Pointer to the buffer that receives the NetBIOS name of the domain or workgroup to which the computer is joined.
            "ptr", $bufferType) ; PNET... BufferType [OUT] Receives the join status of the specified computer

    ; Error ?
    If $err[0] Then
        $lasterr = DllCall("kernel32.dll", "int", "GetLastError")
        ;If $deb Then MsgBox(0, "error NetGetJoinInformation " & FormatMessage($lasterr[0]), FormatMessage($err[0]))
        ;get rid of the array & cleanup before leaving with error
        $entriesstruct = 0
        $totalstruct = 0
        DllCall("Netapi32.dll", "int", "NetApiBufferFree", "ptr", $lpNameBuffer)
        Return -1
    EndIf ; $err[0] <> 0


    $result = DllStructGetData($typStruct, 1)
    $domn = DllStructGetData($domStruct, 1)
    ;If $deb Then MsgBox(0, "NetGetJoinInformation " & $wkst, $result & " " & $domn)

    ;get rid of the array & cleanup
    $entriesstruct = 0
    $totalstruct = 0
    DllCall("Netapi32.dll", "int", "NetApiBufferFree", "ptr", $lpNameBuffer)

    Return $result & "|" & $domn

EndFunc   ;==>netGetJoinInfo
Believe it: did not work!

Does anyone know how to make this code work?

Edited by jscript

http://forum.autoitbrasil.com/ (AutoIt v3 Brazil!!!)

Somewhere Out ThereJames Ingram

somewh10.png

dropbo10.pngDownload Dropbox - Simplify your life!
Your virtual HD wherever you go, anywhere!

Link to comment
Share on other sites

You may want to look at command line type approach to see if you can get it.

I said:

Well, there are many examples in this forum about how to obtain the name of the WorkGroup: some solutions are based on the command line using "nbtstat.exe-n", there are no elegant solutions...

NetGetJoinInformation: A Windows API located in "netapi32.dll" would be an excellent option, check the following links:

http://www.autoitscript.com/forum/index....p?showtopic=31931&view=findpost&p=238423

http://www.autoitscript.com/forum/index....p?showtopic=40518&view=findpost&p=301633

http://forum.autoitbrasil.com/ (AutoIt v3 Brazil!!!)

Somewhere Out ThereJames Ingram

somewh10.png

dropbo10.pngDownload Dropbox - Simplify your life!
Your virtual HD wherever you go, anywhere!

Link to comment
Share on other sites

Why not grab it from WMI?

This is edited from Scriptomatic:

; Generated by AutoIt Scriptomatic July 28, 2010

$wbemFlagReturnImmediately = 0x10
$wbemFlagForwardOnly = 0x20
$colItems = ""
$strComputer = "localhost"

$Output=""
$Output &= "Computer: " & $strComputer  & @CRLF
$Output &= "==========================================" & @CRLF
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\")
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem", "WQL", _
                                          $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

If IsObj($colItems) then
   For $objItem In $colItems
       MsgBox(0, 'test', $objItem.Workgroup)
   Next
EndIf
Link to comment
Share on other sites

  • Moderators

Using the same dll, try: NetServerEnum

With SV_TYPE_DOMAIN_ENUM constant.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Read the function description more carefully.

MsgBox(4096, "teste", netGetJoinInfo())
;########################################################
; Fonction: netGetJoinInfo()
; status: prod
; require: win2k
; return: state of secure channel & "|" & domain/wrkgrp
; ------
;########################################################
Func netGetJoinInfo($wkst = @ComputerName, $deb = 0) ; computer, debug mode
    Local $err, $pBuffer, $tName, $sName, $sDom, $sRet
    Local Const $NetSetupUnknownStatus = 0
    Local Const $NetSetupUnjoined = 1
    Local Const $NetSetupWorkgroupName = 2
    Local Const $NetSetupDomainName = 3

    If StringLen($wkst) = 0 Then $wkst = @ComputerName

    $err = DllCall("Netapi32.dll", "int", "NetGetJoinInformation", _
            "wstr", $wkst, "ptr*", 0, "int*", 0)

    If @error Then return SetError(@error, @extended, 0)

    If $err[0] = 0 Then
        $pBuffer = $err[2]
        $tName = DllStructCreate("wchar[" & _NetApi_BufferSize($pBuffer) & "]", $pBuffer)
        $sName = DllStructGetData($tName, 1)
        $sDom  = $err[3]
        $sRet  = $sName & "|" & $sDom
        _NetApi_BufferFree($pBuffer)
    EndIf

    Return $sRet
;~     Return $result & "|" & $domn

EndFunc   ;==>netGetJoinInfo

Func _NetApi_BufferSize($pBuffer)
    Local $aResult = DllCall("Netapi32.dll", "int", "NetApiBufferSize", "ptr", $pBuffer, "uint*", 0)

    If @error Then Return SetError(@error, @extended, 0)
    If $aResult[0] <> 0 Then Return SetError(-1, $aResult[0], 0)
    Return $aResult[2]
EndFunc

Func _NetApi_BufferFree($pBuffer)
    Local $aResult = DllCall("Netapi32.dll", "int", "NetApiBufferFree", "ptr", $pBuffer)

    If @error Then Return SetError(@error, @extended, False)
    If $aResult[0] <> 0 Then Return SetError(-1, $aResult[0], False)
    Return SetError(0, 0, True)
EndFunc
Link to comment
Share on other sites

@danwilli

For me WMI is not enough, especially in an environment of pre-installation like WinPE, and in my opinion using the Windows APIs is more professional and elegant.

@Authenticity

I knew someone would have a satisfactory answer, congratulations, it works perfectly: no need to use the command line and WMI.

I'll post soon an UDF using your example.

Thank you to everyone who tried to solve the "problem".

http://forum.autoitbrasil.com/ (AutoIt v3 Brazil!!!)

Somewhere Out ThereJames Ingram

somewh10.png

dropbo10.pngDownload Dropbox - Simplify your life!
Your virtual HD wherever you go, anywhere!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...