?
Solved

What is a buffer?

Posted on 2003-03-11
3
Medium Priority
?
138 Views
Last Modified: 2010-04-07
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?


0
Comment
Question by:jbauer22
3 Comments
 
LVL 43

Accepted Solution

by:
TimCottee earned 200 total points
ID: 8111492
Hi jbauer22,

The first part:

Declaring a variable as String * x where x is a number will indeed declare a fixed length string of size x. This string will be unitialized and therefore will contain 250 NULL or Chr(0) characters.

When the function is called, lngSize is passed by reference to the function, when it is passed into the function it tells the function the initial size of the string buffer, when it is returned from the function it contains the actual length of the string excluding any NULL characters.

Hope this helps.

Tim Cottee MCSD, MCDBA, CPIM
http://www.timcottee.tk 

Brainbench MVP for Visual Basic
http://www.brainbench.com

Experts-Exchange Advisory Board Member
0
 
LVL 2

Author Comment

by:jbauer22
ID: 8111757
Just to confirm.

Both variables, strbuffer and intSize, are passed ByRef and modified within the API function?
0
 

Expert Comment

by:imd
ID: 8111810
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!

0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Question has a verified solution.

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

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

601 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