Thread: HANDLE and FILE*

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    HANDLE and FILE*

    Hello everyone,


    I am using Windows API open (create) a new file by CreateFile,

    http://msdn2.microsoft.com/en-us/library/aa363858.aspx

    the return value is HANDLE, if I use fwrite to write content to the file, since fwrite requires FILE* as parameter. I think HANDLE is not the same as FILE*, right?

    Are there any ways (or methods) to transform HANDLE to FILE*?

    I have tried that passing HANDLE directly to fwrite (forcely convert from HANDLE from FILE*) will cause access violation exception. I think the reason is HANDLE is not the same as FILE*, is that correct?


    thanks in advance,
    George
    Last edited by George2; 07-26-2007 at 03:16 AM.

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    No, a HANDLE is not the same as a FILE pointer.
    If you want to write to it, why not use WriteFile()?
    If you want to use fwrite(), then open the file with fopen().

    The windows handles are simply generic resource 'objects/handles'.

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by George2 View Post
    Are there any ways (or methods) to transform HANDLE to FILE*?
    _open_osfhandle(), the Windows moral equivalent of fdopen() on POSIX:

    http://msdn2.microsoft.com/en-us/lib...9x(VS.80).aspx

    There's always a way.

    EDIT: Note the special requirement that the resulting FILE has to be closed with _close(), not fclose().

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with setting text into LTEXT using Window Handle
    By jasperleeabc in forum Windows Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Getting other processes class names
    By Hawkin in forum Windows Programming
    Replies: 3
    Last Post: 03-20-2008, 04:02 PM
  3. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  4. Button handler
    By Nephiroth in forum Windows Programming
    Replies: 8
    Last Post: 03-12-2006, 06:23 AM
  5. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM