Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Serious SetComputerName problem, need a new method of changing the PC name under Windows Xp please.

Posted on 2002-07-12
15
Medium Priority
?
414 Views
Last Modified: 2007-11-27
I'm using the SetComputerName API under VB6 and Windows XP...

Private Declare Function SetComputerName Lib "kernel32" Alias "SetComputerNameA" (ByVal lpComputerName As String) As Long

Private Sub Command1_Click()
   Dim retVal As Long
   retVal = SetComputerName(Text1.Text)
end sub

This changes the computer name that is seen under the ctrl-alt-del (You're logged on computername/username) but when you go into the My Computer properties and select "Computer Name" tab, it shows the OLD computer name, not the new one.  This creates a very serious problem when trying to add these machines to a domain, as the network name they report to WINS is based on THIS name, not the other name.  

So, I need a new method of changing the computer name that actually changes the computer name everywhere, not just in a few places.

I will add 100 points to this question (200 total) if this is resolved within two hours of posting (11:30 am PST now.)  Not that it matters, but a bonus is always nice.

Also note that I'm searching the web furiously to find a fix, please forgive me if I find one externally and thus no longer need this question.

Thank you for your help.
0
Comment
Question by:EDStech
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 4
  • 2
  • +1
15 Comments
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 7149742
Don't you need to re-boot after calling that API?

Anthony
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 7149744
Well, if you change it manually, you ahve to reboot so i think we have to look at registry also. I will see...
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 7149749
Yep, just looked it up on MSDN:
<quote>
The SetComputerName function stores a new NetBIOS name for the local computer. The name is stored in the registry and takes effect the next time the user restarts the computer.
</quote>

And that is also true of the SetComputerNameEx API function call.

Anthony
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 16

Accepted Solution

by:
Richie_Simonetti earned 400 total points
ID: 7149756
From MSDN:

".....
ComputerName Control Entries
The ComputerName key contains the systems computername.

CAUTION: Do not change the ComputerName values in the Registry. The computername should be changed only by choosing the Network icon in Control Panel or by applications using the Get/SetComputerName APIs.

There are two subkeys under ComputerName: ActiveComputerName and ComputerName. This is because a new computername takes effect only on the next system startup. Changing the computername changes the value in the ComputerName key. When you start the system, the ComputerName key gets copied to the ActiveComputerName key (which is volatile and which provides the computername during that session). These two subkeys appear under the following Registry path:


   HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\ComputerName


These two subkeys each contain a value entry of the following type:

ComputerName   REG_SZ   ComputerName

Default:   ComputerName defined during Setup


Defines the name for the computer as identified for network browsers.
...."
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 7149760
Tony, enought cross posting!, don't you think so?
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 7149764
I just wondering if we could "simulate" rebbot with Post/SendMessage API with Brodcasting.
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 7149771
Little example from www.allapi.net:

Const BSF_POSTMESSAGE = &H10
Const BSM_APPLICATIONS = &H8
Const SC_MINIMIZE = &HF020
Const WM_SYSCOMMAND = &H112
Private Declare Function BroadcastSystemMessage Lib "user32" (ByVal dw As Long, pdw As Long, ByVal un As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Sub Command1_Click()
    'KPD-Team 1999
    'URL: http://www.allapi.net/
    'E-Mail: KPDTeam@Allapi.net
    'Broadcast a system message
    BroadcastSystemMessage BSF_POSTMESSAGE, BSM_APPLICATIONS, WM_SYSCOMMAND, SC_MINIMIZE, ByVal 0&
End Sub
0
 
LVL 2

Expert Comment

by:kevotheclone
ID: 7149773
From I've read about the SetComputerName API is that it sets the computer name to be used the next time the system is restarted.  I'm getting the same results with Windows 2000 Professional.

Is restarting the computer an option?

This unsupported API displays the "System Settings Change" dialog box notifying the user that they need to reboot the PC.

Const EWX_LOGOFF = 0
Const EWX_SHUTDOWN = 1
Const EWX_REBOOT = 2
Const EWX_FORCE = 4
Const EWX_POWEROFF = 8

Private Declare Function SHRestartSystemMB Lib "shell32" Alias "#59" (ByVal hOwner As Long, ByVal sExtraPrompt As String, ByVal uFlags As Long) As Long

Or, you should be able to reboot the PC without prompting the user using this API.

Const EWX_LOGOFF = 0
Const EWX_SHUTDOWN = 1
Const EWX_REBOOT = 2
Const EWX_FORCE = 4
Private Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Long, ByVal dwReserved As Long) As Long
0
 
LVL 2

Author Comment

by:EDStech
ID: 7149852
For the record, the PC is automatically rebooted once the name is changed.  The bad-name still is showing up, even after multiple restarts.

If you manually change the name by hand, it's fine.. but using the SetComputername API, it's not fully changing the name.

One interesting thing to notice,
Lets say the old name is FJ-B989XJ and the new name is Bills-XP.  
Both names are pingable via the wins services... IE, from another PC on the network I can resolve both to the machine's Ip address.  This behavior is present immediately after reboot the first time after changing the PC name.

I'll take a look at the other code suggestions and see what pops, I just wanted to clarify the issue further.  :)

Thanks.
0
 
LVL 2

Expert Comment

by:kevotheclone
ID: 7150065
Now that you've added some much needed clarification, lets continue to clarify the issue...

After the automatic reboot does the old name show up in the "Computer Name" tab of My Computer's properties, or is the old name only visible via network pings?

Since you are automatically rebooting the PC it must be a warm boot.  When you change the name manually are you also warm rebooting, or are you completely powering down the PC?

When you find yourself in this "old name/new name" situation, have you searched the registry for the old name.  If found have you tried to add a programatic registry change to the SetComputerName code block?

0
 
LVL 2

Author Comment

by:EDStech
ID: 7150299
"After the automatic reboot does the old name show up in the "Computer Name" tab of My Computer's properties, or is the old name only visible via network pings?"

I'm rebooting by SHELL'ing out the "Shutdown -r" command.  I've been playing with methods of rebooting the PC from within VB, but until now, none have worked very well.. but the suggestions above are a good start on that unrelated area.  :)

After a hard reboot (shutdown, power off, power on) the computer name shows up wrong in the Computer Name on My PC.. but it shows up right under ctrl-alt-del.

"Since you are automatically rebooting the PC it must be a warm boot.  When you change the name manually are you also warm rebooting, or are you completely powering down the PC?"

When manually changing it, I just click "Yes" on the "You must reboot to take effect" dialog..  But the name remains wrong even after a physical shutdown/poweroff.

"When you find yourself in this "old name/new name" situation, have you searched the registry for the old name.  If found have you tried to add a programatic registry change to the SetComputerName code block?"

I am not changing the name via the registry currently, but long ago I had tried to registry method first, and it had no effect on the system itself.  But I'll do a search on the current registry to see what I find, maybe the two tactics used together will yeild results.

I'll get back to you shortly... :)
0
 
LVL 2

Author Comment

by:EDStech
ID: 7150779
Well, that's mighty odd, but I guess the registry kick combined with the API vb code was the trick.  

I first change the name using the API code, and then I edited..
HKLM\System\CurrentControlSet01\Services\TCPIP\Paramaters\Hostname=(Blah)
And the CCS02 entry.. after rebooting the three name changes propigate all across the system, and all becomes well.

So the setname api alone isn't enough, you gotta combine it with the registry fix.  But the registry fix alone isn't enough either, you need to combine it with the API fix.  Go figure.

Either way, thanks all for the help.  And thanks for the reboot code, I had another example of the ExitWindowsEX function which didn't work for beans, this code works much better, thus far.

Thanks.
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 7150989
After reading your last comment i am lost.
did you accept the right comment?
0
 
LVL 2

Author Comment

by:EDStech
ID: 7151021
Yeah, the solution to my problem here was to change the name in the registry, as you specified in your accepted comment.  :)

The problem is that the setcomputername API wasn't changing the name right for me, even after rebooting.  The fix was to change the name in the registry in currentcontrolset01 and 02 as well as the API change.. once I did that, it changed just fine and is perfectly the way it should be.

Hope that clarified.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question