所以即时尝试创建一个需要root权限的应用程序,我发现了这个功能,但我一直收到错误,"没有匹配的成员函数来调用' push_back'",我尝试将矢量更改为字符串,然后将其转换为字符*但后来我得到了分段错误11,
static bool execute(const string &program, const vector<string> &arguments) {
AuthorizationRef ref;
if (AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment, kAuthorizationFlagDefaults, &ref) != errAuthorizationSuccess) {
return false;
}
AuthorizationItem item = {
kAuthorizationRightExecute, 0, 0, 0
};
AuthorizationRights rights = { 1, &item };
const AuthorizationFlags flags = kAuthorizationFlagDefaults
| kAuthorizationFlagInteractionAllowed
| kAuthorizationFlagPreAuthorize
| kAuthorizationFlagExtendRights;
if (AuthorizationCopyRights(ref, &rights, kAuthorizationEmptyEnvironment, flags, 0) != errAuthorizationSuccess) {
AuthorizationFree(ref, kAuthorizationFlagDestroyRights);
return false;
}
vector<char*> args;
for (vector<string>::const_iterator it = arguments.begin(); it != arguments.end(); ++it) {
args.push_back(it->c_str()); // Error here, "No matching member function for call to 'push_back'"
}
args.push_back(0);
OSStatus status = AuthorizationExecuteWithPrivileges(ref, program.c_str(), kAuthorizationFlagDefaults, &args[0], 0);
AuthorizationFree(ref, kAuthorizationFlagDestroyRights);
return status == errAuthorizationSuccess;
}