DOCUMENT:Q122357  03-NOV-1994  [FOXPRO]
TITLE   :PRB: Unexpected Results from _FindVar() in FoxPro for MS-DOS
PRODUCT :Microsoft FoxPro
PROD/VER:2.60a
OPER/SYS:MS-DOS
KEYWORDS:kbprg kbprb

---------------------------------------------------------------------
The information in this article applies to:

 - Microsoft FoxPro for MS-DOS, version 2.6a
 - Microsoft FoxPro for MS-DOS Library Construction Kit, version 2.6a
---------------------------------------------------------------------

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     : INF: 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:

=============================================================================

THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS
PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND.  MICROSOFT DISCLAIMS
ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.  IN NO
EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR
ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL,
CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF
MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGES.  SOME STATES DO NOT ALLOW THE EXCLUSION
OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES
SO THE FOREGOING LIMITATION MAY NOT APPLY.

Copyright Microsoft Corporation 1994.