?
Solved

What is a buffer?

Posted on 2003-03-11
3
Medium Priority
?
135 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month9 days, 23 hours left to enroll

762 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