PDA

View Full Version : C and the win32api


lysip
Sep12-04, 09:09 AM
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...

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

const OCHAR xb_xbdm_enabled[] = OTEXT("\\Device\\Harddisk0\\Partition1\\debug_monitor_ena bled.bleh");


then i use it like this useing the if statment
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 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

rgrig
Sep12-04, 02:08 PM
One problem with your code is that it is illegal in C/C++ to write:

void f(int a)
{
int a;
}


To solve your problem I would write something like:

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:

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.

lysip
Sep12-04, 04:29 PM
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.

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
#include <file_exist.h>

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

in a diffrent file that i want to run the function from...
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... :)

rgrig
Sep12-04, 10:06 PM
ok that didn't work so its time to start over...

It works just fine over here.

lysip
Sep13-04, 05:14 AM
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

rgrig
Sep13-04, 09:28 AM
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...

dduardo
Sep13-04, 02:42 PM
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.

lysip
Sep13-04, 05:38 PM
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.