BUG: MULMIX Mixed-Language Example Fails with Win 3.1

ID: Q82293


The information in this article applies to:
  • Microsoft FORTRAN for MS-DOS, version 5.1


SYMPTOMS

When the MULMIX mixed-language C and FORTRAN Windows program shipped with Microsoft FORTRAN version 5.1 is run under Windows 3.1, a dialog box that accepts numeric input may not work correctly. When the user inputs numbers in the two available fields, the OK button may not be enabled and no calculation performed. Erratic program behavior may also be observed.


CAUSE

The C code in the source file MULMIX.C contains an error. The Windows versions 3.0 and 3.1 SDK documentation states that the EM_GETLINE parameter to the SendMessage() API does NOT return a null terminated string. However, in Windows 3.0, the string is actually null terminated. The code in MULMIX.C relies on this undocumented behavior.

In Windows 3.1 the functionality of this message type is as described in the Win 3.0 and 3.1 SDK documentation, and no null character is appended to the returned string. As a result, the string returned by the SendMessage() function never passes the numerical verification test in the MULMIX code and the OK button is not enabled.

Erratic behavior may also result because the absence of the null character terminating the string can cause the strtol() function to access memory not owned by the program.


RESOLUTION

The code in MULMIX.C should be modified to correct this problem. There are two ways to do this:

  • Manually append the null character to the string returned by the SendMessage() function.


  • -or-

  • Use the WM_GETTEXT parameter instead of the EM_GETLINE parameter. WM_GETTEXT will return a null terminated string.



STATUS

Microsoft has confirmed this to be a problem in the sample program MULMIX.C shipped with Microsoft FORTRAN version 5.1. We are researching this problem and will post new information here as it becomes available.


MORE INFORMATION

The following code fragment reproduces the problem (from MULMIX.C):

Sample Code 1


/* Copy edit-control contents to szBuffer */ 

   nNumChars = SendMessage( (HWND) LOWORD( lParam ),
                            EM_GETLINE,
                            0, (LONG)(LPSTR)szBuffer );

   if( nNumChars )
   {
       dNums[n] = strtol( szBuffer, &pc, 10 );
       bNumsOK[n] = (*pc == '\0') || (*pc == ' ');
   }
   else
   {
       bNumsOK[n] = FALSE;
   } 

Sample Code 2


/* This is solution 1, adding the null character:
   Copy edit-control contents to szBuffer
*/ 

   nNumChars = SendMessage( (HWND) LOWORD( lParam ),
                            EM_GETLINE,
                            0, (LONG)(LPSTR)szBuffer );

   if( nNumChars )
   {
       dNums[n] = strtol( szBuffer, &pc, 10 );
       szBuffer[nNumChars] = '\0';     /* new line added. */ 
       pc = &(szBuffer[nNumChars]);    /* new line added. */ 
       bNumsOK[n] = (*pc == '\0') || (*pc == ' ');
   } 

Sample Code 3


/* This is solution 2, using WM_GETTEXT:
   Copy edit-control contents to szBuffer
*/ 

   nNumChars = SendMessage( (HWND) LOWORD( lParam ),
                            WM_GETTEXT,
                            16, (LONG)(LPSTR)szBuffer );

   if( nNumChars )
   {
      dNums[n] = strtol( szBuffer, &pc, 10 );
      bNumsOK[n] = (*pc == '\0') || (*pc == ' ');
   }
   else
   {
      bNumsOK[n] = FALSE;
   } 

Additional query words: 3.00 3.10 5.10 nofps

Keywords :
Version : :5.1
Platform :
Issue type :


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