I am trying to change the computer name of a domain computer using SetComputerNameEx()
. The function returns success but when i restart the computer it still has the old computer name. SetComputerNameEx()
works perfectly for non domain machines I am facing this issue only on domain computers. Am i missing something?
EDIT: I am using the following code.
BOOL ChangeCompName(CString csCompName)
{
if( !SetComputerNameEx(ComputerNamePhysicalDnsHostname,csCompName.GetBuffer()))
{
_tprintf(L"Failed to set Physical DNS name %d\n",GetLastError());
return FALSE;
}
else
{
_tprintf(L"Computer name change Success\n");
}
if( !SetComputerNameEx(ComputerNamePhysicalNetBIOS,csCompName.GetBuffer()))
{
_tprintf(L"Failed to set Physical NetBios name %d\n",GetLastError());
return FALSE;
}
else
{
_tprintf(L"Computer name change Success\n");
}
return TRUE;
}
int main()
{
if(!ChangeCompName(L"NewCompName"))
{
_tprintf(L"Failed to change Name.\n");
return 1;
}
else
{
_tprintf(L"Computer name change.\n");
}
return 0;
}
I tried running the code with both administrator and system privileges. everytime i run the code it returns success but does not change the computer name. strange thing is that this code works perfectly for workgroup computer and does not work for domain computers.
I figured out how to do a computer rename of a domain machine. Here is the solution if anyone needs it.
Instead of calling SetComputerNameEx() Just use NetGetJoinInformation() with domain administrator credential. This will rename the computer in both the domain and also locally.