Opening files with CreateFile

delphi package - easy access to security apis
Post Reply
2BrightSparks
Posts: 28
Joined: Mon Jan 03, 2005 3:03 pm
Contact:

Opening files with CreateFile

Post by 2BrightSparks »

First, sorry about the constant questions :oops: I've got the usual "not enough time, too much too do" problem.

I need to get the security object via a Windows handle. This is because I need to open files/folders with Unicode names, and to do this I'm using CreateFileW. However, I cannot get a handle to the file if I'm denied access, but if I use your FileSecurity() method and pass the filename it works.

I'm using CreateFileW like this:

CreateFileW(PWideChar(useFName), GENERIC_READ, 0, nil, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS or FILE_FLAG_POSIX_SEMANTICS, 0)

What is the correct way to get a file/folder handle to just read/change the security info? Or do I need to set a privelage first? I've a feeling you're going to say you are using the GetFileSecurity API...If so, any chance of adding support for widestring filenames?

Thanks
2BrightSparks
Posts: 28
Joined: Mon Jan 03, 2005 3:03 pm
Contact:

Post by 2BrightSparks »

After reading and searching the Win32 help files, I'll answer my own question :crazy:

To open for reading (with SE_BACKUP_NAME):

CreateFileW(PWideChar(useFName), READ_CONTROL, 0, nil, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS or FILE_FLAG_POSIX_SEMANTICS, 0);

And to open for writing (with SE_RESTORE_NAME):

CreateFileW(PWideChar(useFName), GENERIC_WRITE or WRITE_OWNER or WRITE_DAC, 0, nil, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS or FILE_FLAG_POSIX_SEMANTICS, 0);

Are you using CreateFile to get the handle, or GetFileSecurity ?

Thanks
madshi
Site Admin
Posts: 10764
Joined: Sun Mar 21, 2004 5:25 pm

Post by madshi »

I'm not getting a handle. There are security APIs which are happy with the name. If you give me a handle, I'm using it. If you give me a name, I'm using it directly, too, without converting it to a handle first.

Adding wide string support to madSecurity makes sense, I guess. Will do that in the next build.
madshi
Site Admin
Posts: 10764
Joined: Sun Mar 21, 2004 5:25 pm

Post by madshi »

Here's a new beta build with wide support:

http://madshi.net/madCollectionBeta.exe
2BrightSparks
Posts: 28
Joined: Mon Jan 03, 2005 3:03 pm
Contact:

Post by 2BrightSparks »

Excellent, thanks!
Post Reply