Link to home
Start Free TrialLog in
Avatar of jbauer22
jbauer22

asked on

What is a buffer?

This question is mostly academic.

A developer that left our company wrote a function using the GetComputerName API:

Public Function GetServerName()

    Dim strbuffer As String * 250
    Dim lngSize As Long
   
    '--- Get the computer name
    lngSize = Len(strbuffer) + 1
    GetComputerName strbuffer, lngSize
    GetServerName = Mid(strbuffer, 1, lngSize)
   
End Function

It works fine, but I just want to know what's going on here.

First, when you declare a variable as string * 250, what's going on?  Are you limiting the size of that variable to 250 characters?

Second, "lngSize = Len(strbuffer) + 1".  Len(strbuffer) is equal to 250 when I view in the immediate window, but lngSize is equal to 13 the Length of the Computer Name.  What is going on here?  How does 250 + 1 = 13?


ASKER CERTIFIED SOLUTION
Avatar of TimCottee
TimCottee
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jbauer22
jbauer22

ASKER

Just to confirm.

Both variables, strbuffer and intSize, are passed ByRef and modified within the API function?
Just to add to what TimCottee said, for the sake of enrichment (academic :-)

The GetComputerName API function was written in C. It needs to know the length of the buffer you're passing as an argument so it doesn't overwrite the memory past the end of that occupied by your VB string.

It is important to pass strings that will be written to _ByVal_. this modifier has been overridden so functions written in C can get to your string's memory location.

Your question is entitled 'What is a buffer?' A buffer is simply an area of memory used for holding input/output (pretty much a fancy term).

Code on!