Developer FAQs - Platform

This page contains several FAQs on Platform issues of developing in Excel.
The Code segments below can be selected in your browser and copied into an Excel VBA module and run. You are free to use any code on this page for personal non-distributed use.


How do I avoid running out of memory or system resources?

What is vtable binding?

What is the difference between late binding and early binding?

How do I Pass Data to DLLs from Visual Basic for Applications?

 

How do I avoid running out of memory or system resources?
Keywords: Memory Resources Usage Optimize
Posted October 14, 1996

Here are some general tips for reducing memory and resources usage:

What is vtable binding?
Keywords: VBA OLE VTable Virtual
Posted March 22, 1996

There are two forms of Early binding. The first and slower method utilizes the Invoke method of the OLE IDispatch interface. The second and faster method utilizes a technique called virtual table (vtable) binding. It uses an offset into a virtual function table to determine what code to call. Calling a vtable-bound method for an in-process OLE server typically requires no more overhead than calling a function in a DLL. What does this mean to you as a Microsoft Office programmer? Almost nothing. This is all taken care of behind the scenes.

What is the difference between late binding and early binding?
Keywords: OLE Late Early Compile Type Library
Posted March 22, 1996

Late binding and early binding are terms that are typically heard when discussing OLE Automation. Binding refers to a process that takes place during compilation. During compilation the compiler needs various pieces of information about objects it is dealing with to do syntax checking and other operations. Early binding means that this information for an object is determined at compile time. This is typically done by referencing a type library. The type library provides all of the information required by the compiler. Late binding means that the information for an object is determined at run time. This is typically done by querying the interfaces of the running object and determining what methods are valid. This process must occur each and every time the application runs and is slow. Early binding is typically twice as fast as late binding.

How do I Pass Data to DLLs from Visual Basic for Applications?
Keywords: DLL API Declare
Posted February 10, 1996

Arrays
In versions of Microsoft Excel prior to 5.0 arrays were passed to and from DLLs using the XLOPER structure. This structure was quite similar to how arrays were maintained internal to Excel and made quite a bit of sense. When VBA was added to Excel v5.0 a second method of passing array data was introduced. This new method is based upon the OLE 2.0 SafeArray data type. This makes sense when you realize how heavily dependent upon OLE VBA is. The SafeArray data type is allocated using a series of OLE 2.0 functions which all start with SafeArray. The functions you are most likely to use are:


Function

Description


SafeArrayCreate

Creates a SafeArray


SafeArrayDestroy

Destroys a SafeArray


SafeArrayGetDim

Gets the dimensions


SafeArrayGetElement

Gets an element


SafeArrayGetLBound

Gets the lower bound


SafeArrayGetUBound

Gets the upper bound


SafeArrayPutElement

Puts an element into the structure


SafeArrayRedim

Changes the size

When Excel passes you a SafeArray it is passed as a SAFEARRAY **pSafe. Most of the functions will expect you to pass *pSafe.

Strings
Strings are now passed from VBA as BSTRs. A BSTR is another OLE data type that provides a standard way to allocate and free strings across components. While a BSTR is supposed to be an opaque data type you will find that it is very similar to the byte counted strings you may have encountered when working with XLOPERs. When you pass a string by reference, you should declare the argument as a BSTR *. When you pass a string by value, you should declare the argument as just a BSTR.

Note: Excel passes strings in an odd way. All strings are passed as ANSI strings. This holds true even under Win32 where BSTRs are expected to be UNICODE based. To get around this problem use the Byte versions of the BSTR functions, i.e. SysAllocStringByteLen.

You can download excellent sample code revealing the master's secrets for passing a SafeArray to a C-based DLL.

Back to Top


Back Back to the FAQ Table of Contents


  Random Thoughts...
Be nice to your kids. They'll choose your nursing home.


Copyright© 1996-1999, Baarns Consulting Group, Inc. - All rights reserved.