I'm trying to change the name of the computer programmatically. Occasionally we have to wipe a system and restore it's database in an upgrade. I'm trying to have all the system settings be read out of the database and be set up automatically. Most of it is pretty simple stuff, but changing the name of the system is really throwing me for a loop. EDIT: code edited to reflect changes from comments
if(dbHostName.length() > MAX_COMPUTERNAME_LENGTH)
{
dbHostName.truncate(MAX_COMPUTERNAME_LENGTH);
}
LPCTSTR cname = dbHostName.toStdWString().c_str();
bool nameset = SetComputerNameEx(ComputerNamePhysicalDnsHostname, cname);
if(nameset) qDebug() << "Computer name changed to" << dbHostName;
else qDebug() << "Computer name NOT changed!";
I'm taking a QString in, making sure it's not too long, converting it to a standard wide string, converting that to a LPCTSTR and then attempting to use that to change the computer name.
This returns false: Computer name not changed!