Checking File Existence with C and Win32 API

  • Thread starter lysip
  • Start date
In summary: I've never heard of "NtOpenFile". I've heard of _NtOpenFile@24, though. I know that one. I don't know if NtOpenFile is an API function or some other function. If it's an API function, please include the header and library that declares and defines it. If it's some other function, give me a reference to the code. I have Visual C++ 6.0, so I can look at the header and library files.2) I'll assume that the file specified by the "file_name" parameter is supposed to exist. If it doesn't exist, then the initial value of hFind will be INVALID_HANDLE_VALUE and the return value will be
  • #1
lysip
5
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 teh 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
  • #2
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.
 
  • #3
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:
  • #4
lysip said:
ok that didn't work so its time to start over...

It works just fine over here.
 
  • #5
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
 
  • #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...
 
  • #7
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.
 
  • #8
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.
 

What is the purpose of checking file existence with C and Win32 API?

The purpose of checking file existence is to determine whether a certain file exists in a given location. This can be useful in various applications, such as checking if a required file is present before attempting to open it, or verifying the success of a file transfer.

How do you check for file existence using C and Win32 API?

To check for file existence, you can use the GetFileAttributes() function from the Win32 API. This function takes in the file path as a parameter and returns a DWORD value that contains file attributes, including the FILE_ATTRIBUTE_DIRECTORY flag if the specified path is a directory. If the function returns INVALID_FILE_ATTRIBUTES, it means the file or directory does not exist.

Can you check for file existence in a specific directory?

Yes, you can specify the directory path in the GetFileAttributes() function to check for file existence in a specific directory. This can be useful when dealing with multiple files in different locations and needing to verify their existence before performing operations on them.

What is the difference between checking for file existence and file access in C and Win32 API?

Checking for file existence simply determines whether a file or directory exists in a given location, while file access checks if the specified file or directory is accessible and can be opened. The GetFileAttributes() function can be used for both file existence and access, but it only returns the file attributes and does not open or access the file.

Are there any potential errors or exceptions when checking for file existence with C and Win32 API?

One potential error is when the specified file path is invalid or contains invalid characters, which can cause the GetFileAttributes() function to return INVALID_FILE_ATTRIBUTES. Another potential error is when the file or directory exists but the user does not have permission to access it. In this case, the function will return the file attributes but may not accurately reflect the file access.

Similar threads

Replies
16
Views
2K
  • Programming and Computer Science
Replies
16
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
3K
  • Programming and Computer Science
Replies
1
Views
646
  • Programming and Computer Science
Replies
6
Views
8K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
3K
Replies
1
Views
1K
  • Programming and Computer Science
Replies
4
Views
2K
Back
Top