External Procedure Declaration Can Cause Crash in MS Excel

ID: Q152206


4.00    | 3.50 3.51
WINDOWS | WINDOWS NT
kbprg 

The information in this article applies to:
  • Microsoft Excel Software Development Kit, version 5.0
  • Microsoft Excel for Windows 95, version 7.0


SUMMARY

A missing data type of the value returned by an external DLL function can cause Microsoft Excel to crash. If the prototype for the function is not VOID, the "Declare" statement should specify the correct return value.


MORE INFORMATION

For example, to get the Windows Directory, the GetWindowsDirectory function from the Windows API could be used from VBA. The prototype for GetWindows Directory is:


   UINT GetWindowsDirectory(
      LPTSTR lpBuffer,    // address of buffer for Windows directory
      UINT uSize  // size of directory buffer
      ); 
Following is the VBA code that will cause an Application Error during the calling of GetWindowsDirectory:

   Declare Function GetWindowsDirectory Lib "kernel32.dll" _
      Alias "GetWindowsDirectoryA" _
      (ByVal lpBuffer As String, ByVal uSize As Long)

   Sub ThisCrashes()
      Dim lpBuffer As String
      Dim uSize As Long
      uSize = 100
      lpBuffer = Space(uSize)
      GetWindowsDirectory lpBuffer, uSize
      MsgBox lpBuffer
   End Sub 
If the following Declare statement is used, specifying the return type, the procedure works correctly:

   Declare Function GetWindowsDirectory Lib "kernel32.dll" _
      Alias "GetWindowsDirectoryA" _
    (ByVal lpBuffer As String, ByVal uSize As Long) As Long 

Additional query words: 3.50 3.51 4.00 xlsdk xldk vba

Keywords : kbnokeyword
Version : :5.0; Win95:7.0
Platform : Win95
Issue type :


Last Reviewed: November 11, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.