Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Help with CreateFile...

Posted on 2004-11-08
21
Medium Priority
?
563 Views
Last Modified: 2008-02-26
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.
0
Comment
Question by:mi6agent
  • 9
  • 5
  • 3
  • +2
21 Comments
 
LVL 17

Assisted Solution

by:mokule
mokule earned 500 total points
ID: 12526292
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, CREATE_ALWAYS, 0, 0);
    buffer := 'something';
    nWrite := Length(buffer);
    WriteFile(fHandle,PChar(buffer), nWrite, nWritten, nil);
    CloseFile(fHandle);
0
 

Author Comment

by:mi6agent
ID: 12526404
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).

0
 
LVL 5

Expert Comment

by:Hypoviax
ID: 12527952
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
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 14

Expert Comment

by:DragonSlayer
ID: 12530784
perhaps the file you wanted is on a Network share and you do not have the privileges to access it?
0
 
LVL 17

Expert Comment

by:geobul
ID: 12531160
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
0
 

Author Comment

by:mi6agent
ID: 12531468
> 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
0
 
LVL 14

Expert Comment

by:DragonSlayer
ID: 12531532
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...
0
 

Author Comment

by:mi6agent
ID: 12531548
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.

0
 

Author Comment

by:mi6agent
ID: 12531599
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
0
 
LVL 17

Expert Comment

by:mokule
ID: 12532168
??? So You don't receive ERROR_NET_WRITE_FAULT
0
 

Author Comment

by:mi6agent
ID: 12532229
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?
0
 
LVL 17

Expert Comment

by:mokule
ID: 12532252
???
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
0
 

Author Comment

by:mi6agent
ID: 12532325
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?
0
 
LVL 17

Expert Comment

by:mokule
ID: 12532336
You've received Handle 88. It is not an error code !!
0
 
LVL 17

Expert Comment

by:mokule
ID: 12532381
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;
0
 

Author Comment

by:mi6agent
ID: 12532398
ok, let me play around with this a bit more.  I'll get back to you soon.

thanks for the help - appreciated :)
0
 
LVL 17

Expert Comment

by:geobul
ID: 12532554
A possible cause could be that file is already opened EXLUSIVELY by other app.
0
 
LVL 17

Accepted Solution

by:
geobul earned 500 total points
ID: 12532617
On your second attempt (using CREATE_NEW) everything is OK.
- First run without error and the file was created.
- Second run : error 80 (file already exists) and INVALID_HANDLE_VALUE returned in fhandle (that really large number).

Regards, Geo
0
 
LVL 14

Expert Comment

by:DragonSlayer
ID: 12533129
duh...
0
 

Author Comment

by:mi6agent
ID: 12542406
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 :)
0
 

Author Comment

by:mi6agent
ID: 12565737
thanks again to all
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This is an update to some code that someone else posted on Experts Exchange. It is an alternate approach, I think a little easier to use, & makes sure that things like the Task Bar will update.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Loops Section Overview
Suggested Courses
Course of the Month14 days, 15 hours left to enroll

577 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question