Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I change the computer name in VB6

Status
Not open for further replies.

tedsmith

Programmer
Nov 23, 2000
1,762
0
0
AU
I want to be able to change the name of a computer to the name of the user when he plugs in a USB memory chip that will always have his name on a file.
This is so other people on the workgroup can easily find him no matter where he is.
I can have a small app. always running in the background on all computers.
My question is how do I change the name with VB6 code?
I presume I have to change the registry and maybe reboot the computer but is that all I have to do - and how do I do it?

Also is there a way this can be done automatically without having to pre-install a VB6 program on each computer or can I run a VB6 app. from a USB chip without having to install it first?
 
But beware of SetComputerName if your workstation is in a domain. You might want to investigate the NetRenameMachineInDomain API call
 
Makes it very hard to resolve network problems as well.

-David
2006 & 2007 Microsoft Most Valuable Professional (MVP)
2006 Dell Certified System Professional (CSP)
 
Thanks
The computers are only a simple workgroup.
I notice there is also a SetComputerNameEx function that changes the Netbios etc. and looks very complex.
I presume I would only have to use SetComputerName as I want the name seen by other computers to change but not necessarily its local address. Would this one also change the Remost host name?

For trouble shooting I could have a name that combines its normal name with the user name Eg. Mike_A123 and never change the A123 part.
 
I also note there is a Private Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Long, ByVal dwReserved As Long) As Long.
What is the difference between using this and SHRestartSystemMB that Hypetia suggests?
Can you force a reboot by using EWX_FORCE + EWX_Reboot ?
 
ExitWindowsEx initiates a restart, shutdown or logoff request.

SHRestartSystemMB also does the same but prompts the user using a standard message box that looks like this.
[tt]
---------------------------
System Settings Change
---------------------------
You must restart your computer before the new settings will take effect.

Do you want to restart your computer now?
---------------------------
Yes No
---------------------------
[/tt]
The user can choose to restart immediately by choosing Yes or ignore the advice otherwise. The return value of the function is same as the VB's MsgBox function, i.e. vbYes or vbNo indicating which button was pressed. If user chooses Yes, the function itself takes the specified action (perhaps by calling the ExitWindowsEx function internally).

In addition to the standard prompt shown above, you can also provide some further details using the sExtraPrompt parameter. Note that you need to convert your string to Unicode format for Windows XP and other Unicode operating systems.

The uFlags parameter has the same meaning for both functions. You can use EWX_REBOOT + EWX_FORCE to force a reboot. However, SHRestartSystemMB will still require user's consent to take action.

The last thing is... SHRestartSystemMB function is undocumented and it may be arbitrarily modified or removed in future versions of Windows. I am not aware of its existance on Windows Vista. So use it at your own risk.
 
I'd be really unhappy to find my computer names changing sporadically. It's likely to break file and print shares and cause untold other havoc.

Surely it wouldn't be too much trouble to build a UDP or Mailslots name resolver service into your application? Clearly this is just a LAN-based application.

Whether chewing up UDP ports or just using the far larger Mailslots namespace a new client could "ping" for other users to report their locations back by doing a broadcast. This way no prebuilt user name list is needed either, every active copy would just report back its user name and machine name when it saw the broadcast query. The querying program could build a user/machine namelist from the replies.

As individual copies of the program "go away" (shut down) they could broadcast that fact too. Or each peer might do a broadcast query every 30 seconds to 5 minutes to keep namelists current.

If you want to simply know when "Joe" is active (and where) simply broadcast a specific query and have only a machine with "Joe" as the user reply.

Yes, a little bit of network traffic. Not a lot though and much less obnoxious than changing machine names. Heck, what happens when a different user logs on? Change the name again?
 
Er, thats for something very different from simply renaming a PC
 
It will definitely change computer name.
here is how I used it
Dim oShell As New WshShell
strTmp = "C:\newsid.exe /a /n " & SNewName
oShell.Run strTmp, 0, True


Sam
 
Sure, it can rename a PC, but that isn't what it is for. Most of the time you don't want your registry having hundreds of changes made to it keys and keys values, nor do you want your entire filing system and registry to get their security descriptors changed, just because you have renamed the PC.

 
good point strongm. I used this in a situation where the pc's are brand new, freshly ghosted and haven't joined the domain yet so it hasn't caused any problems here

Sam
 
Ok, the use I envisiaged was not just a casual thing every time someone sits down in front of a conputer.
The situation might be that one person was away on a job for a few days so someone else uses his computer for those few days until he returns - or when a computer has failed and has to be replaced by a spare while it is away being repaired.
I would like that computer to be temporarily known to everyone else by the name of the person using it so they can exchange files etc without changing who they send to.
Is there any other way doing this, apart from changing the registry and rebooting? There is no domain involved.
I didnt want to have a special copying program running on all the other computers and just use the standard Windows stuff so the listening winsock and accept ID method would not work.
 
You could have a "personal" network space(logical drive) assigned to each user. It would be available wherever the user might login. That is pretty much the extent of my knowledge on the subject. I would recommend one of the network forums for further details.

Sam
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top