我正在爲MacOSX編寫一個安裝程序/ Autoupdater應用程序(使用Qt和C++)。我需要升級權限才能覆蓋應用程序文件夾中的舊文件。在MacOSX下啓動C中的特權應用程序
我的升級代碼是基於下面的例子:http://www.michaelvobrien.com/blog/2009/07/authorizationexecutewithprivileges-a-simple-example/ 我試着重新啓動我的現有應用程序具有管理員權限是這樣的:
void MainDialog::EscalatePrivileges()
{
AuthorizationRef authorizationRef;
OSStatus status;
status = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment, kAuthorizationFlagDefaults, &authorizationRef);
char* tool = QApplication::instance()->applicationFilePath().toLocal8Bit().data();
char* args[] = { "STARTUPDATE", NULL };
FILE* pipe = NULL;
status = AuthorizationExecuteWithPrivileges(authorizationRef, tool, AuthorizationFlagDefaults, args, &pipe);
QApplication::instance()->quit();
}
不過,我得到錯誤-60031(發射失敗工具) 。 問題:
一)爲什麼會失敗?我懷疑它是因爲工作文件夾設置不正確? (我可以設置工具的工作文件夾不知何故?)
編輯:好了,想通,一出:在ARGS [] - 陣列本身需要以NULL結尾。已經固定在上面的代碼中。
b)其他互聯網來源說功能AuthorizationExecuteWithPrivileges已棄用,不應使用,因爲安全問題。有人可以舉例說明如何以更好的方式做到這一點?
[AuthorizationExecuteWithPrivileges is deprecated]的可能重複(http://stackoverflow.com/questions/6841937/authorizationexecutewithprivileges-is-deprecated) – bdash