Checking File Existence with C and Win32 API

  • Thread starter Thread starter lysip
  • Start date Start date
Click For Summary

Discussion Overview

The discussion centers on creating a function in C that checks for the existence of a file using the Win32 API, specifically the NtOpenFile function. Participants share code snippets, errors encountered during compilation, and their approaches to solving the problem.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant shares an initial implementation of a file existence check using NtOpenFile, but encounters compilation errors related to parameter redefinition and const qualifiers.
  • Another participant suggests a simpler approach using the FindFirstFile function from the Win32 API, but acknowledges that the original poster insists on using NtOpenFile.
  • A later reply critiques the complexity of the original code and suggests using fopen as an alternative method to check file existence, although the original poster insists on the requirement to use NtOpenFile.
  • Participants express frustration with the original poster's insistence on using NtOpenFile, with one stating that it may only make sense in the context of homework or a preference for complexity.
  • The original poster reiterates their need to use NtOpenFile and expresses confusion and frustration over their inability to get the code to work despite reviewing documentation.

Areas of Agreement / Disagreement

There is no consensus on the best approach to check for file existence, as participants propose different methods and express varying levels of frustration with the original poster's requirements.

Contextual Notes

Participants note that the original code may contain unnecessary complexity and that the requirement to use NtOpenFile is not universally agreed upon as the best practice for this task.

lysip
Messages
5
Reaction score
0
ok well I am new to this fourm and from what I've looked at you guys should be proud. anyway to the point I am 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:
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:
const OCHAR xb_xbdm_enabled[] = OTEXT("\\Device\\Harddisk0\\Partition1\\debug_monitor_enabled.bleh");

then i use it like this useing the if statement
Code:
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
file_exist.h(11) : error C2082: redefinition of formal parameter 'file_1'
file_exist.h(15) : error C4090: '=' : different 'const' qualifiers
file_exist.h(17) : error C4090: 'function' : different 'const' qualifiers
NMAKE : U1077: 'cl' : return code '0x2'

ive tried a few things but i can't seem to get it to build. if anybody could help that would be great. oh and if anybody the windows api that would be really great seening as I am sure some of the stuff in my file_exist.h is useless. thanks
 
Computer science news on Phys.org
c code. bleah.

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

Code:
void f(int a)
{
   int a;
}

To solve your problem I would write something like:

Code:
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:
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.
 
ok that didn't work so its time to start over...
ok well here is my function as it is right now... I've been changeing things trying to get it to work.
Code:
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:
#include <file_exist.h>

and this
Code:
INITIALIZED_OBJECT_STRING_RDATA(xbdm_enable, "\\Device\\Harddisk0\\Partition1\\xbdm_enabled");

in a diffrent file that i want to run the function from...
Code:
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 I am learning from example so some of this stuff is alilttle confuseing and may be unnecessary... :)
 
Last edited:
lysip said:
ok that didn't work so its time to start over...

It works just fine over here.
 
well you but ut doesn't work for me seeing as what I am doing. it may work in C but it has to be done with ntopenfile. to work for me. so you see my problem
 
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...
 
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.
 
well thanks for the help kinda but i absolutly have to use notopenfile, and this isn't homework, as you said i just liek complacated things. they way the build is and has to be is to use ntopenfile, I am sorry if this confuses some ppl but it confuses me too. I am alittle furstrated by this seeing as i have to use ntopenfile. I've poored over the msdn and i still can't get to work. but thanks for you guys reponse.