Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to get Server Name? 1

Status
Not open for further replies.

cgreynes

Programmer
Feb 28, 2003
29
US
hi,

is there a way to get the system's name?

the server's name value will be saved in the database together with the error number.

thanks
clark
 
Do you mean the name of the computer that the app is running on?

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
yes, an app is running on each server. there are two app servers which are managed by load balancing. for ex, if server 1 issued an error to the user then the vb app will pass a value such as "AppServer1" and the error number to the database.

my other option is to have a server name constant, but my manager wants to minimize hard coded values.

 
I'm not currently running a server OS so I'm not sure which of the Environment Variables you would need. To find out, open a Cmd window and type Set and press enter. This will list all the Environment variables on the machine, one of them will be the computer's name (on XP Pro its COMPUTERNAME). In your program you just need to use the Environ(VariableName) function to get the information.

Hope this helps.
 
thanks earthandfire!

both are windows 2000 servers and environment variable is still COMPUTERNAME. I already tried it and it worked.

Thank you so much!!!:D The function you provided is very helpful.

 
It isn't 100% reliable, of course ..."

does this mean that sometimes it does not get the right value of COMPUTERNAME?
 
No, it means that COMPUTERNAME doesn't necessari;y holf the genune computer name ... Users (and programs) can easily set it to anything they like

A quick search in this forum should find a number of posts on this subject
 
Aside from COMPUTERNAME,I can use USERDOMAIN and LOGONSERVER.

Do you think that it is better to use LOGONSERVER or it is also easy to change due to the nature of environment variables?

I searched about changing LOGONSERVER and I did only see a few post.

Thanks!
 
strongm is right, the Environment variables aren't a totally reliable source of information:

You could try:

Declare an API function -

Private Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA" (ByVal lpBuffer As String, lngSize As Long) As Long

and to use it:

Dim RetVal As Long
Dim strComputerName As String
Dim BufferSize As Long

BufferSize = 255 'Max Size
strComputerName = Space(BufferSize) 'Fill the buffer with spaces
RetVal = GetComputerName(strComputerName, BufferSize)

- you mat want to trim trailing spaces off strComputerName


RetVal is true (non 0) if Function is successful otherwise false (0)

Hope this helps - but in general I think Environ is normally sufficient.
 
Thinking about it, I believe the returned string will be an ASCIIZ (terminated with Chr(0)) - so

After

RetVal = GetComputerName(strComputerName, BufferSize)


If RetVal = 0 Then
MsgBox "Unable to get Computer Name"
Else
strComputerName = Left(strComputerName,InStr(strComputerName, Chr(0)) - 1)
End If


Hope this helps.
 
hi earthandfire,

is GetComputerName() function built-in? or do i have to make it?

thanks
 
Its "built-in" as far as its part of Windows (its in kernel32.dll)

You need to declare a reference to it which you could do in a global module

this is the line that begins Private Declare Function

then whereever you need to use it it will act like a built-in function called GetComputerName

Hope this helps.



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top