Thread: CreateFile function

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    19

    CreateFile function

    szFileName contains calc.exe

    hFile = CreateFile(szFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
    OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

    dwFileSize = GetFileSize(hFile, NULL);
    printf("%d",GetLastError());


    error code which i am getting is 183....which says "Cannot create a file when that file already exists"GetFIleSize function is failing....
    can i use CreateFile with first argument as cal.exe....??

  2. #2
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    If you don't want to create a new file, maybe try OPEN_EXISTING instead of OPEN_ALWAYS.
    OPEN_ALWAYS makes a new file if the file does not exist. OPEN_EXISTING fails, if the file does not exist. I am not sure it helps, but try it out.

  3. #3
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    EDIT:

    Ohh, I see you beat me to it...

    EDIT:

    Quote Originally Posted by max
    I am not sure it helps, but try it out.
    Why wouldn't it?
    If it doesn't, then his system has big problems.
    Last edited by Queatrix; 09-11-2006 at 01:40 PM.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    19
    OPEN_EXISTING -> opens a file if it exists ...
    otherwise CreateFile will fail ..


    OPEN_ALWAYS -> opens a file if it exists
    otherwise new file will be created...


    i tried OPEN_EXISTING also ....but again GetFileSize returns 0.

    calc.exe exists ......

  5. #5
    Madly in anger with you
    Join Date
    Nov 2005
    Posts
    211
    could you please post your declaration of the path to calc.exe?

    also try checking the value of hFile against INVALID_HANDLE_VALUE. if the call to CreateFile is failing, then that is your problem, and it could be failing due to an incorrect path to calc.exe, because otherwise everything looks ok as long as you are using OPEN_EXISTING (and are not using OPEN_ALWAYS together with it).
    Last edited by Bleech; 09-11-2006 at 11:12 PM.

    Intel Core 2 Quad Q6600 @ 2.40 GHz
    3072 MB PC2-5300 DDR2
    2 x 320 GB SATA (640 GB)
    NVIDIA GeForce 8400GS 256 MB PCI-E

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    19
    thanx
    path to calc.exe was incorrect

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM