SetComputerNameEx() Return Codes?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Eric Dropps

    SetComputerNameEx() Return Codes?

    Hello all,

    Having a problem with SetComputerName Ex() -- 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 =
    SetComputerName Ex(COMPUTER_NAM E_FORMAT.Comput erNamePhysicalD nsHostname,
    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.LblSta tus.Text = "There was an error renaming the computer"

    Else

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

    myCaller.JoinDo mainButton.Enab led = False

    myCaller.DeptsB ox.Enabled = False

    End If

    End Sub



    Eric Dropps
    Jr. Systems Administrator
    Harvard University FAS Computer Services
    dropps@fas.harv ard.edu 617.495.9768

  • Mattias Sjögren

    #2
    Re: SetComputerName Ex() Return Codes?

    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

    --
    Mattias Sjögren [C# MVP] mattias @ mvps.org
    http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
    Please reply only to the newsgroup.

    Comment

    Working...