Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C and the win32api

  1. Sep 12, 2004 #1
    ok well im new to this fourm and from what ive looked at you guys should be proud. anyway to the point im trying to make a function that checks if a file exists or not and returns 0 or 1 acordingly. what i have so far

    here is my file_exist.h the file that i get build errors in...
    Code (Text):

    NTSTATUS
    Xefile_exists(const OCHAR file_1[]) {
    /******************************************************
     * checks and sees if a file exists if not it returns *
     * 0 if so it returns 1                               *
     ******************************************************/
       
        NTSTATUS status;
        OBJECT_ATTRIBUTES ObjectAttributes;
        IO_STATUS_BLOCK IoStatusBlock;
        HANDLE file_1;
        OCHAR RedirectionBuffer[MAX_LAUNCH_PATH + 1];
        POSTR Delimiter;

        InitializeObjectAttributes(&ObjectAttributes, file_1, OBJ_CASE_INSENSITIVE, NULL, NULL);

        status = NtOpenFile(file_1, GENERIC_READ, &ObjectAttributes, &IoStatusBlock, FILE_SHARE_READ, FILE_SYNCHRONOUS_IO_NONALERT);

        if (!NT_SUCCESS(status)) {
            return 0;
        } else {
            return 1;
        }

    }
     
    i took that part that opens the file from some orther sources i had as far as i know they should work. but the problem is when i pass a const into it as the file.

    my const in my orther file is this as i define it
    Code (Text):

    const OCHAR xb_xbdm_enabled[] = OTEXT("\\Device\\Harddisk0\\Partition1\\debug_monitor_enabled.bleh");
     
    then i use it like this useing the if statment
    Code (Text):
    if (Xefile_exists(xb_xbdm_enabled) == 1)
    when i try and build i get a few errors haveing to do with the const.

    my errors are
    ive tried a few things but i cant seem to get it to build. if anybody could help that would be great. oh and if anybody teh windows api that would be realy great seening as im sure some of the stuff in my file_exist.h is useless. thanks
     
  2. jcsd
  3. Sep 12, 2004 #2
    c code. bleah.

    One problem with your code is that it is illegal in C/C++ to write:

    Code (Text):
    void f(int a)
    {
       int a;
    }
     
    To solve your problem I would write something like:

    Code (Text):
    int file_exists(const char file_name[])
    {
      WIN32_FIND_DATA FindFileData;
      HANDLE hFind;
      hFind = FindFirstFile(file_name, &FindFileData);
      if (hFind == INVALID_HANDLE_VALUE)
        return 0;
      else
      {
        FindClose(hFind);
        return 1;
      }
    }
    This is almost a copy/paste from a MSDN example of using FindFirstFile win32 API function.

    But the more serious problem is that you are still learning C in 2004. Tell me if this isn't easier:

    Code (Text):
    File.Exists("whatever name");
    That is the C# code. The class File and the static method Exists are part of the .NET class library. You don't need to write them. Try to concentrate on solving new problems.
     
  4. Sep 12, 2004 #3
    ok that didn't work so its time to start over...
    ok well here is my function as it is right now... ive been changeing things trying to get it to work.
    Code (Text):

    Xefile_exists(const char PcFileName) {
    /******************************************************
     * checks and sees if a file exists if not it returns *
     * 0 if so it returns 1                    -lysip     *
     ******************************************************/
       
        NTSTATUS Pcstatus;
        HANDLE PcFileHandle;
        OBJECT_ATTRIBUTES O_Att;
        IO_STATUS_BLOCK IoSBlock;

        InitializeObjectAttributes(&O_Att, &xbdm_enabled, OBJ_CASE_INSENSITIVE, NULL, NULL);

        Pcstatus = NtOpenFile(&PcFileHandle, GENERIC_READ, &O_Att, &IoSBlock, FILE_SHARE_READ, FILE_SYNCHRONOUS_IO_NONALERT);
       
        if (!NT_SUCCESS(Pcstatus)) {
            return 0;
        } else {
            NtClose(PcFileHandle);
            return 1;
        }

    }
     
    and i specify this
    Code (Text):
    #include <file_exist.h>
    and this
    Code (Text):
    INITIALIZED_OBJECT_STRING_RDATA(xbdm_enable, "\\Device\\Harddisk0\\Partition1\\xbdm_enabled");
    in a diffrent file that i want to run the function from...
    Code (Text):
    if (Xefile_exists(xbdm_enable) == 1) {
    blah
    blah
    } else {
    blah
    }
    and it has to be written with NtOpenfile anybody got anyclue?

    oh and one more thing im learning from example so some of this stuff is alilttle confuseing and may be unnecessary... :)
     
    Last edited: Sep 12, 2004
  5. Sep 12, 2004 #4
    It works just fine over here.
     
  6. Sep 13, 2004 #5
    well ya but ut doesnt work for me seeing as what im doing. it may work in C but it has to be done with ntopenfile. to work for me. so you see my problem
     
  7. Sep 13, 2004 #6
    No, I can't. The phrase "has to be made with NtOpenFile" makes sense only in two cases:

    1. you like complicated things
    2. it is a homework

    The first variant is supported by the fact that you are learning win32api and C. I did learn those two but this was a few years ago when it made sense to learn them. In any case, I am not willing to read carefully that attrocious code you posted. Maybe you'll find someone with more time to spare...
     
  8. Sep 13, 2004 #7

    dduardo

    User Avatar
    Staff Emeritus

    lysip, that's some ugly code just to check to see if a file exists. I don't program for windows, so I don't know the windows API, but if your doing straight C you can simply use fopen and check if it returns NULL.
     
  9. Sep 13, 2004 #8
    well thanks for the help kinda but i absolutly have to use notopenfile, and this isnt homework, as you said i just liek complacated things. they way the build is and has to be is to use ntopenfile, im sorry if this confuses some ppl but it confuses me too. im alittle furstrated by this seeing as i have to use ntopenfile. ive poored over the msdn and i still cant get to work. but thanks for you guys reponse.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: C and the win32api
  1. Learning C/C++ (Replies: 5)

  2. C question (Replies: 6)

  3. C++ vector (Replies: 3)

  4. Learning C++ (Replies: 9)

  5. A C problem (Replies: 9)

Loading...