Link to home
Start Free TrialLog in
Avatar of mi6agent
mi6agent

asked on

Help with CreateFile...

Can anyone provide a sample of using CreateFile to open a file for read/write access and if the file does not exist, create it.  In both cases, returning a handle to the file to work with from that point onward.

I've tried to play around with CreateFile and it just isn't working out for me.  

So any help or even advice so i can understand it all better would be welcomed.
SOLUTION
Avatar of mokule
mokule
Flag of Poland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mi6agent
mi6agent

ASKER

I used

fhandle:=CreateFile(PChar(pathandfilename),
                         GENERIC_READ or GENERIC_WRITE,
                         FILE_SHARE_READ or FILE_SHARE_WRITE,
                         nil,
                         OPEN_ALWAYS,
                         FILE_ATTRIBUTE_NORMAL or FILE_FLAG_RANDOM_ACCESS,
                         0);

if fhandle=INVALID_HANDLE_VALUE  <== flag error else continue

as i wanted it to open the file if it exists or create it if it didn't BUT - i keep getting ERROR_NET_WRITE_FAULT and Access is denied (when i check via getlasterror).

You can use the fileexists function to check if the file exists:

if fileexists(pchar(filename)) then
open_file_code
else
begin
fhandle:=CreateFile(PChar(pathandfilename),
                         GENERIC_READ or GENERIC_WRITE,
                         FILE_SHARE_READ or FILE_SHARE_WRITE,
                         nil,
                         OPEN_ALWAYS,
                         FILE_ATTRIBUTE_NORMAL or FILE_FLAG_RANDOM_ACCESS,
                         0);
end;

Regards,

Hypoviax
perhaps the file you wanted is on a Network share and you do not have the privileges to access it?
Hi,

mi6agent, your code seems correct. It might be priviledges as DragonSlayer said but it might also be wrong file path. What is the content of pathandfilename variable? UNC file name like
\\computername\sahedresource\filename
or something else?

If the filename is correct then try to open the file for read-only:

fhandle:=CreateFile(PChar(pathandfilename),
                         GENERIC_READ,
                         FILE_SHARE_READ or FILE_SHARE_WRITE,
                         nil,
                         OPEN_EXISTING,
                         0,
                         0);

If the above doesn't fail then you don't have write priviledges.

Regards, Geo
> perhaps the file you wanted is on a Network share and you do not have the privileges to access it?

That was my thought at first DragonSlayer - then i realized, i am not on a network, am logged in as administrator (Win2000) and i'm using FAT32 - so privileges should not be an issue.

The pathandfilename is set to a normal path and filename type - ie: c:\test\testfile.txt

any help or pointers are welcomed
Here are the error code listing http://support.microsoft.com/kb/q155011/

And your error (if you got the right one, that is) is definitely network related... strange...
Very strange.  The error "A write fault occurred on the network" seems odd when i'm not on a network or even trying to write/read from a networked system - it's all local.

just thought of something - maybe i'm at fault somewhere.

i've edited the code a bit but here is the code and how to see the error.

var
fhandle : THandle;
begin
fhandle:=CreateFile(PChar(pathandfilename),
                        GENERIC_READ or GENERIC_WRITE,
                        FILE_SHARE_READ or FILE_SHARE_WRITE,
                        nil,
                        CREATE_NEW,
                        FILE_ATTRIBUTE_NORMAL,
                        0);

Showmessage('Handle Returned = ' + IntTostr(fHandle) + ' -  LastError = ' + GetLastError);

    <== On first run of code - Message will show:  Handle Returned = 88 - LastError = 0
    <== On second run of code - Message will show:  Handle Returned = really large number - LastError = 80

I expect the LastError values but the "Handle Returned" values?  Are these correct or as i thought, errors.


if fhandle=INVALID_HANDLE_VALUE  <== flag error else continue
??? So You don't receive ERROR_NET_WRITE_FAULT
yes - on the first run of the code the handle returned is 88 (ERROR_NET_WRITE_FAULT)

or have i got the wrong end of the stick with the createfile function?
???
I can't see it here.
You've got Handle Returned = 88

    <== On first run of code - Message will show:  Handle Returned = 88 - LastError = 0
    <== On second run of code - Message will show:  Handle Returned = really large number - LastError = 80
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/debug/base/system_error_codes__500-999_.asp

Code 88 =  A write fault occurred on the network. ERROR_NET_WRITE_FAULT

<== On first run of code - Message will show:  Handle Returned = 88 <== Error?
You've received Handle 88. It is not an error code !!
In my previous code there were errors.
Should be like this

procedure TForm1.Button1Click(Sender: TObject);
var
      fHandle: THandle;
      Filename: string;
      nWritten: cardinal;
      nWrite: cardinal;
      buffer: string;
begin
    Filename := 'C:\test.txt';
    fHandle := CreateFile(PChar(Filename),GENERIC_READ or GENERIC_WRITE,  FILE_SHARE_READ or FILE_SHARE_WRITE, nil, OPEN_ALWAYS, 0, 0);
    if fHandle <> INVALID_HANDLE_VALUE then
      begin
      buffer := 'something';
      nWrite := Length(buffer);
      WriteFile(fHandle,PChar(buffer)^, nWrite, nWritten, nil);
      CloseHandle(fHandle);
      Edit1.Text := 'OK';
      end
    else
      Edit1.Text := IntToStr(getlasterror);
end;
ok, let me play around with this a bit more.  I'll get back to you soon.

thanks for the help - appreciated :)
A possible cause could be that file is already opened EXLUSIVELY by other app.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
duh...
geobul is right...
and yep, i heard myself say "duh!" when i realised i made a mistake.

i'm going to split the points between mokule & geobul if that is ok with everyone?

i'll wait a day to see if anyone objects - just making sure that i am being fair :)
thanks again to all