|
-
August 3rd, 2004, 10:12 AM
#1
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...
-
August 3rd, 2004, 10:15 AM
#2
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.
-
August 3rd, 2004, 10:32 AM
#3
You know what the sad part is?
Code:
Dim compName as String
compName = Environ$("COMPUTERNAME")
note; NT/2k/XP only
-
August 3rd, 2004, 10:34 AM
#4
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.
-
August 3rd, 2004, 10:52 AM
#5
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!
-
August 3rd, 2004, 11:58 AM
#6
 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.
 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|