How to get the computer name?

I have a COM+ application. I would like to let IIS call this COM+ application and let the COM+ application determine which server IIS creates instance. Security of the ObjectContext object allows me to get the user ID who has created the object. Are there any way for a COM+ application to get the computer name as well? If it is possible, please let me know the way. If someone has the some code Please give me...
Thanks in advance.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
gargjapanAuthor Commented:
I want to know which server IIS creates instance?
This code will return the ComputerName from the Registry.
You may need to test whether it reads the calling computer, or the computer the Com+ object is installed on.

RegCompName = GetSett(HKEY_LOCAL_MACHINE, "SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName", "ComputerName", REG_SZ)


Option Explicit
'WIN32API Costant
Public Const REG_SZ = 1
Public Const HKEY_LOCAL_MACHINE = &H80000002
Public Const ERROR_SUCCESS = 0&

'WIN32 API declaration
Public Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hkey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Public Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hkey As Long) As Long
Public Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hkey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, lpData As Any, lpcbData As Long) As Long         ' Note that if you declare the lpData parameter as String, you must pass it By Value.

Public hCurKey As Long
Public Function GetSett(ByVal hkey As Long, ByVal lpSubKey As String, ByVal lpString As String, ByVal dwType As Long) As Variant
On Error GoTo ErrHandle

    'Data validation
    If hkey = 0 Or lpSubKey = "" Or lpString = "" Then GoTo ErrHandle
    'Open the given hkey + subkey
    If RegOpenKey(hkey, lpSubKey, hCurKey) = ERROR_SUCCESS Then
        'Retrieve the given string value
        Dim strbuff As String
        strbuff = String(255, Chr(0))
        If RegQueryValueEx(hCurKey, lpString, 0, dwType, ByVal strbuff, Len(strbuff)) = ERROR_SUCCESS Then
            GetSett = Left(strbuff, InStr(1, strbuff, Chr(0), vbTextCompare))
            GetSett = "Error"
        End If
        GetSett = "Error opening key"
    End If
    RegCloseKey hCurKey
    Exit Function
    GetSett = ""
End Function

Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Here is a quick and easy way of getting the computer name via API

'This goes in the module
Declare Function getcomputername& Lib "kernel32" Alias "GetComputerNameA" (ByVal lpBuffer As String, nSize As Long)

Public Function ComputerName() As String
On Error Resume Next
Dim irc_string As String
irc_string = String(255, Chr(0))
getcomputername irc_string, Len(irc_string)
ComputerName = irc_string
End Function

Sub Main()
MsgBox "The computer name is " & ComputerName
End Sub
You can just use this:

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

Dim sNomeComputer As String * 40
Dim lLen As Long

' Get the computername
lLen = 40
GetComputerName sNomeComputer, lLen

Descrizione = Mid$(sNomeComputer, 1, lLen)

How about:

MsgBox Environ("ComputerName")

This will give the current computer name in NT and 2000, and I think it also works in 95/98.  If you want a remote computer name, I'm not sure it will work.

The above will not work in Win 9X/Me


I have tried Simone's suggestion and just cannot get it to work..... is there any more someone can add to it??, i dont really want it in a private sub or anything all i require is a string variable that i can add into a piece of my code to show the computer name.

Can anyone add to this??
This will do what you need. Make sure the Declares are at the top of your code. All you need to do to get the machine name is to reference the "GetComputerName" function, i.e. MsgBox GetComputerName

Private Declare Function WSAStartup Lib "WSOCK32.DLL" (ByVal wVersionRequired&, ByVal WSData As String) As Long
Private Declare Function WSACleanup Lib "WSOCK32.DLL" () As Long
Private Declare Function gethostname Lib "WSOCK32.DLL" (ByVal HostName As String, ByVal HostLen As Long) As Long

Function GetComputerName() As String

  Dim HostName As String * 256
  Dim WSData As String * 15
  Dim MachineNam As String

  WSAStartup &H101, WSData

  gethostname HostName, 256
  MachineNam = Trim$(HostName)
  If InStr(MachineNam, Chr$(0)) Then
    MachineNam = Trim$(Left$(MachineNam, InStr(MachineNam, Chr$(0)) - 1))
  End If

  GetComputerName = MachineNam
End Function
Rejecting proposed answer.

Experts, please guidance on whose answer/comment I should accept.

Community Support Moderator @ Experts-Exchange
Greetings, gargjapan!

You've asked 14 questions, but only completed and awarded three of them.  I will update each for you to ensure that you are notified by Email and return to finalize them.

It's time to clean up this topic area and that means taking care of this question. Your options at this point are:
1. Award points to the Expert who provided an answer, or who helped you most. Do this by clicking on the "Accept Comment as Answer" button that lies above and to the right of the appropriate expert's name.
2. PAQ the question because the information might be useful to others, but was not useful to you. To use this option, you must state why the question is no longer useful to you, and the experts need to let me know if they feel that you're being unfair.
3.  Ask Community Support to help split points between participating experts.  Just comment here with details.
4.  Delete the question because it is of no value to you or to anyone else.  To use this option, you must state why the question is no longer useful to you, and the experts need to let me know if they feel that you're being unfair.
If you elect for option 2, 3 or 4, just post comment with details here and I'll take it from there.  We also request that you review any other open questions you might have and update/close them.  Display all your question history from your Member Profile to view details.
Hi Experts:
In the event that the Asker does not respond, I would very much appreciate your opinions as to which Expert ought to receive points (if any) as a result of this question.  Likewise, you can also suggest that I PAQ or delete the question.
Experts, please do not add further "answer" information to this question.  I will be back in about one week to finalize this question.
Thank you everyone.
Moondancer :)
Community Support Moderator @ Experts Exchange
vbhelper and gargjapan are duplicate accounts. What is more, they both have a tendency to abandon their questions as soon as they have their answer.

Their accounts have been closed. Which leaves these open questions as so many loose ends.

Of the participants I would like to ask, please advise. Did anyone provide enough info to deserve the points? Or should this question be deleted?

Thank you

Community Support
Lacking timely response from gargjapan or contributing experts, but seeing that there is useful information here, I recommend:

    Save as PAQ -- No Refund.

DanRollins -- EE database cleanup volunteer
Points reduced and placed in PAQ

E-E Moderator

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.