Jump to content

What is CreateFile function.

- - - - -

  • Please log in to reply
7 replies to this topic

#1
freiza

freiza

    Learning Programmer

  • Members
  • PipPipPip
  • 54 posts
CreateFileW( 

L"FileName",

GENERIC_READ,

FILE_SHARE_WRITE,  // Can I use it like this   FILE_SHARE_WRITE|FILE_SHARE_READ  FOR BOTH READ AND WRITE ACCESS or simply FILE_SHARE_WRITE is sufficient for both read and write access simultaneously?

lpSecurityAttributes, // I tried reading msdn but didn't understand it. Please explain me in simple way. 

OPEN_EXISTING,

dwFlagsAndAttributes, // (Why in certain examples it is 0. While 0 is not define in msdn. i.e FILE_ATTRIBUTE_NORMAL == 0x80. But what is 0 stands for.)

hTemplateFile     // I didn't understand this too. What is templatefile?


#2
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US
What's your question?

#3
freiza

freiza

    Learning Programmer

  • Members
  • PipPipPip
  • 54 posts
Q1 Can I use FILE_SHARE_WRITE|FILE_SHARE_READ FOR BOTH READ AND WRITE ACCESS or simply FILE_SHARE_WRITE is sufficient for both read and write access simultaneously?
Q2 What is lpSecurityAttributes?
Q3 Why in certain examples dwFlagsAndAttributes is set to 0. While 0 is not define in msdn. i.e FILE_ATTRIBUTE_NORMAL == 0x80. But what is 0 stands for?
Q4 What is templatefile?

#4
Ancient Dragon

Ancient Dragon

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 400 posts
>>// I tried reading msdn but didn't understand it. Please explain me in simple way.

Just set it to NULL.

Here is how I normally call that function

	CreateFile(L"Hello", GENERIC_READ|GENERIC_WRITE,0,

		0,FILE_ATTRIBUTE_NORMAL,0,0);


Visit Grandpa's Forums, a social networking forum, with family-oriented arcade games, blogs, discussion forums, and photo albums.

#5
freiza

freiza

    Learning Programmer

  • Members
  • PipPipPip
  • 54 posts
But what is it actually? And when it is not set to NULL

#6
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US
A1: I suppose FILE_SHARE_* just means that you allow other processes (programs) to * your file. And yes, I do believe you have to include all that apply; so for other processes to be able to read the file, you would use FILE_SHARE_READ, while for other processes to have read, write, and delete access to the file, you would use FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE

A2: It is the pointer to (memory address of) the SECURITY_ATTRIBUTES structure for the file. I do not know how the SECURITY_ATTRIBUTES structure is used, nor what's it for, except for something about security. It usually works when I use 0 for this parameter.

A3: I don't know. I didn't even know you can leave that as 0. I usually use FILE_ATTRIBUTE_NORMAL.

A4: I am actually not sure what it means. It's been a while since I read the MSDN page about it. Hold on, let me check what it says. Oh, I think it's for using the file's (that you specify the handle of) attributes. So if you open file1.txt, and you are making a new file, file2.txt, then you can specify file1.txt ' s handle here, and so if file1.txt is hidden, for example, then file2.txt would also be hidden. That's from what I understood from reading the description, at least. You can just use 0 for this, if you don't care about that part of it.

#7
freiza

freiza

    Learning Programmer

  • Members
  • PipPipPip
  • 54 posts
@RhetoricalRuvim : someone told me this. So I'd like to share this with you.

A2) Security Attributes control things like sharing file handles and thread safe access... this can usually be NULL in which case the default security descriptor for the directory you are in is used.
A3) When reading or opening an existing file this is usually 0, which does not change the existing disk attributes of the file.

And are you sure we can use dwShareMode like this too FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE ?
I thought just mentioning FILE_SHARE_WRITE will give you read access by default? (I am not sure , I am just guessing)
And If you get any example on how to use hTemplatefile. Please post.
Thank You

#8
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US
Well, having in mind that FILE_SHARE_READ is 0x01 (0001b), FILE_SHARE_WRITE is 0x02 (0010b), and FILE_SHARE_DELETE is 0x04 (0100b), it would make sense that those three can be ORd, or added, to make 0111b for full sharing access. At least from what I understand, including this page, you do have to explicitly specify all the FILE_SHARE_* values. Or you can use the number 7, which is the same as all three ORd together; that's what I like to use.

As for the hTemplateFile parameter, I never used that before, so I don't think I would be able to provide any good examples; however, maybe there's someone else on this forum who could, I don't know.

With the GENERIC_READ and GENERIC_WRITE and GENERIC_EXECUTE, you have to specify all that apply, also. Though if you want to specify all of them, you can just use GENERIC_ALL .




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users