Thread: CreateFile problems

  1. #1
    Registered User surgeon's Avatar
    Join Date
    Jan 2015
    Posts
    26

    CreateFile problems

    Hi, CreateFile always return INVALID_HANDLE_VALUE. I run program into MyDocuments directory, and on C drive. What am i doing wrong?
    Code:
    printf("Enter path:\n");
        
        scanf("%s", &szPath);
    
    
        HANDLE hFile = CreateFile(szPath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ |
            FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
    
    
        if (hFile == INVALID_HANDLE_VALUE)
        {
            printf("Error hFile!\n");
            std::cout << GetLastError();
            return 0;
        }
    Output:
    Error hFile!
    5

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    What is the exact path you are entering? (If there are any spaces in the path, your "scanf()" will not be sufficient.)

    Is the file supposed to already exist?

    Code:
    OPEN_EXISTING
    Opens a file or device, only if it exists.
    If the specified file or device does not exist, the function fails and the last-error code is set to ERROR_FILE_NOT_FOUND (2).
    https://msdn.microsoft.com/en-us/lib...(v=vs.85).aspx

    Why are you using C functions (printf/scanf) in a C++ program?

  3. #3
    Registered User surgeon's Avatar
    Join Date
    Jan 2015
    Posts
    26
    I have two files in one folder, so i enter only filename, without full path.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Then what is the name of the file?

    You should post the simplest program that demonstrates the problem.

    This looks suspicious:

    Code:
    scanf("%s", &szPath);
    What type is "szPath"?

  5. #5
    Registered User surgeon's Avatar
    Join Date
    Jan 2015
    Posts
    26
    char szPath[256];
    The file i've try to open is ollydbg.exe

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    And you're not getting any warnings from your compiler with that "scanf()" call?

    Output:
    Error hFile!
    5
    According to MSDN, a value of 5 indicates "access denied".

    The OllyDbg software seems to be used sometimes for questionable practices. What exactly is your goal with this program?

  7. #7
    Registered User surgeon's Avatar
    Join Date
    Jan 2015
    Posts
    26
    I use _CRT_SECURE_NO_WARNINGS.
    I think problem is not in scanf function, because i tried CreateFile(argc[1]) and there was the same problem

  8. #8
    Registered User surgeon's Avatar
    Join Date
    Jan 2015
    Posts
    26
    any ideas?

  9. #9
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    I gave strong hints about a problem with your "scanf()" call - whether or not it's related to the problem, it should still be looked into.

    I also gave a link that tells you what an error code of 5 stands for. A quick search with the appropriate terms yields plenty of results. Do some research, try to solve the problem, and if you're still stuck, post with an update (including what you've done since and how it didn't work).

  10. #10
    Registered User
    Join Date
    Dec 2010
    Location
    Trinidad, CO (log cabin in middle of nowhere)
    Posts
    148
    If Matticus won't spell it out for you (cuz he wants you to do a little work yourself) I will. This...

    scanf("%s", &szPath);

    ...is quite poor. Your C/C++ fundamentals are failing you. I expect you are coding in Windows as seen by the Hungarian Notation, which is fine (Oh! CreateFile() too!). Anyway, I expect you have something like so...

    char szPath[256];

    That's fine. In C/C++ syntax the name of an array without the brackets, i.e.,

    szPath

    ...is a pointer to the first byte of the array, i.e., its base address. So your scanf function call needs to lose the '&' symbol.

    Why in the world would you want to open OllyDebug.exe for writting???

  11. #11
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Quote Originally Posted by surgeon View Post
    I use _CRT_SECURE_NO_WARNINGS.
    I think problem is not in scanf function, because i tried CreateFile(argc[1]) and there was the same problem
    I assume you meant argv[1].

    Access violation may be due to trying to open a read only file for writing or a security attributes issue. Check the attributes on that file. You could use Windows file / folder explorer (My Computer), right click on file, then properties.
    Last edited by rcgldr; 12-24-2015 at 03:10 PM.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by freddie View Post
    If Matticus won't spell it out for you (cuz he wants you to do a little work yourself) I will. This...

    scanf("%s", &szPath);

    ...is quite poor. Your C/C++ fundamentals are failing you. I expect you are coding in Windows as seen by the Hungarian Notation, which is fine (Oh! CreateFile() too!). Anyway, I expect you have something like so...

    char szPath[256];

    That's fine. In C/C++ syntax the name of an array without the brackets, i.e.,

    szPath

    ...is a pointer to the first byte of the array, i.e., its base address. So your scanf function call needs to lose the '&' symbol.

    Why in the world would you want to open OllyDebug.exe for writting???
    It's not just poor. It's a security risk.
    But we have yet to hear if this is C or C++. The code is pure C, but it's posted in the C++ board.
    There are better ways to read strings--safely--in both languages.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. createfile()
    By mr_empty in forum C++ Programming
    Replies: 4
    Last Post: 11-20-2007, 05:50 AM
  2. CreateFile function
    By Brij in forum Windows Programming
    Replies: 5
    Last Post: 09-12-2006, 09:38 AM
  3. CreateFile();
    By cgod in forum C++ Programming
    Replies: 10
    Last Post: 12-31-2004, 05:05 AM
  4. for using CreateFile()
    By Jasonymk in forum Windows Programming
    Replies: 3
    Last Post: 02-28-2003, 06:47 AM
  5. Using CreateFile() in Win NT
    By Bazzz in forum Windows Programming
    Replies: 1
    Last Post: 09-17-2002, 12:41 AM