netrenamemachineindomain (netapi32)
Last changed: TheDoctor-162.114.211.139

.
Summary
Renames an already joined machine on a domain.

C# Signature:

    [DllImport("netapi32.dll", SetLastError=true, CharSet = CharSet.Unicode)]
    internal static extern int NetRenameMachineInDomain(
        string lpServer,
        string lpNewMachineName,
        string lpAccount,
        string lpPassword,
        uint fRenameOptions
        );

VB Signature:

     Declare Function NetRenameMachineInDomain Lib "netapi32.dll" (TODO) As TODO

C# User-Defined Types:

    internal const uint NETSETUP_ACCT_CREATE = 2;

Alternative Managed API:

Do you know one? Please contribute it!

Notes:

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. Windows NT: This string must begin with \\.

lpNewMachineName - (in) Pointer to a constant string that specifies the new name of the computer. If specified, the local computer name is changed as well. If this parameter is NULL, the function assumes you have already called the SetComputerNameEx function.

lpAccount - (in) Pointer to a constant string that specifies an account name to use when connecting to the domain controller. If this parameter is NULL, the caller's context is used.

lpPassword - (in) If the lpAccount parameter specifies an account name, this parameter must point to the password to use when connecting to the domain controller. Otherwise, this parameter must be NULL.

fRenameOptions - (in) Specifies the rename options. If this parameter is NETSETUP_ACCT_CREATE, the function renames the account in the domain.

Common Errors :

1326 - ERROR_LOGON_FAILURE

Tips & Tricks:

Please add some!

C# Sample Code:

    [DllImport("netapi32.dll", SetLastError=true, CharSet = CharSet.Unicode)]
    internal static extern int NetRenameMachineInDomain(
        string lpServer,
        string lpNewMachineName,
        string lpAccount,
        string lpPassword,
        uint fRenameOptions
        );

    internal const uint NETSETUP_ACCT_CREATE = 2;

    private string _domainUser = @"domain\domainuser";
    private string _domainPassword = "domainpassword";

    private void RenameMachine()
    {
        //This function will change the current PC's name to "PCNEWNAME" using the given domain account.  This changes it both locally and on the domain.

        int error = 0;
        error = NetRenameMachineInDomain(null, "PCNEWNAME", _domainUser, _domainPassword, NETSETUP_ACCT_CREATE);

        if(error == 0)
        {
            MessageBox.Show("Rename Successful.");
        }
        else
        {
            MessageBox.Show("Rename Failed.\r\nError: " + error.ToString());
        }
    }

Documentation