------------------------------------------------------------------------------------------
                      [BUG/PRB.] VFP 9.0 FIX - THE NUMBER OF TIMES TO TRY AGAIN AFTER OPEN FILE FAILED
                                 January 2024
                 ------------------------------------------------------------------------------------------
                                     CCB



1. BUG:

     In vfp9 (and vfp6, vfp7, vfp8), somtimes the open file command runs failed on Windows Vista or later.


2. CAUSE:

     There are some compatible problems when the open file command calls the CreateFileA Windows API
     for the Windows Defender program or the antivirus programs.
     Now if the open file command calls the CreateFileA Windows API failed,
     it will call the CreateFileA Windows API many times again.

     There are some BUGs in the following code.


3. RESOLUTION:

     We can write some code to fix the BUG.

     Fun42cf7b :: ; proc near
             sub esp , 010Ch                                                 ;0x0042cf7b :        81ec0c010000
             mov eax ,  dword ptr [ Data937090 ]                             ;0x0042cf81 :        a190709300
             push ebp                                                        ;0x0042cf86 :        55
             push esi                                                        ;0x0042cf87 :        56
             push edi                                                        ;0x0042cf88 :        57
             mov  edi , SetErrorMode                           ;0x0042cf89 :        8b3d04719100
             pushd 08001h                                                    ;0x0042cf8f :        6801800000
             mov  dword ptr [ esp + 0118h ] , eax                            ;0x0042cf94 :        89842418010000
             call edi                                                        ;0x0042cf9b :        ffd7
             mov  esi , dword ptr [ esp + 011Ch ]                            ;0x0042cf9d :        8bb4241c010000
             mov ebp , eax                                                   ;0x0042cfa4 :        8be8
             lea eax ,  dword ptr [ esp + 12 ]                               ;0x0042cfa6 :        8d44240c
             mov ecx , eax                                                   ;0x0042cfaa :        8bc8
             cmp ecx , esi                                                   ;0x0042cfac :        3bce
             je Label598206                                                  ;0x0042cfae :        0f8452b21600
             lea ecx ,  dword ptr [ esp + 12 ]                               ;0x0042cfb4 :        8d4c240c
             mov edx , 0105h                                                 ;0x0042cfb8 :        ba05010000
             sub esi , ecx                                                   ;0x0042cfbd :        2bf1
             lea ebx ,  dword ptr [ebx]                                      ;0x0042cfbf :        8d1b

     Label42cfc1 ::
             mov  cl , byte ptr [ esi + eax ]                                ;0x0042cfc1 :        8a0c06
             dec edx                                                         ;0x0042cfc4 :        4a
             test cl , cl                                                    ;0x0042cfc5 :        84c9
             je Label42cef5                                                  ;0x0042cfc7 :        0f8428ffffff
             mov  byte ptr [eax] , cl                                        ;0x0042cfcd :        8808
             inc eax                                                         ;0x0042cfcf :        40
             test edx , edx                                                  ;0x0042cfd0 :        85d2
             jbe Label42cef5                                                 ;0x0042cfd2 :        0f861dffffff
             jmp Label42cfc1                                                 ;0x0042cfd8 :        ebe7

     Label42cef5 ::
             mov  byte ptr [eax] , 00h                                       ;0x0042cef5 :        c60000

     Label42cef8 ::
             mov  edx , dword ptr [ esp + 0120h ]                            ;0x0042cef8 :        8b942420010000
             mov  eax , dword ptr [edx]                                      ;0x0042ceff :        8b02
             test  byte ptr ah , 02h                                         ;0x0042cf01 :        f6c402
             jne Label43d8d9                                                 ;0x0042cf04 :        0f85cf090100
             mov cl , al                                                     ;0x0042cf0a :        8ac8
             and cl , 04h                                                    ;0x0042cf0c :        80e104
             neg cl                                                          ;0x0042cf0f :        f6d9
             sbb ecx , ecx                                                   ;0x0042cf11 :        1bc9
             and ecx , 0FFFFFFFDh                                            ;0x0042cf13 :        83e1fd
             add ecx , 03h                                                   ;0x0042cf16 :        83c103

     Label42cf19 ::
             test al , 01h                                                   ;0x0042cf19 :        a801
             je Label43480d                                                  ;0x0042cf1b :        0f84ec780000
             mov eax , 080000000h                                            ;0x0042cf21 :        b800000080

     Label42cf26 ::

     
     ;
     ;                 -------------------------------------------------------------------------------
     ;                      VFP 9.0 FIX - THE NUMBER OF TIMES TO TRY AGAIN AFTER OPEN FILE FAILED
     ;                                 January 2020
     ;                 -------------------------------------------------------------------------------
     ;                                     CCB
     ;
     ; The number of times to try again after open file failed.
     ;
     ; 2020/1/29, by ccb
     ;

     ;         pushd 00h                                                       ;0x0042cf26 :        6a00
     ;         pushd 080h                                                      ;0x0042cf28 :        6880000000
     ;         pushd 03h                                                       ;0x0042cf2d :        6a03
     ;         pushd 00h                                                       ;0x0042cf2f :        6a00
     ;         push ecx                                                        ;0x0042cf31 :        51
     ;         push eax                                                        ;0x0042cf32 :        50
     ;         lea edx ,  dword ptr [ esp + 36 ]                               ;0x0042cf33 :        8d542424
     ;         push edx                                                        ;0x0042cf37 :        52
     ;         call  CreateFileA                                 ;0x0042cf38 :        ff15f8719100

             mov dword ptr vfpa_filetostr_createfile_cnt,00h
             mov dword ptr vfpa_filetostr_createfile_access,eax
             mov dword ptr vfpa_filetostr_createfile_sharemode,ecx
             mov dword ptr vfpa_filetostr_createfile_flags,80h

     Label42cf26_isopenfile_start ::
             cmp dword ptr vfpa_sys9029_data,00h
             je Label42cf26_isopenfile_end
             jmp Label42cf27_isopenfile_createfile
     Label42cf26_isopenfile_end ::
             jmp Label42cf27

     Label42cf27_isopenfile_createfile ::
             pushd 00h
             push dword ptr vfpa_filetostr_createfile_flags
             pushd 03h
             pushd 00h
             push dword ptr vfpa_filetostr_createfile_sharemode
             push dword ptr vfpa_filetostr_createfile_access
             lea edx ,  dword ptr [ esp + 36 ]
             push edx
             call  CreateFileA
             cmp eax , 0FFFFFFFFh
             jne Label42cf28

             call GetLastError
             mov ecx , eax
             mov eax , 0FFFFFFFFh
             cmp ecx , 05h
             je Label42cf27_isopenfile_createfile_attribute
             cmp ecx , 20h
             je Label42cf27_isopenfile_createfile_attribute
             cmp ecx , 02h
             je Label42cf28
             cmp ecx , 03h
             je Label42cf28
             cmp ecx , 0Fh
             je Label42cf28
             cmp ecx , 07Bh
             je Label42cf28
             jmp Label42cf27_isopenfile_createfile_attribute

     Label42cf27_isopenfile_createfile_attribute ::
             lea edx ,  dword ptr [ esp + 12 ]
             push edx
             call GetFileAttributesA
             mov ecx , eax
             mov eax , 0FFFFFFFFh
             cmp ecx , 0FFFFFFFFh
             je Label42cf27_isopenfile_createfile_cnt
             test ecx , 10h
             jne Label42cf28
             test dword ptr vfpa_filetostr_createfile_access , 40000000h
             je Label42cf27_isopenfile_createfile_cnt
             test ecx , 01h
             jne Label42cf28
             jmp Label42cf27_isopenfile_createfile_cnt

     Label42cf27_isopenfile_createfile_cnt ::
             lea edx ,  dword ptr [ esp + 12 ]
             push edx
             call lstrlenA
             lea edx ,  dword ptr [ esp + 12 ]
             add edx , eax
             dec edx
             mov eax , 0FFFFFFFFh
             cmp byte ptr [edx],"\"
             je Label42cf28
             mov ecx,dword ptr vfpa_sys9029_data
             inc dword ptr vfpa_filetostr_createfile_cnt
             cmp dword ptr vfpa_filetostr_createfile_cnt,ecx
             ja Label42cf28
             push dword ptr vfpa_sys9132_data
             call  Sleep
             jmp Label42cf27_isopenfile_createfile

     Label42cf27 ::
             pushd 00h
             push dword ptr vfpa_filetostr_createfile_flags
             pushd 03h
             pushd 00h
             push dword ptr vfpa_filetostr_createfile_sharemode
             push dword ptr vfpa_filetostr_createfile_access
             lea edx ,  dword ptr [ esp + 36 ]
             push edx
             call  CreateFileA
             cmp eax , 0FFFFFFFFh
             jne Label42cf28

             call GetLastError
             mov ecx , eax
             mov eax , 0FFFFFFFFh
             cmp ecx , 05h
             je Label42cf27_createfile_attribute
             cmp ecx , 20h
             je Label42cf27_createfile_attribute
             cmp ecx , 02h
             je Label42cf28
             cmp ecx , 03h
             je Label42cf28
             cmp ecx , 0Fh
             je Label42cf28
             cmp ecx , 07Bh
             je Label42cf28
             jmp Label42cf27_createfile_attribute

     Label42cf27_createfile_attribute ::
             lea edx ,  dword ptr [ esp + 12 ]
             push edx
             call GetFileAttributesA
             mov ecx , eax
             mov eax , 0FFFFFFFFh
             cmp ecx , 0FFFFFFFFh
             je Label42cf27_createfile_cnt
             test ecx , 10h
             jne Label42cf28
             test dword ptr vfpa_filetostr_createfile_access , 40000000h
             je Label42cf27_createfile_cnt
             test ecx , 01h
             jne Label42cf28
             jmp Label42cf27_createfile_cnt

     Label42cf27_createfile_cnt ::
             lea edx ,  dword ptr [ esp + 12 ]
             push edx
             call lstrlenA
             lea edx ,  dword ptr [ esp + 12 ]
             add edx , eax
             dec edx
             mov eax , 0FFFFFFFFh
             cmp byte ptr [edx],"\"
             je Label42cf28
             inc dword ptr vfpa_filetostr_createfile_cnt
             cmp dword ptr vfpa_filetostr_createfile_cnt,03h
             ja Label42cf28
             push dword ptr vfpa_sys9074_data
             call  Sleep
             jmp Label42cf27

     Label42cf28 ::
     ;         mov dword ptr vfpa_filetostr_isfiletostr,00h
     ;         mov dword ptr vfpa_filetostr_isfiletostr2,00h
     ;         mov dword ptr vfpa_filetostr_isfiletostr3,00h
     ;         mov dword ptr vfpa_strtofile_isstrtofile,00h
     ;         mov dword ptr vfpa_strtofile_isstrtofile2,00h
     ;         mov dword ptr vfpa_strtofile_isstrtofile3,00h
     ;         mov dword ptr vfpa_buildexe_isbuildexe,00h
     ;         mov dword ptr vfpa_buildexe_isbuildexe2,00h
     ;         mov dword ptr vfpa_buildexe_isbuildexe3,00h

     
             push ebp                                                        ;0x0042cf3e :        55
             mov  dword ptr [ebx] , eax                                      ;0x0042cf3f :        8903
             call edi                                                        ;0x0042cf41 :        ffd7
             cmp  dword ptr [ebx] , 0FFFFFFFFh                               ;0x0042cf43 :        833bff
             je Label43d3c1                                                  ;0x0042cf46 :        0f8475040100
             mov  edi , dword ptr [ esp + 0120h ]                            ;0x0042cf4c :        8bbc2420010000
             mov  ecx , dword ptr [ esp + 011Ch ]                            ;0x0042cf53 :        8b8c241c010000
             xor esi , esi                                                   ;0x0042cf5a :        33f6
             call Fun43195e                                                  ;0x0042cf5c :        e8fd490000

     Label42cf61 ::
             mov  ecx , dword ptr [ esp + 0114h ]                            ;0x0042cf61 :        8b8c2414010000
             pop edi                                                         ;0x0042cf68 :        5f
             mov eax , esi                                                   ;0x0042cf69 :        8bc6
             pop esi                                                         ;0x0042cf6b :        5e
             pop ebp                                                         ;0x0042cf6c :        5d
             call Fun42bf1d                                                  ;0x0042cf6d :        e8abefffff
             add esp , 010Ch                                                 ;0x0042cf72 :        81c40c010000
             ret 08h                                                         ;0x0042cf78 :        c20800


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
     http://www.baiyujia.com/vfpdocuments/f_vfp9fix107.asp
     http://www.baiyujia.com/vfpdocuments/f_vfp9fix65.asp
     http://www.baiyujia.com/vfpdocuments/f_vfp9fix66.asp
     http://www.baiyujia.com/vfpdocuments/f_vfp9fix67.asp
     http://www.baiyujia.com/vfpdocuments/f_vfp9fix68.asp
     http://www.baiyujia.com/vfpdocuments/f_vfp9fix69.asp
     http://www.baiyujia.com/vfpdocuments/f_vfp9fix70.asp
     http://www.baiyujia.com/vfpdocuments/f_vfp9fix71.asp
     http://www.baiyujia.com/vfpdocuments/f_vfp9fix72.asp
     http://www.baiyujia.com/vfpdocuments/f_vfp9fix73.asp
     http://www.baiyujia.com/vfpdocuments/f_vfp9fix74.asp
     http://www.baiyujia.com/vfpdocuments/f_vfp9fix84.asp
     http://www.baiyujia.com/vfpdocuments/f_vfp9fix85.asp
     http://www.baiyujia.com/vfpdocuments/f_vfp9fix109.asp
     http://www.baiyujia.com/vfpdocuments/f_vfp9fix110.asp
     http://www.baiyujia.com/vfpdocuments/f_vfp9fix143.asp
     http://www.baiyujia.com/vfpdocuments/f_vfp9fix144.asp
     http://www.baiyujia.com/vfpdocuments/f_vfp9fix145.asp
     http://www.baiyujia.com/vfpdocuments/f_vfp9fix146.asp
     http://www.baiyujia.com/vfpdocuments/f_vfp9fix147.asp
     http://www.baiyujia.com/vfpdocuments/f_vfp9fix148.asp
     http://www.baiyujia.com/vfpdocuments/f_vfp9fix149.asp
     http://www.baiyujia.com/vfpdocuments/f_vfp9fix150.asp
     http://www.baiyujia.com/vfpdocuments/f_vfp9fix203.asp
     http://www.baiyujia.com/vfpdocuments/f_vfp9fix261.asp
     http://www.baiyujia.com/vfpdocuments/f_vfp9fix262.asp
     http://www.baiyujia.com/vfpdocuments/f_vfp9fix269.asp
     http://www.baiyujia.com/vfpdocuments/f_vfp9fix270.asp
     http://www.baiyujia.com/vfpdocuments/f_vfp9fix278.asp
     http://www.baiyujia.com/vfpdocuments/f_vfp9fix279.asp
     http://www.baiyujia.com/vfpdocuments/f_vfp9fix348.asp
     http://www.baiyujia.com/vfpdocuments/f_vfp9fix349.asp
     http://www.baiyujia.com/vfpdocuments/f_vfp9fix350.asp
     http://www.baiyujia.com/vfpdocuments/f_vfp9fix351.asp

     2, microsoft.com:
     https://devblogs.microsoft.com/oldnewthing/20120907-00/?p=6663

     3, foxite.com:
     https://www.foxite.com/archives/error-111-after-zap-0000493590.htm
     https://www.foxite.com/archives/intermittent-error-111-0000406044.htm


6. OTHER:

     For reference only, there is no guarantees.

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