Microsoft KB Archive/132290

= PRB: Problems Using CFile::m_hFile When Porting MFC Apps =

Article ID: 132290

Article Last Modified on 11/21/2006

-

APPLIES TO

 Microsoft Foundation Class Library 4.2, when used with:  Microsoft Visual C++ 1.0 Professional Edition

 Microsoft Visual C++ 1.5 Professional Edition

 Microsoft Visual C++ 1.51

 Microsoft Visual C++ 1.52 Professional Edition</li></ul>

 Microsoft Visual C++ 1.0 Professional Edition</li></ul>

 Microsoft Visual C++ 2.0 Professional Edition</li></ul>

 Microsoft Visual C++ 2.1</li></ul>

 Microsoft Visual C++ 2.2</li></ul>

 Microsoft Visual C++ 4.0 Standard Edition</li></ul> </li></ul>

-

<div class="notice_section">

This article was previously published under Q132290

<div class="symptoms_section">

SYMPTOMS
When you port MFC applications that use CFile from Windows version 3.1 to Windows NT or Windows 95, you may encounter problems related to use of the m_hFile member variable.

<div class="cause_section">

CAUSE
In all versions of MFC, CFile:: m_hFile is defined as a UINT. In the 16-bit versions of MFC (versions 2.0 and 2.5x), m_hFile is a HANDLE that is created from a call to the C Runtime Library (CRT) function _dos_open. In this case, other CRT functions that use the handle, such as _eof, work correctly.

In the 32-bit versions of MFC (versions 2.1 and 3.x), m_hFile is a handle that is created from a call to the Win32 function CreateFile, which is incompatible with the C Runtime functions. In this case, you need to use the Win32 API function set.

<div class="resolution_section">

RESOLUTION
If your MFC application must use functions outside of the CFile class to obtain the functionality you need, note these differences and use compatible functions.

To obtain a C run-time file handle and set it to point to the operating system file handle m_hFile, you can use the _open_osfhandle function. The handle returned by that function is compatible with _eof and other C Runtime Library functions.

<div class="references_section">