Jump to content

_NetWork.au3 (UDF)


JScript
 Share

Recommended Posts

Hello everyone,

To Set/Get WorkGroupName and Set ComputerName without restarting!

Try the code below:

_NetWork.au3

#include-once
#include "EnvUpdate.inc"
; #INDEX# =======================================================================================================================
; Title .........: Set/Get WorkGroupName and Set ComputerName
; AutoIt Version.: 3.2.12++
; Language.......: English
; Description ...: And Refreshes the OS environment.
; Author ........: João Carlos (jscript)
; Support .......:
; ===============================================================================================================================

; #CURRENT# =====================================================================================================================
;_SetComputerName
;_SetWorkGroupName
;_GetWorkgroupName
; ===============================================================================================================================

; #INTERNAL_USE_ONLY# ===========================================================================================================
; ===============================================================================================================================

; #VARIABLES# ===================================================================================================================

; #FUNCTION# =======================================================================================================================================================
; Name...........: _SetComputerName
; Description ...: Sets the new computer name.
; Syntax.........: _SetComputerName( "ComputerName" )
; Parameters ....:
; Requirement(s).:
; Return values .: Success - Returns 1
;                  Failure - Returns 0
; Author ........: jscript
; Modified.......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: _SetComputerName( "VAIO" )
; ==================================================================================================================================================================
Func _SetComputerName($sCmpName)
    Local $sLogonKey = "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon"
    Local $sCtrlKey = "HKLM\SYSTEM\CurrentControlSet"

    ; RagsRevenge -> http://www.autoitscript.com/forum/index.php?showtopic=54091&view=findpost&p=821901
    If StringRegExp($sCmpName, '\\|/|:|\*|\?|"|<|>|\.|,|~|!|@|#|\$|%|\^|&|\(|\)|\;|{|}|_|=|\+|\[|\]|\x60' & "|'", 0) = 0 Then Return 0

    ; 5 = ComputerNamePhysicalDnsHostname
    $aRet = DllCall("Kernel32.dll", "BOOL", "SetComputerNameEx", "int", 5, "str", $sCmpName)
    If $aRet[0] = 0 Then Return 0
    RegWrite($sCtrlKey & "\Control\Computername\ActiveComputername", "ComputerName", "REG_SZ", $sCmpName)
    RegWrite($sCtrlKey & "\Control\Computername\Computername", "ComputerName", "REG_SZ", $sCmpName)
    RegWrite($sCtrlKey & "\Services\Tcpip\Parameters", "Hostname", "REG_SZ", $sCmpName)
    RegWrite($sCtrlKey & "\Services\Tcpip\Parameters", "NV Hostname", "REG_SZ", $sCmpName)
    RegWrite($sLogonKey, "AltDefaultDomainName", "REG_SZ", $sCmpName)
    RegWrite($sLogonKey, "DefaultDomainName", "REG_SZ", $sCmpName)
    RegWrite("HKEY_USERS\.Default\Software\Microsoft\Windows Media\WMSDK\General", "Computername", "REG_SZ", $sCmpName)
    _EnvUpdate("COMPUTERNAME", $sCmpName, False, True)
    Return 1
EndFunc   ;==>_SetComputerName

; #FUNCTION# =======================================================================================================================================================
; Name...........: _SetWorkGroupName
; Description ...: Sets the new NetBIOS Workgroup name.
; Syntax.........: _SetWorkGroupName( "WorkGroupName" )
; Parameters ....:
; Requirement(s).:
; Return values .: Success - Returns NERR_Success
;                  Failure - Return value can be one of the following error codes: http://msdn.microsoft.com/en-us/library/aa370433%28VS.85%29.aspx
; Author ........: jscript
; Modified.......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: _SetWorkGroupName( "MSHOME" )
; ==================================================================================================================================================================
Func _SetWorkGroupName($sGroupName)
    Local $aRet = DllCall("Netapi32.dll", "long", "NetJoinDomain", "int", 0, "wstr", $sGroupName, "int", 0, "int", 0, "int", 0, "dword", 0x00000040)
    Return $aRet[0]
EndFunc   ;==>_SetWorkGroupName

; #FUNCTION# =======================================================================================================================================================
; Name...........: _GetWorkgroupName
; Description ...: Retrivies the current NetBIOS Workgroup.
; Syntax.........: _GetWorkgroupName()
; Parameters ....:
; Requirement(s).:
; Return values .: Success - Returns the string value of the computer's current NetBIOS Workgroup
;                  Failure - Returns "MSHOME" and set @Error = 1
; Author ........: joell
; Modified.......: Authenticity, jscript
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: _GetWorkgroupName()
; ==================================================================================================================================================================
Func _GetWorkgroupName()
    Local $NERR, $pBuffer, $sName
    Local Const $NetSetupUnknownStatus = 0
    Local Const $NetSetupUnjoined = 1
    Local Const $NetSetupWorkgroupName = 2
    Local Const $NetSetupDomainName = 3

    $NERR = DllCall("Netapi32.dll", "int", "NetGetJoinInformation", "wstr", @ComputerName, "ptr*", 0, "int*", 0)

    If @error Then Return SetError(@error, @extended, "")
    If $NERR[0] = 0 Then
        $pBuffer = $NERR[2]
        $sName = DllStructGetData(DllStructCreate("wchar[" & __NetApi_BufferSize($pBuffer) & "]", $pBuffer), 1)
        __NetApi_BufferFree($pBuffer)
    EndIf
    If @error Then Return SetError(@error, @extended, "")

    Return $sName
EndFunc   ;==>_GetWorkgroupName

;  Authenticity
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   ;==>__NetApi_BufferSize

;  Authenticity
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   ;==>__NetApi_BufferFree

Need the below code:

_EnvUpdate.au3:

#include-once
; #INDEX# =======================================================================================================================
; Title .........: Environment Update
; AutoIt Version.: 3.2.12++
; Language.......: English
; Description ...: Refreshes the OS environment.
; Author ........: João Carlos (jscript)
; Support .......: trancexx, PsaltyDS, KaFu
; ===============================================================================================================================

; #CURRENT# =====================================================================================================================
;_EnvUpdate
; ===============================================================================================================================

; #INTERNAL_USE_ONLY# ===========================================================================================================
; ===============================================================================================================================

; #VARIABLES# ===================================================================================================================
Global $MAX_VALUE_NAME = 1024
Global $HWND_BROADCAST = 0xffff
Global $WM_SETTINGCHANGE = 0x001A
Global $SMTO_ABORTIFHUNG = 0x0002
Global $SMTO_NORMAL = 0x0000
Global $MSG_TIMEOUT = 5000

; #Example# =====================================================================================================================
;_EnvUpdate("VERSION", "7.07.0110.2600")
;MsgBox(4096, @error, EnvGet("VERSION"))
;_EnvUpdate("VERSION", "", True, True)
;MsgBox(4096, @error, EnvGet("VERSION"))
; ===============================================================================================================================

; #FUNCTION# ====================================================================================================================
; Name...........: _EnvUpdate
; Description ...: Refreshes the OS environment.
; Syntax.........: _EnvUpdate( ["envvariable" [, "value" [, CurrentUser [, Machine ]]]] )
; Parameters ....: envvariable  - [optional] Name of the environment variable to set. If no variable, refreshes all variables.
;                  value        - [optional] Value to set the environment variable to. If a value is not used the environment
;                                   variable will be deleted.
;                  CurrentUser  - [optional] Sets the variable in current user environment.
;                  Machine      - [optional] Sets the variable in the machine environment.
; Return values .: Success      - None
;                  Failure      - Sets @error to 1.
; Author ........: João Carlos (jscript)
; Support .......: trancexx, PsaltyDS, KaFu
; Modified.......:
; Remarks .......:
; Related .......:
; Link ..........;
; Example .......; _EnvUpdate("TEMP", @SystemDir & "TEMP", True, True)
; ===============================================================================================================================
Func _EnvUpdate($sEnvVar = "", $vValue = "", $fCurrentUser = True, $fMachine = False)
    Local $sREG_TYPE = "REG_SZ", $iRet1, $iRet2

    If $sEnvVar <> "" Then
        If StringInStr($sEnvVar, "\") Then $sREG_TYPE = "REG_EXPAND_SZ"
        If $vValue <> "" Then
            If $fCurrentUser Then RegWrite("HKCU\Environment", $sEnvVar, $sREG_TYPE, $vValue)
            If $fMachine Then RegWrite("HKLM\System\CurrentControlSet\Control\Session Manager\Environment", $sEnvVar, $sREG_TYPE, $vValue)
        Else
            If $fCurrentUser Then RegDelete("HKCU\Environment", $sEnvVar)
            If $fMachine Then RegDelete("HKLM\System\CurrentControlSet\Control\Session Manager\Environment", $sEnvVar)
        EndIf
        ; http://msdn.microsoft.com/en-us/library/ms686206%28VS.85%29.aspx
        $iRet1 = DllCall("Kernel32.dll", "BOOL", "SetEnvironmentVariable", "str", $sEnvVar, "str", $vValue)
        If $iRet1[0] = 0 Then Return SetError(1)
    EndIf
    ; http://msdn.microsoft.com/en-us/library/ms644952%28VS.85%29.aspx
    $iRet2 = DllCall("user32.dll", "lresult", "SendMessageTimeoutW", _
            "hwnd", $HWND_BROADCAST, _
            "dword", $WM_SETTINGCHANGE, _
            "ptr", 0, _
            "wstr", "Environment", _
            "dword", $SMTO_ABORTIFHUNG, _
            "dword", $MSG_TIMEOUT, _
            "dword_ptr*", 0)

    If $iRet2[0] = 0 Then Return SetError(1)
EndFunc   ;==>_EnvUpdate

Send comments!

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

does this work in a domain environment? Or just a stand alone workgroup?

By that I mean, can you change the computer name (without restarting) and also add the computer to a domain under the new computer name in the same session? Obviously adding to a domain will require a reboot, but by being able to change the workstation name and then also add to a domain under the new name without having to reboot saves loads of time.

Link to comment
Share on other sites

does this work in a domain environment? Or just a stand alone workgroup?

(...)

Workgroup and Domain: http://msdn.microsoft.com/en-us/library/ms724931%28VS.85%29.aspx

SetComputerNameEx Function: Sets a new NetBIOS or DNS name for the local computer.

NameType:

ComputerNamePhysicalDnsDomain -> Sets the primary DNS suffix of the computer.

ComputerNamePhysicalDnsHostname -> Sets the NetBIOS and the Computer Name (the first label of the full DNS name).

ComputerNamePhysicalNetBIOS -> Sets the NetBIOS name to the name specified.

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...