SetComputerNameEx() Return Codes?

E

Eric Dropps

Hello all,

Having a problem with SetComputerNameEx() -- the API says it returns 0 on
failure, but for me it is always returning a random number on failure or
success. Interestingly though, the code is working in terms of renaming a
computer, assuming the new name is valid. How can i check to see if the
name is invalid, since the function is always returning a non-zero value?

Sample Code Below:

Private Sub RenameComputer(ByVal strNewName)

Dim RtrnVal

RtrnVal =
SetComputerNameEx(COMPUTER_NAME_FORMAT.ComputerNamePhysicalDnsHostname,
strNewName)

MsgBox(RtrnVal)

If RtrnVal = 0 Then

MsgBox("Error: Could not rename computer. Check to make sure the computer
name does not contain spaces, symbols, or punctuation.")

myCaller.LblStatus.Text = "There was an error renaming the computer"

Else

myCaller.LblStatus.Text = "Computer renamed successfully. Please reboot to
continue."

myCaller.JoinDomainButton.Enabled = False

myCaller.DeptsBox.Enabled = False

End If

End Sub



Eric Dropps
Jr. Systems Administrator
Harvard University FAS Computer Services
(e-mail address removed) 617.495.9768
 
M

Mattias Sjögren

Eric,
How can i check to see if the
name is invalid, since the function is always returning a non-zero value?

You didn't post the most interesting part of your code - your
declaration of the function. But I'm guessing you've taken an old VB6
declaration where the return type is Long instead of Boolean as it
should be in VB.NET. That type of error generally manifests itself in
"random" number being returned (since you're reading garbage off the
stack).

Sample Code Below:

Private Sub RenameComputer(ByVal strNewName)

Dim RtrnVal

Eek! I strongly recommend turning on Option Strict and start typing
your variables.


Mattias
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top