Results 1 to 2 of 2
  1. #1
    2 Star Lounger
    Join Date
    Jul 2001
    Posts
    132
    Thanks
    0
    Thanked 0 Times in 0 Posts

    declare function (VB6)

    Hello
    I have seen many people using the "declare function" to do stuff, I found this line of code on a wesite suggested by another user, my wuestion is ...what do you do with it? Here is the line of code

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

    Now, can I use this to put the computer names into a textbox?? If so how? I don;t really get this whole api/declare function stuff.

    Thanks

  2. #2
    5 Star Lounger
    Join Date
    Dec 2000
    Location
    Reading/Swindon, Berkshire, United Kingdom
    Posts
    664
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: declare function (VB6)

    Okay,

    to borrow shamelessly from allapi.net:

    'example by Donavon Kuhn
    Private Const MAX_COMPUTERNAME_LENGTH As Long = 31
    Private Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long


    Private Sub Form_Load()
    Dim dwLen As Long
    Dim strString As String
    'Create a buffer
    dwLen = MAX_COMPUTERNAME_LENGTH + 1
    strString = String(dwLen, "X")
    'Get the computer name
    GetComputerName strString, dwLen
    'get only the actual data
    strString = Left(strString, dwLen)
    'Show the computer name
    MsgBox strString
    End Sub

    so, enter the above code into a module and run the sub. you should get a messagebox telling you the name of your computer. The variable strString can be used in any way you like. So to do what you were suggesting in Excel VBA, you'd need to do something like:

    ActiveSheet.TextBox1.Text = strString or Form1.Textbox1.Text = strString

    So, what's going on here? the API (Application Programming Interface) can be viewed as the interface through which programs - such as windows - manage to operate. In the declaration above, kernel32 is referenced. Do a search for this on your computer and you should find kernel32.dll. This will have both this function and many others in it, which windows uses all the time. However, you can also tap into this resource.

    As I said above, get over to allapi.net. The site is no longer maintained but it's still up at the moment. Have a read around and download the API-guide (which is where I pulled the example above from). They also have a set of tutorials over there, though I have never worked though them so can't vouch for them.

Posting Permissions

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