Go Premium for a chance to win a PS4. Enter to Win


How to get the computer name?

Posted on 2001-06-04
Medium Priority
Last Modified: 2010-05-02
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.
Question by:gargjapan
LVL 54

Expert Comment

by:Ryan Chong
ID: 6154832

Author Comment

ID: 6155148
I want to know which server IIS creates instance?

Expert Comment

ID: 6155161
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 6155565
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

Expert Comment

ID: 6155568
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)

LVL 22

Expert Comment

ID: 6156912
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.


Expert Comment

ID: 6158187
The above will not work in Win 9X/Me



Expert Comment

ID: 6253054
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??

Expert Comment

ID: 6256762
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

Expert Comment

ID: 6400319
Rejecting proposed answer.

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

Community Support Moderator @ Experts-Exchange

Expert Comment

ID: 6418452
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

Expert Comment

ID: 6418518
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
LVL 49

Expert Comment

ID: 7095041
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

Accepted Solution

Computer101 earned 0 total points
ID: 7167837
Points reduced and placed in PAQ

E-E Moderator

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

783 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question