Solved

How can one get a HFILE?

Posted on 2002-04-17
18
587 Views
Last Modified: 2013-12-03
I know CreateFile() and OpenFile() can create HFILE.

Is there other APIs can create HFILE?
0
Comment
Question by:fengtao2000
  • 4
  • 4
  • 2
  • +4
18 Comments
 
LVL 32

Expert Comment

by:jhance
ID: 6948459
What do you mean?  
0
 
LVL 1

Author Comment

by:fengtao2000
ID: 6948630
I found some programs don't call CreateFile() to get HFILE, but it can call ReadFile() successfully.

So I want to know which APIs can get a HFILE, or do the job same with CreateFile().
0
 
LVL 32

Expert Comment

by:jhance
ID: 6948637
How do you know that they don't call CreateFile() or OpenFile()?
0
 
LVL 1

Author Comment

by:fengtao2000
ID: 6949350
I intercept CreateFileA() and ReadFileA(), and log all the calls.

The app is ansi, so there is no unicode APIs call.

I log many normal CreateFile(), but there are some ReadFile() calls, and the HFILE is not returned by CreateFile(), I don't know where they come from.

There must be another way to do the same job as CreateFile().

Who know about it?
0
 
LVL 12

Expert Comment

by:pjknibbs
ID: 6949976
The applications in question might be using the OpenFile() function--however, I wouldn't suggest using this yourself because it is provided purely for backward compatibility with 16-bit Windows.
0
 
LVL 5

Accepted Solution

by:
robpitt earned 100 total points
ID: 6950380
ReadFile works with some objects that are not files. In particular ReadFile works with handles to consoles, pipes, sockets and mailslots (maybe more).
0
 
LVL 1

Assisted Solution

by:Pavlik
Pavlik earned 100 total points
ID: 6955097
You can use ReadFile with socket handles for example. Or with pipes of any sort. (for example inherited from parent process). Or with handles created by CreateFileMapping().
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 30

Expert Comment

by:Axter
ID: 6958434
>>I found some programs don't call CreateFile() to get
>>HFILE, but it can call ReadFile() successfully.

If the program is using MFC code, then it's probably calling CreateFile indirectly, via CFile or a derive class from CFile.

CFile uses CreateFile, and it has an undocumented HFILE operator.
0
 
LVL 1

Expert Comment

by:Pavlik
ID: 6960275
Axter!!!
What are you talking about?
mfc\src\filecore.cpp (line 194):

HANDLE hFile = ::CreateFile(lpszFileName, dwAccess, dwShareMode, &sa, dwCreateFlag, FILE_ATTRIBUTE_NORMAL, NULL);

And what undocumented you have found here?

I think that's not the case.

0
 
LVL 30

Expert Comment

by:Axter
ID: 6960345
Pavlik,
>>And what undocumented you have found here?

See the following:
C:\PROGRAM FILES\MICROSOFT VISUAL STUDIO\VC98\MFC\Include\AFX.H(1245): operator HFILE() const;

AND

C:\PROGRAM FILES\MICROSOFT VISUAL STUDIO\VC98\MFC\Include\AFX.INL(85):_AFX_INLINE CFile::operator HFILE() const

You'll notice that on the second line of source code, that the operator returns m_hFile class member.

This data member, happens to be given the value that is returned from CreateFile.

See the end of CFile::Open member function, and you'll see the following:
m_hFile = (HFILE)hFile;
0
 
LVL 30

Expert Comment

by:Axter
ID: 6960438
Pavlik,
>>And what undocumented you have found here?
The CFile HFILE operator member function, is not documented in MSDN, or any other MS documents that I've come accross.
0
 
LVL 1

Expert Comment

by:Pavlik
ID: 6960538
And i don't understand what's wrong with it.
The type of hFile is HANDLE and therefore CFile::operator HFILE() will NEVER be called in the line like this:
m_hFile = (HFILE)hFile;

It is just conversion from (void*) to (int).

And even if something mystical was done with that handle. Who cares? Since initially this handle was obtained from CreateFile().

Don't think that the only thing that Microsoft does is making undocumented holes. Believe me, it's much harder to keep track of such holes than not to make and not to use them.

And this operator is very usual technique in the implementations of thin object oriented wrappers around non object oriented API. It allows to use object of CFile in the places, where HFILE is required.
0
 
LVL 30

Expert Comment

by:Axter
ID: 6960594
>>and therefore CFile::operator HFILE() will NEVER be
>>called in the line like this:
>>m_hFile = (HFILE)hFile;

I never said it would.  I think you misunderstood my comments.

That line of code comes from the CFile::Open function.

The point of me posting that is that you said CFile doesn't use HFILE.  But it does.

The CFile::operator HFILE() function returns m_hFile.

m_hFile is given the value of hFile in the CFile::Open function.

hfile is initated to the value returned from CreateFile.

Now hfile variable is of HANDLE type.
And m_hFile variable is of UNIT type.
And of couse the operator HFILE() member function is returning an HFILE type.

So the MS's code casted a HANDLE type to an UNIT, then via the operator HFILE() member function, it's casting a UNIT type to an HFILE type.

>>Don't think that the only thing that Microsoft does is
>>making undocumented holes. Believe me, it's much
>>harder to keep track of such holes than not to make and
>>not to use them.

I'm not sure what your point is in all this, and what it has to do with the question, or the comments I posted.
 
The questioner asked specifically about CreateFile(), OpenFile(), and HFILE.

My original commented pointed out the MFC code uses these functions, and it has the HFILE operator in CFile class.

So even if you have an MFC application that is not directly calling CreateFile() and OpenFile(), it still would be called if CFile is used.
Furthermore, an HFILE type could be crated adn passed around via CFile since CFile has an HFILE operator.
0
 
LVL 1

Expert Comment

by:Pavlik
ID: 6960620
Oops.

Sorry Axter.
Looks like i didn't understand your very first comment.
As i thought, fengtao2000 hooks all the calls to CreateFile/ReadFile, not reads through some source code. My point is that if he logs actual calls to CreateFile he won't miss this call for MFC based app.
0
 
LVL 8

Expert Comment

by:Answers2000
ID: 6965598
_lcreat
_lopen
APIs also return HFILE
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

This article shows how to make a Windows 7 gadget that accepts files dropped from the Windows Explorer.  It also illustrates how to give your gadget a non-rectangular shape and how to add some nifty visual effects to text displayed in a your gadget.…
What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now