FIX: Invalid Index Passed to Screen.Fonts Collection Causes GPF

ID: Q180526


The information in this article applies to:


SYMPTOMS

An application error may occur within the Windows CE emulator (pvb.exe) if an invalid index is passed to the Screen.Fonts collection. The error is as follows:

The instruction at "0x01000a68b" referenced memory at "0x00000034". The memory could not be "read".
On the actual Windows CE device, this error causes the application to end without an error dialog.


RESOLUTION

Ensure that the index that is passed to the Screen.Fonts collection is valid. The following code sample shows how a function (IsValidFontIndex) can be used to ensure that the error does not occur:


   Option Explicit
   Private Sub Command1_Click()

      Dim iFontNumber
      'Set iFontNumber to an invalid Screen.Font index value.
      iFontNumber = Screen.FontCount
      If IsValidFontIndex(iFontNumber) Then
         Command1.Font = Screen.Fonts(iFontNumber)
      Else
         'Insert some other error handling or set
         'the font to a default font.
         MsgBox "Invalid Font Index"
      End If

   End Sub

   Private Function IsValidFontIndex(Index)
      If Index >= Screen.FontCount Then
         IsValidFontIndex = False
      Else
         IsValidFontIndex = True
      End If
   End Function 


STATUS

Microsoft has confirmed this to be a problem in the Microsoft products listed at the beginning of this article.

This problem was corrected in Windows CE Toolkit for Visual Basic 6.0.


MORE INFORMATION

Steps to Reproduce Behavior

WARNING: The following code sample will produce an application error in PVB.EXE.
  1. Start a new Windows CE Project in Visual Basic 5.0. Form1 is created by default.


  2. Add a CommandButton to Form1.


  3. Paste the following code into the Declarations section of Form1:
    
          Private Sub Command1_Click()
             Dim iFontNumber
             'Set iFontNumber to an invalid Screen.Font index value.
             iFontNumber = Screen.FontCount
             'The following line of code will produce
             'an application error in PVB.EXE.
             Command1.Font = Screen.Fonts(iFontNumber)
          End Sub 


  4. Press the F5 key to run the project and note that pressing the Command1 button will produce an application error.




© Microsoft Corporation 1999, All Rights Reserved.
Contributions by Mike Dixon, Microsoft Corporation

Additional query words: wince vbce vbce5 vbce6


Keywords          : kbToolkit kbVBp kbVBp500bug kbVBp600fix kbWinCE kbWinCE100 kbGrpVB 
Version           : WINDOWS:1.0
Platform          : WINDOWS 
Issue type        : kbbug 

Last Reviewed: February 25, 1999