Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 421
  • Last Modified:

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

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
EDStech
Asked:
EDStech
  • 7
  • 4
  • 2
  • +1
1 Solution
 
Anthony PerkinsCommented:
Don't you need to re-boot after calling that API?

Anthony
0
 
Richie_SimonettiIT OperationsCommented:
Well, if you change it manually, you ahve to reboot so i think we have to look at registry also. I will see...
0
 
Anthony PerkinsCommented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Richie_SimonettiIT OperationsCommented:
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
 
Richie_SimonettiIT OperationsCommented:
Tony, enought cross posting!, don't you think so?
0
 
Richie_SimonettiIT OperationsCommented:
I just wondering if we could "simulate" rebbot with Post/SendMessage API with Brodcasting.
0
 
Richie_SimonettiIT OperationsCommented:
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
 
kevothecloneCommented:
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
 
EDStechAuthor Commented:
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
 
kevothecloneCommented:
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
 
EDStechAuthor Commented:
"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
 
EDStechAuthor Commented:
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
 
Richie_SimonettiIT OperationsCommented:
After reading your last comment i am lost.
did you accept the right comment?
0
 
EDStechAuthor Commented:
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 7
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now