Results 1 to 8 of 8
  1. #1
    Lounger
    Join Date
    Nov 2001
    Location
    Fort Dodge, Iowa, USA
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts

    WinAPI calls - this works HOW? (Access 97/VBA)

    I turned a tip - I can't remember from where - into a function that returns the running computer's identifier...

    Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA" _
    (ByVal Buffer As String, Size As Long) As Long

    ...

    Public Function fx_system_get_Comp_ID() As String
    Dim RetVal As Long
    Dim computerName As String
    Dim ComputerNameBuff As String * 16
    RetVal = GetComputerName(ComputerNameBuff, Len(ComputerNameBuff))
    computerName = Left(ComputerNameBuff, InStr(ComputerNameBuff, vbNullChar) - 1)
    fx_system_get_Comp_ID = UCase(computerName)
    End Function

    It works GREAT! Problem is...I don't know HOW. I know that the information is being extracted by the...
    RetVal = GetComputerName(ComputerNameBuff, Len
    (ComputerNameBuff))
    ...line but only because, until the function executes that particular line, ComputerNameBuff is full of spaces. After execution, it has the computer ID, right-padded with spaces.

    From what (little) I know of how functions work, I'd expect GetComputerName to return the name. The way it's used, it looks to (poor, inexperienced) me like I'm passing a function variables - the value of which I never set - and then referencing that PREVISOUSLY-UNSET variable (instead of the function) to figure out the computer name.

    Me very confused...

    Please help...oh knowers of the unknowable...

    malkor

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: WinAPI calls - this works HOW? (Access 97/VBA)

    The Windows API is a big topic. Generally speaking, the return value of an API function is some information about the success/failure of the function or the size of something you wanted to know about. You pass the function one or more blank strings so that there is a place in memory for the function to store information into. Frankly, I don't know the best place to start studying this. Going into the online help for the Declare statement would be the tip of the iceberg. Whole books have been written...

    And... learning the API is less necessary than before because you can gather a lot of environment information using VBScript and the Windows Script Host. Paste this into a new text file, rename it with a .VBS extension, and enjoy the power:
    <pre>Dim objNetwork
    Set objNetwork = WScript.CreateObject("WScript.Network")
    MsgBox "You are " & objNetwork.UserName & ", using " & _
    objNetwork.ComputerName & ", in domain " & _
    objNetwork.UserDomain & "." & vbCrLf & vbCrLf & "Howdy!"
    Set objNetwork = Nothing</pre>


  3. #3
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: WinAPI calls - this works HOW? (Access 97/VBA)

    If you have an Access-related question, it's better to post it in the Access forum (where we also understands the workings of VBA as applied to Access <img src=/S/grin.gif border=0 alt=grin width=15 height=15>). In this case, though, your question applies to the Windows API, which is not, strictly speaking Access or VB/VBA, but the answer really applies to VB/VBA. Confused enough yet? <img src=/S/grin.gif border=0 alt=grin width=15 height=15>

    GetComputerName, like many API calls, returns a long that tells you whether it succeeded. However, you would normally pass it a pointer to a buffer variable and a pointer to a long variable, and it also populates those variables. You gave it a 16 character string buffer, so it always fills it with a 15 character string and a Null terminator, no matter what the actual length of the computer name was. Normally, you would also pass it a long variable containing the maximum length of the buffer, rather than passing it the value as you have done. In other words, you would dim lngLength as Long, set it's value to 16, and pass that into the API call.

    What GetComputerName does is populate the string buffer with the padded computer name and a Null termination character AND, if you passed it a long variable, it populates that with the *actual* length of the computer name. Then all you have to do is say:

    ComputerName = Left(ComputerNameBuff, lngLength)

    It works to do it the way your code does, but it's harder to understand.
    Charlotte

  4. #4
    4 Star Lounger
    Join Date
    Feb 2001
    Location
    Gillingham, Kent, England
    Posts
    511
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: WinAPI calls - this works HOW? (Access 97/VBA)

    Hi jscher2000,

    Is it possible to get VB script running in plain old VB? Im getting Object Required on the following line:
    Set objNetwork = WScript.CreateObject("WScript.Network")

    Im currently trying to find the users local domain.

  5. #5
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: WinAPI calls - this works HOW? (Access 97/VBA)

    CreateObject is native VB/VBA, so you can use it without qualification:

    Set objNetwork = CreateObject("WScript.Network")

  6. #6
    4 Star Lounger
    Join Date
    Feb 2001
    Location
    Gillingham, Kent, England
    Posts
    511
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: WinAPI calls - this works HOW? (Access 97/VBA)

    Thanks Hans, worked like a treat!

    Jscher2000's bit of code was the final piece to a puzzle i'd been working on since finding MSDE (freeSQL) didn't include the dll which lists the servers available!

  7. #7
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: WinAPI calls - this works HOW? (Access 97/VBA)

    Glad it worked. Could you mention in your other thread (about the list of SQL servers) that the problem has been solved? Thanks.

  8. #8
    4 Star Lounger
    Join Date
    Feb 2001
    Location
    Gillingham, Kent, England
    Posts
    511
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: WinAPI calls - this works HOW? (Access 97/VBA)

    No probs.
    I was gonna put in the bit of code that actaully lists the servers. Im currently trying to find the page I got it from so I can give credit where its due.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •