解決exe和DLL直接傳遞FILE 指針崩潰的問題


EXE程序和DLL之間可能傳遞FILE指針,但是可能會造成程序崩潰。這是由於_lock_file引起的

  1. void __cdecl _lock_file (  
  2.         FILE *pf  
  3.         )  
  4. {  
  5.         /* 
  6.          * The way the FILE (pointed to by pf) is locked depends on whether 
  7.          * it is part of _iob[] or not 
  8.          */  
  9.         if ( (pf >= _iob) && (pf <= (&_iob[_IOB_ENTRIES-1])) )  
  10.         {  
  11.             /* 
  12.              * FILE lies in _iob[] so the lock lies in _locktable[]. 
  13.              */  
  14.             _lock( _STREAM_LOCKS + (int)(pf - _iob) );  
  15.             /* We set _IOLOCKED to indicate we locked the stream */  
  16.             pf->_flag |= _IOLOCKED;  
  17.         }  
  18.         else  
  19.             /* 
  20.              * Not part of _iob[]. Therefore, *pf is a _FILEX and the 
  21.              * lock field of the struct is an initialized critical 
  22.              * section. 
  23.              */  
  24.             EnterCriticalSection( &(((_FILEX *)pf)->lock) );  
  25. }  

if ( (pf >= _iob) && (pf <= (&_iob[_IOB_ENTRIES-1])) ) 把fp和一個全局變量_iob比較,exe和DLL可能會有不同的全局變量,這導致fp不在_iob數組的範圍內,導致出現錯誤。

解決的頒發是exe和DLL都動態連接到CRT,或者把DLL編譯成靜態庫。

 

轉自:http://blog.csdn.net/leechiyang/article/details/6873952

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章