VS2010 fwrite过程

本文详细解析了fwrite过程的汇编指令与c++源码实现,包括参数检查、文件句柄处理、错误处理及锁文件操作等关键步骤。通过深入分析,读者可以更全面地理解fwrite函数的工作原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

fwrite过程

调用fwrite过程:

8BF4                mov         esi,esp 

8B 45F8             mov         eax,dword ptr [fp] 

50                   push        eax 

6A04                push        4 

6A01                push        1 

68 40 5522 61       push        offset string "ddddd"(61225540h) 

FF 15 9082 22 61    call        dword ptr [__imp__fwrite(61228290h)] 

83 C410             add         esp,10h 

3BF4                cmp         esi,esp 

E8 A6 FBFF FF       call        @ILT+310(__RTC_CheckEsp)(6122113Bh) 

89 45E0             mov         dword ptr [wcont],eax 

跟入_imp_fwrite的c++源码

{

size_t>检查size和cout是否为0

if(size ==0>

   >检查文件句柄是否为空

_VALIDATE_RETURN( (stream != NULL) ,EINVAL, 0);

获得文件句柄

_lock_str(stream);                    

__try {

真正的写操作

   >

     }

__finally {

释放文件句柄

    _unlock_str(stream);            /* unlock stream */

    }

返回实际写入的个数

return>

调用的汇编指令为:

5AF15E35 33C0                xor         eax,eax 

5AF15E37 83 7D 1400          cmp         dword ptr [ebp+14h](文件句柄),0 

5AF15E3B 0F 95C0             setne       al 

5AF15E3E 89 45E0             mov         dword ptr [_Expr_val],eax 

5AF15E41 83 7D E000          cmp         dword ptr [_Expr_val],0 

5AF15E45 751E                jne         fwrite+75h (5AF15E65h) 

若文件句柄为空,则执行下面,发送调试报告。不为空则跳转。

5AF15E47 68 9C 58EB 5A       push        offset string L"(stream !=NULL)" (5AEB589Ch) 

5AF15E4C 6A00                push        0 

5AF15E4E 6A4D                push        4Dh 

5AF15E50 68 F0 6DEB 5A       push        offset stringL"f:\\dd\\vctools\\crt_bl"... (5AEB6DF0h) 

5AF15E55 6A02                push        2 

5AF15E57 E8 94 E70A 00       call        _CrtDbgReportW (5AFC45F0h) 

5AF15E5C 83 C414             add         esp,14h 

5AF15E5F 83 F801             cmp         eax,1 

5AF15E62 7501                jne         fwrite+75h (5AF15E65h) 

5AF15E64 CC                   int         3 

5AF15E65 83 7D E000          cmp         dword ptr [_Expr_val],0 

5AF15E69 752A                jne         fwrite+0A5h (5AF15E95h) 

下面也是错误处理。文件句柄不为空继续跳。

5AF15E6B E8 20 C5FE FF       call        _errno (5AF02390h) 

5AF15E70 C7 00 1600 00 00    mov         dword ptr [eax],16h 

5AF15E76 6A00                push        0 

5AF15E78 6A4D                push        4Dh 

5AF15E7A 68 F0 6DEB 5A       push        offset stringL"f:\\dd\\vctools\\crt_bl"... (5AEB6DF0h) 

5AF15E7F 68 DC 6DEB 5A       push        offset string L"fwrite" (5AEB6DDCh) 

5AF15E84 68 9C 58EB 5A       push        offset string L"(stream !=NULL)" (5AEB589Ch) 

5AF15E89 E8 32 F80A 00       call        _invalid_parameter (5AFC56C0h) 

5AF15E8E 83 C414             add         esp,14h 

5AF15E91 33C0                xor        eax,eax 

5AF15E93 EB4C                jmp         $LN12+3 (5AF15EE1h) 

上面的_lock_str(stream)过程

5AF15E95 8B 5514             mov         edx,dword ptr [ebp+14h] 

5AF15E98 52                   push        edx 

5AF15E99 E8 32 B7FF FF       call        _lock_file (5AF115D0h) 

5AF15E9E 83 C404             add         esp,4

就是一文件句柄指针调用_lock_file。下面看以下_lock_file过程。

_lock_file过程

功能如名字,锁住一个文件。

void __cdecl_lock_file (

        FILE *pf

        )

{

        /*

         这里检查文件句柄是否在_iob(stdio控制结构数组)范围内。觉得锁的行为是哪种。

         */

        if ((pf >= _iob) && (pf <= (&_iob[_IOB_ENTRIES-1])) )

        {

            /*

             * FILE lies in _iob[] so the locklies in _locktable[].

             */

            _lock( _STREAM_LOCKS + (int)(pf - _iob) );

            /* We set_IOLOCKED to indicate we locked the stream */

            pf->_flag |= _IOLOCKED;

        }

        else

            /*

             * Not part of _iob[]. Therefore,*pf is a _FILEX and the

             * lock field of the struct is aninitialized critical

             * section.

             */

            EnterCriticalSection(&(((_FILEX *)pf)->lock) );

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值