------------------------------------------------------------
                      [BUG/PRB.] VFP 9.0 FIX - FILE HANDLE NUMBER ZERO
                                 January 2024
                 ------------------------------------------------------------
                                    CCB



1. BUG:

     In vfp9, sometimes the function FOPEN() will return file handle number zero.

     There is some message from Mr. Ken:

     In both VFP 8 and VFP 9, it is possible that the function
     FOPEN(filename, 1)
     will return handle number zero.  When this happens, the function
     FSEEK(handle, 0, 2)
     appears to not work -- it always returns zero.

     There is a program for test:

     *PROC testfilehandlenumberzero

     SET STEP OFF
     SET ECHO OFF
     SET DEBUG OFF
     SET ESCAPE OFF
     SET TALK OFF
     SET SAFETY OFF

     _SCREEN.VISIBLE=.T.
     _SCREEN.WINDOWSTATE=2

     SET EXCLUSIVE OFF
     SET MULTILOCK ON
     SET COLLATE TO "MACHINE"

     CLOSE DATABASES ALL
     CLOSE TABLES ALL

     =STRTOFILE("123456789","testfilehandlenumberzero.txt")

     * close the task manager file (foxtask.dbf).
     _FOXTASK=""

     PRIVATE m.q_hand
     m.q_hand=FOPEN("testfilehandlenumberzero.txt",0)
     ?"filehandlenumber: "+LTRIM(STR(m.q_hand,16))
     WAIT

     PRIVATE m.q_seek
     m.q_seek=FSEEK(m.q_hand,5,0)
     ?"FSEEK(m.q_hand,5,0): "+LTRIM(STR(m.q_seek,16))
     IF m.q_seek=5
        ?"FSEEK() OK."
     ELSE
        ?"FSEEK() ERROR!"
     ENDI
     WAIT

     ?"FREAD(m.q_hand,1): "+FREAD(m.q_hand,1)
     ?"FREAD(m.q_hand,1): "+FREAD(m.q_hand,1)
     ?"FREAD(m.q_hand,1): "+FREAD(m.q_hand,1)
     WAIT

     PRIVATE m.q_seek2
     m.q_seek2=FSEEK(m.q_hand,0,1)
     ?"FSEEK(m.q_hand,0,1): "+LTRIM(STR(m.q_seek2,16))
     IF m.q_seek2=8
        ?"FSEEK() OK."
     ELSE
        ?"FSEEK() ERROR!"
     ENDI
     WAIT

     =FCLOSE(m.q_hand)

     CLOSE DATABASES ALL
     CLOSE TABLES ALL

     RETURN

     * END OF PROC TESTFILEHANDLENUMBERZERO.

     We can create a configuration file config.fpw:
     RESOURCE=OFF
     that is vfp will not open the resource file (foxuser.dbf).

     then we can start vfp9:
     VFP9.EXE -Cconfig.fpw

     then we can run the following statement in the Command window:
     DO testfilehandlenumberzero.prg
     Usually, the function FSEEK() will return zero.


2. CAUSE:

     There are some BUGs in the following code.


3. RESOLUTION:

     We can write some code to fix the BUG.

     Label8e63f4 ::
             test edi , edi                                                  ;0x008e63f4 :        85ff


     ;
     ;                 -------------------------------------------------
     ;                      VFP 9.0 FIX - FILE HANDLE NUMBER ZERO
     ;                                  August 2015
     ;                 -------------------------------------------------
     ;                                     CCB
     ;
     ; For the file handle number 0, sometimes VFP will process it same as the file handle number -1.
     ;
     ; 2015/8/12, by ccb
     ;

     ;         jle Label8e640c                                                 ;0x008e63f6 :        7e14
             jl Label8e640c                                                 ;0x008e63f6 :        7e14


             mov eax ,  dword ptr [ Data93757c ]                             ;0x008e63f8 :        a17c759300
             imul edi , edi , 038h                                           ;0x008e63fd :        6bff38
             mov  ecx , dword ptr [ edi + eax + 4 ]                          ;0x008e6400 :        8b4c0704
             mov  eax , dword ptr [ ebp - 12 ]                               ;0x008e6404 :        8b45f4
             mov  dword ptr [ eax + 12 ] , ecx                               ;0x008e6407 :        89480c
             jmp Label8e6413                                                 ;0x008e640a :        eb07

     Label8e640c ::
             mov  eax , dword ptr [ ebp - 12 ]                               ;0x008e640c :        8b45f4
             and  dword ptr [ eax + 12 ] , 00h                               ;0x008e640f :        83600c00

     Label8e6413 ::
             mov  byte ptr [eax] , 049h                                      ;0x008e6413 :        c60049
             mov  dword ptr [ eax + 4 ] , 0Ah                                ;0x008e6416 :        c740040a000000

     Label8e641d ::
             pop edi                                                         ;0x008e641d :        5f
             pop esi                                                         ;0x008e641e :        5e
             pop ebx                                                         ;0x008e641f :        5b
             leave                                                           ;0x008e6420 :        c9
             ret                                                             ;0x008e6421 :        c3

     Now in VFP Advanced, the function FSEEK() can run fine even the file handle number is zero.


4. APPLIES TO:

     VFP 6.0.8167.0
     VFP 6.0.8961.0 (SP5)

     VFP 7.0.0.9262
     VFP 7.0.0.9465 (SP1)

     VFP 8.0.0.2521
     VFP 8.0.0.3117 (SP1)

     VFP 9.0.0.2412
     VFP 9.0.0.3504 (SP1)
     VFP 9.0.0.4611 (SP2)
     VFP 9.0.0.5015 (SP2)
     VFP 9.0.0.5411 (SP2)
     VFP 9.0.0.5721 (SP2)
     VFP 9.0.0.5815 (SP2)
     VFP 9.0.0.6303 (SP2)
     VFP 9.0.0.6602 (SP2)
     VFP 9.0.0.7423 (SP2)

     The bug has been fixed in VFP Advanced.


5. REFERENCE WEBSITES:

     1, baiyujia.com:
     http://www.baiyujia.com

     2, microsoft.com:
     https://social.msdn.microsoft.com/Forums/en-US/cf6429ff-eb67-436e-ad39-441a60b62033/problems-with-fseek

     3, tek-tips.com:
     http://www.tek-tips.com/viewthread.cfm?qid=292708


6. OTHER:

     For reference only, there is no guarantees.

     Any questions or suggestions, please send me an email at ccb2000@163.com.