Results 1 to 10 of 10

Thread: GetComputerName

  1. #1

    Thread Starter
    Addicted Member aturner's Avatar
    Join Date
    Nov 2000
    Posts
    179

    Cool GetComputerName

    Every time i try to run GetComputerName my system crashes. Has anyone had this before? Does anyone know why this is happening?

    TIA
    Due to the energy crisis, the light at the end of the tunnel has been turned off.
    Sorry for any inconvenience this may cause

  2. #2

    Thread Starter
    Addicted Member aturner's Avatar
    Join Date
    Nov 2000
    Posts
    179
    mistake!!! Only VB6 crashes, soz
    Due to the energy crisis, the light at the end of the tunnel has been turned off.
    Sorry for any inconvenience this may cause

  3. #3
    Megatron
    Guest
    Maybe you're using it wrong. Post your code up.

  4. #4
    Megatron
    Guest
    Here's some code that should work.
    VB Code:
    1. Private Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long
    2.  
    3. Private Sub Command1_Click()
    4.  
    5.     Dim sName As String
    6.     Dim iLength As Long
    7.    
    8.     sName = Space$(255)
    9.     iLength = GetComputerName(sName, 255)
    10.     sName = Left$(sName, iLength)
    11.    
    12.     MsgBox sName
    13.    
    14. End Sub

  5. #5

    Thread Starter
    Addicted Member aturner's Avatar
    Join Date
    Nov 2000
    Posts
    179
    thanks for your help, what i wasnt doing was filling the string with spaces. (why do i have to do that?)

    little mod to the left bit (why left$?) and it was working fine.

    thanks Alex.

    Dim sName As String
    Dim iLength As Long

    sName = Space$(255)
    iLength = GetComputerName(sName, 255)
    sName = Left$(sName, Len(sName) - iLength)
    Label2.Caption = sName
    Due to the energy crisis, the light at the end of the tunnel has been turned off.
    Sorry for any inconvenience this may cause

  6. #6

    Thread Starter
    Addicted Member aturner's Avatar
    Join Date
    Nov 2000
    Posts
    179
    for some reason the length came out as one, this did seem abnormal but all i needed to do was take the null terminator off the string (i think)
    Due to the energy crisis, the light at the end of the tunnel has been turned off.
    Sorry for any inconvenience this may cause

  7. #7

    Thread Starter
    Addicted Member aturner's Avatar
    Join Date
    Nov 2000
    Posts
    179
    stupidity strike me again!!!

    i think this is how it should have been

    Dim sName As String
    Dim iLength As Long
    Dim result As Long


    sName = Space$(255)
    iLength = 255
    result = GetComputerName(sName, iLength)
    sName = Left$(sName, iLength)
    Label2.Caption = sName

    Due to the energy crisis, the light at the end of the tunnel has been turned off.
    Sorry for any inconvenience this may cause

  8. #8
    Addicted Member
    Join Date
    Mar 2001
    Location
    Greece
    Posts
    164

    Why spaces ?

    It's not necessary to fill the name$ variable with spaces; you could fill it in with nulls (ASCII code 0).

    e.g.
    Dim sName as String


    It is absolutelly necessary though to

  9. #9
    Addicted Member
    Join Date
    Mar 2001
    Location
    Greece
    Posts
    164

    ...continued...

    Sorry about the unfinished message. Here it goes :


    Dim sName as String

    sName=string(255,0)

    (maybe not char 0 but char let's say 20)


    this way, a **constant** memory block of 255 chars in size is allocated to a variable (sName) and can be safely passed to the API call. On the other hand, a variable with no initialization occupies dynamically the memory, say : sName=sName+"hello"
    All similar API calls I know of require this.

    I am not sure if I made myself clear, maybe reading some of the C basics (LPSTR compared to BSTR in Visual Basic) would help.

  10. #10
    Megatron
    Guest
    It's not necessary to fill the name$ variable with spaces; you could fill it in with nulls (ASCII code 0).
    You could fill them with whatever you want, just make sure they're filled.

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