CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jul 2003
    Location
    Florida
    Posts
    651

    Get Computer Name Question

    I found the following example in a response by Cimperiali in regards to how to get the ComputerName. It seems pretty straightforward, but I have a question...

    Is there a reason that MAX_COMPUTERNAME_LENGTH is set in a Constant? Can't you just put 'dwLen = 32' instead of 'dwLen = MAX_COMPUTERNAME_LENGTH + 1' since we know that 'MAX_COMPUTERNAME_LENGTH + 1' is always 32?


    Code:
    'example by Donavon Kuhn ([email protected])
    Private Const MAX_COMPUTERNAME_LENGTH As Long = 31
    Private Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long
    Private Sub Form_Load()
     Dim dwLen As Long
     Dim strString As String
     'Create a buffer
     dwLen = MAX_COMPUTERNAME_LENGTH + 1
     strString = String(dwLen, "X")
     'Get the computer name
     GetComputerName strString, dwLen
     'get only the actual data
     strString = Left(strString, dwLen)
     'Show the computer name
     MsgBox strString
    End Sub
    I'd rather be wakeboarding...

  2. #2
    Join Date
    Jan 2001
    Location
    Germany
    Posts
    222
    Sure, but it makes the code much more readable. 32 is just a number which doesn't really have a meaning. Look at it in a few months from now and you won't know where it comes from. The way it is done here, with a constant, you won't have any problems understanding it.

    Also, if the number should change, it's easier to put it in a constant and change it once than having to change it wherever it appears. I guess it comes down to the fact that putting this stuff in a constant is just good programming practice.
    Teamwork Software - Stuff That Does Something

  3. #3
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104
    You know what the sad part is?

    Code:
    Dim compName as String
    compName = Environ$("COMPUTERNAME")
    note; NT/2k/XP only
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

  4. #4
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726
    btw, nothing of wrong in making it:
    Private Const MAX_COMPUTERNAME_LENGTH As Long = 32
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  5. #5
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104
    by the way, the max lenght for a computer name allowable under windows xp, is 63 characters. try setting your name to this:
    123456789012345678901234567890123456789012345678901234567890123
    and then type some more.. denied

    so maybe that code should be max length of 63,
    dont forget that 1 is added to the 63, because the string MUST be terminated with a 0x00 character (64 characters, less one for null terminator = 63)

    that's under win xp..
    i think under other primitive os, and maybe NT too, max length is 32, less one null terminator, hence 31. I vote CImperialis code be left alone at 31, as it more accurately reflects the truth; (unless its windows xp, then it should = 63)

    additionally, the maximum length for a NETBIOS name is 15 bytes, so you can create network headaches for yourself by making computers with names longer than this..

    so stick to names less than 16, and all problems are solved!
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

  6. #6
    Join Date
    Jul 2003
    Location
    Florida
    Posts
    651
    Quote Originally Posted by cjard
    You know what the sad part is?

    Code:
    Dim compName as String
    compName = Environ$("COMPUTERNAME")
    note; NT/2k/XP only
    I would love to use this in my code instead of the other, but a lot of the client computers are still running Win98.


    Quote Originally Posted by Captain Nuss
    Also, if the number should change, it's easier to put it in a constant and change it once than having to change it wherever it appears.
    I agree, but for this particular instance, I would only need to use it once.

    Thanks for all your input!
    I'd rather be wakeboarding...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured