Using the Stack

In Microsoft Windows, DLLs must share the same stack space as Microsoft Excel. When a DLL is first called, there is usually about 40K of space left on the stack. When that DLL calls the Excel4 function, Microsoft Excel consumes even more space on the stack. Microsoft Excel tests the amount of available stack space before it runs the function or command specified by the Excel4 function. If the stack would be overrun, Microsoft Excel will not run the command or function, and the Excel4 function call fails. This means that it is very important to use as little stack space as possible. Don't put large data structures on the stack, declare local variables as static if at all possible, and avoid calling functions recursively.

In 16-bit Microsoft Windows, the Excel4 function stack check requires 8.5K of available stack space when the function is called from a DLL called from a macro sheet or in response to an event or button press, and 4K of available stack space when the function is called from a DLL called from a calculating worksheet.

Note

Microsoft Excel 4.0 did not perform this stack checking. Microsoft Excel 97 tests for adequate stack space for the most stack-intensive function or command every time you call the Excel4 function. If there is not enough stack space available for the most stack-intensive function or command, your function call will fail, even if it could run with the available stack space. This "worst case" stack check may cause some Excel4 function calls that worked in Microsoft Excel version 4.0 to fail in Microsoft Excel 97.

To help you in debugging, a function called xlStack is provided, which returns the number of bytes left on the stack. If you suspect that you are overrunning the stack, call this function frequently to see how much stack space is left.

Finally, if you desperately need more stack space than the 40K that you normally get, you can use the Windows functions SwitchStackTo and SwitchStackBack. For more information about these functions, see the documentation for the Microsoft Win32 SDK.

Important

If you switch stacks using SwitchStackTo, you must make sure you use the original stack when calling the Excel4 function.