如何用API函数CreateDirectory 创建目录的同时,设置该目录的安全性?(100分)

  • 主题发起人 主题发起人 大师
  • 开始时间 开始时间

大师

Unregistered / Unconfirmed
GUEST, unregistred user!
函数原型:<br>BOOL CreateDirectory(<br>&nbsp; LPCTSTR lpPathName, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // pointer to directory path string<br>&nbsp; LPSECURITY_ATTRIBUTES lpSecurityAttributes &nbsp;// pointer to security descriptor<br>);<br>关键是LPSECURITY_ATTRIBUTES的设置,大部分资料都简单设为0,即默认安全性<br>
 
只有NT才能设置.<br>
 
我知道,请问在NTFS文件系统下如何调用呢?
 
大概看了下帮助, 好麻烦.<br>首先先当然必须指定一个SECURITY_ATTRIBUTES结构.<br>然后用GetSecurityDescriptorControl, GetSecurityDescriptorDacl, <br>GetSecurityDescriptorGroup, GetSecurityDescriptorLength, <br>GetSecurityDescriptorOwner, GetSecurityDescriptorSacl, <br>InitializeSecurityDescriptor 之一获取一个SECURITY_DESCRIPTOR, 再用<br>&nbsp;SetSecurityDescriptorDacl, SetSecurityDescriptorGroup, <br>SetSecurityDescriptorOwner, SetSecurityDescriptorSacl 之一设置希望的值,<br>再将这个SECURITY_DESCRIPTOR赋值给SECURITY_ATTRIBUTES.lpSecurityDescriptor....
 
Another_eYes:<br>能给出Delphi下的实例吗,很有用的功能哟!!!<br>(我API不熟悉)
 
大师:如果还想接着讨论请定期提前自己的帖子,如果不想继续讨论请结束帖子。<br>
 
参考help不行吗?
 
procedure TForm1.CreateDirectory1Click(Sender: TObject);<br>var<br>&nbsp; ErrorMessage: Pointer; &nbsp; &nbsp;// holds a system error message<br>&nbsp; ErrorCode: DWORD; &nbsp; &nbsp; &nbsp; &nbsp; // holds a system error code<br>begin<br>&nbsp; {determine if a directory path has been specified}<br>&nbsp; if DirName.GetTextLen = 0 then<br>&nbsp; begin<br>&nbsp; &nbsp; StatusBar1.SimpleText := 'Directory name not specified';<br>&nbsp; &nbsp; Exit;<br>&nbsp; end;<br><br><br>&nbsp; {if so, then create the new directory under the current directory}<br>&nbsp; if not CreateDirectory(PChar(DirectoryListBox1.Directory + '/' +<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;DirName.Text), nil) then<br>&nbsp; begin<br>&nbsp; &nbsp; {if there was an error creating the directory, display the error message}<br>&nbsp; &nbsp; ErrorCode := GetLastError;<br>&nbsp; &nbsp; FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER or FORMAT_MESSAGE_FROM_SYSTEM,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nil, ErrorCode, 0, @ErrorMessage, 0, nil);<br><br>&nbsp; &nbsp; StatusBar1.SimpleText:='Error Copying File: '+string(PChar(ErrorMessage));<br>&nbsp; &nbsp; LocalFree(hlocal(ErrorMessage));<br>&nbsp; end;<br><br>&nbsp; {update the directory listing to show the new directory}<br>&nbsp; DirectoryListBox1.Update;<br>end;<br><br>procedure TForm1.CreateDirectoryFromTemplate1Click(Sender: TObject);<br>var<br>&nbsp; ErrorMessage: Pointer; &nbsp; &nbsp;// holds a system error message<br>&nbsp; ErrorCode: DWORD; &nbsp; &nbsp; &nbsp; &nbsp; // holds a system error code<br><br>begin<br>&nbsp; {determine if a directory path has been specified}<br>&nbsp; if DirName.GetTextLen = 0 then<br>&nbsp; begin<br>&nbsp; &nbsp; StatusBar1.SimpleText := 'Directory name not specified';<br>&nbsp; &nbsp; Exit;<br>&nbsp; end;<br><br>&nbsp; {if so, then create the new directory under the current directory}<br>&nbsp; if not CreateDirectoryEx(PChar(Template.Text),PChar(DirectoryListBox1.<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Directory + '/' + DirName.Text), nil) then<br><br>&nbsp; begin<br>&nbsp; &nbsp; {if there was an error creating the directory, display the error message}<br>&nbsp; &nbsp; ErrorCode := GetLastError;<br>&nbsp; &nbsp; FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER or FORMAT_MESSAGE_FROM_SYSTEM,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nil, ErrorCode, 0, @ErrorMessage, 0, nil);<br>&nbsp; &nbsp; StatusBar1.SimpleText:='Error Copying File: '+string(PChar(ErrorMessage));<br>&nbsp; &nbsp; LocalFree(hlocal(ErrorMessage));<br>&nbsp; end;<br><br>&nbsp; {reset UI elements}<br>&nbsp; Template.Text := '';<br><br>&nbsp; CreateDirectoryFromTemplate1.Enabled := FALSE;<br>&nbsp; CreateDirectory1.Enabled := TRUE;<br>&nbsp; Template1.Enabled := TRUE;<br>&nbsp; ClearTemplate1.Enabled := FALSE;<br><br>&nbsp; {update the directory listing to show the new directory}<br>&nbsp; DirectoryListBox1.Update;<br>end;<br>
 
接受答案了.
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
后退
顶部