使用ShellExecute运行具有多个参数的icacls

时间:2011-11-12 22:44:23

标签: c++ cmd shellexecute cacls

我正在尝试从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 
); 

3 个答案:

答案 0 :(得分:1)

您的lpFile参数可能应为"icacls.exe",其扩展名为.exe

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

答案 1 :(得分:0)

取消删除帖子:

我之前写过这段代码,遗憾的是它是CLR / .NET特有的。但是,既然你声称使用'API'很难(就是我10年前做过而NTFS ACLS没有选择),你可能会受到以下示例的激励,以集成一些.NET代码(C ++ / CLI或Interop基础?)


不使用C#代码的任何特定原因?

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

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

来自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);
}

请参阅文章以获取完整的详细信息和评论

答案 2 :(得分: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?]