PRB: Unexpected Results from _FindVar() in FoxPro for MS-DOS

ID: Q122357

The information in this article applies to:

SYMPTOMS

The _FindVar() function, when used with FoxPro for MS-DOS, returns 1 (TRUE) when the variable passed to this function does not exist.

CAUSE

An invalid (negative) NTI value was passed to the _FindVar() function.

RESOLUTION

Do not pass negative NTI values to the _FindVar() function. The NTI value is passed to the _FindVar() function as the first parameter of that function.

For a technique you can use to determine if a variable exists, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q115990
   TITLE     : How to Determine If a Variable Exists in Memory

STATUS

This behavior is by design. The behavior of the _FindVar() function is undefined when negative values are passed to the function. The behavior of _FindVar() can only be predicted when valid NTI values are passed to the function.

MORE INFORMATION

FoxPro Code to Reproduce Behavior

CLEAR

IF "Win"$VERS()

   SET ALTERNATE TO testwin.txt
   SET LIBRRARY TO testwin.dll

ELSE

   SET ALTERNATE TO testdos.txt
   SET LIBRARY TO testdos.exe

ENDIF

SET ALTERNATE ON

testvar = "Hello this is a test"

? "Test with a variable name that does exist ..." ? = qmark("testvar")

? ? "Test with a variable name that does not exist ..." ? = qmark("notexist")

SET LIBRARY TO SET ALTERNATE TO CLOSE ALTERNATE

C Code to Reproduce Problem

#include <stdlib.h>
#include <stdio.h>
#include <pro_ext.h>

void FAR NameTableIndexEx(ParamBlk FAR *parm)
{

     NTI nti;
     char FAR *name;
     Locator loc;
     Value val;
     int WhatFind ;
     char buffer[20];

//
//     Null terminate character string, name of variable
//

 if (!_SetHandSize(parm->p[0].val.ev_handle, parm->p[0].val.ev_length + 1))
     {
          _Error(182); // "Insufficient memory"
     }

     _HLock(parm->p[0].val.ev_handle);

     name = (char FAR *) _HandToPtr(parm->p[0].val.ev_handle);
     name[parm->p[0].val.ev_length] = '\0';

     nti = _NameTableIndex(name);

     WhatFind = _FindVar(nti, 1, &loc);

    _itoa( WhatFind, buffer, 10 ) ;
    _PutStr("\nThe value returned by FindVar (where = 1) is: ") ;
     _PutStr(buffer) ;

     WhatFind = _FindVar(nti, -1, &loc);

    _itoa( WhatFind, buffer, 10 ) ;
    _PutStr("\nThe value returned by FindVar (where = -1) is: ") ;
     _PutStr(buffer) ;

     WhatFind = _FindVar(nti, 0, &loc);

    _itoa( WhatFind, buffer, 10 ) ;
    _PutStr("\nThe value returned by FindVar (where = 0) is: ") ;
     _PutStr(buffer) ;
    _PutStr("\n") ;

}

FoxInfo myFoxInfo[] = {

     {"QMARK", (FPFI) NameTableIndexEx, 1, "C"},
};

FoxTable _FoxTable = {

     (FoxTable FAR *) 0, sizeof(myFoxInfo)/sizeof(FoxInfo), myFoxInfo
};

Results

The following results are observed when this code is executed in FoxPro for Windows. These are the expected results:

Test with a variable name that does exist ...

The value returned by FindVar (where = 1) is: 0 The value returned by FindVar (where = -1) is: 1 The value returned by FindVar (where = 0) is: 1

Test with a variable name that does not exist ...

The value returned by FindVar (where = 1) is: 0 The value returned by FindVar (where = -1) is: 0 The value returned by FindVar (where = 0) is: 0

The following results are observed when this code is executed in FoxPro for MS-DOS. The last two lines indicate the problem:

Test with a variable name that does exist ...

The value returned by FindVar (where = 1) is: 0 The value returned by FindVar (where = -1) is: 1 The value returned by FindVar (where = 0) is: 1

Test with a variable name that does not exist ...

The value returned by FindVar (where = 1) is: 0 The value returned by FindVar (where = -1) is: 1 The value returned by FindVar (where = 0) is: 1

Additional reference words: 2.60a FoxDos KBCategory: kbprg kbprb KBSubcategory:

Last Reviewed: January 5, 1995