-1

我正在尝试从 C++ 执行 icacls。下面的代码什么都不做。我仍然不知道为什么它什么都不做。我看不到 icacls 返回给我的内容,因为 cmd windows 自动关闭。HINSTANCE 也没有为我提供太多。如何使用具有多个参数的 icacls?

HINSTANCE hInst = ShellExecute( NULL, 
NULL,  
L"icacls",  
L"s.jpg /grant:r %username%:W",     
L"C:/",    
SW_NORMAL 
); 
4

3 回答 3

1

您的lpFile参数可能应该"icacls.exe"带有.exe扩展名。

此外,您应该始终检查错误。如果ShellExecute()成功,则返回大于 32 的值。有关可能返回的错误代码列表,请参阅MSDN 。

于 2011-11-13T02:46:52.407 回答
0

Undeleting post:

I had written up this bit of code earlier, unfortunately it is CLR/.NET specific. However, since you professed that using 'the API' is hard (it is, I did it 10 years ago and NTFS ACLS are no picknick), you might be motivated by the below sample to integrate a bit of .NET code (C++/CLI or Interop based?)


Any specific reason not to use C# code?

AddFileSecurity(fileName, @"DomainName\AccountName",
        FileSystemRights.ReadData, AccessControlType.Allow);

RemoveFileSecurity(fileName, @"DomainName\AccountName",
        FileSystemRights.ReadData, AccessControlType.Allow);

With the following helpers from MSDN: How to: Add or Remove Access Control List Entries:

public static void AddFileSecurity(string fileName, string account,
        FileSystemRights rights, AccessControlType controlType)
{
    FileSecurity fSecurity = File.GetAccessControl(fileName);
    fSecurity.AddAccessRule(new FileSystemAccessRule(account,
                rights, controlType));
    File.SetAccessControl(fileName, fSecurity);
}

public static void RemoveFileSecurity(string fileName, string account,
        FileSystemRights rights, AccessControlType controlType)
{
    FileSecurity fSecurity = File.GetAccessControl(fileName);
    fSecurity.RemoveAccessRule(new FileSystemAccessRule(account,
                rights, controlType));
    File.SetAccessControl(fileName, fSecurity);
}

See the article for full details and comments

于 2011-11-12T22:50:42.220 回答
0

icacls出于测试目的,您可以通过将其包装在以下内容中来查看输出cmd /k

HINSTANCE hInst = ShellExecute( NULL,
    NULL,
    L"cmd",
    L"/k icacls s.jpg /grant %username%:W",
    L"C:/",
    SW_NORMAL
);

[为什么grant:r?]

于 2011-11-13T21:37:17.760 回答