source: trunk/Utilities/Miscellaneous/SetFileOwner/SetFileOwner.cpp @ 118

Last change on this file since 118 was 118, checked in by roman, 10 years ago
  • Property svn:keywords set to Id
File size: 1.8 KB
Line 
1////////////////////////////////////////////////////////////
2// Copyright (C) Roman Ryltsov, 2012
3// Created by Roman Ryltsov roman@alax.info
4//
5// $Id: SetFileOwner.cpp 118 2012-09-09 12:23:37Z roman $
6
7#include "stdafx.h"
8#include <atlpath.h>
9#include <atlsecurity.h>
10#include <atlfile.h>
11
12VOID PrintOwner(LPCTSTR pszPath)
13{
14        PSID pSecurityIdentifer = NULL;
15        ATLENSURE_SUCCEEDED(HRESULT_FROM_WIN32(GetNamedSecurityInfo(const_cast<LPTSTR>(pszPath), SE_FILE_OBJECT, OWNER_SECURITY_INFORMATION, &pSecurityIdentifer, NULL, NULL, NULL, NULL)));
16        CSid SecurityIdentifer((const SID*) pSecurityIdentifer);
17        _tprintf(_T("Owner: %s\\%s\n"), SecurityIdentifer.Domain(), SecurityIdentifer.AccountName());
18}
19
20int _tmain(int argc, _TCHAR* argv[])
21{
22        #pragma region Enable SeRestorePrivilege
23        CAccessToken Token;
24        if(!Token.GetThreadToken(TOKEN_QUERY | TOKEN_ADJUST_PRIVILEGES))
25        {
26                const HRESULT nResult = AtlHresultFromLastError();
27                ATLENSURE_THROW(nResult == HRESULT_FROM_WIN32(ERROR_NO_TOKEN), nResult);
28                ATLENSURE_THROW(Token.GetProcessToken(TOKEN_QUERY | TOKEN_ADJUST_PRIVILEGES), AtlHresultFromLastError());
29        }
30        ATLENSURE_THROW(Token.EnablePrivilege(SE_RESTORE_NAME), AtlHresultFromLastError());
31        #pragma endregion
32        static LPCTSTR g_pszPath = _T("D:\\Projects\\Alax.Info\\Repository-Public\\Utilities\\Miscellaneous\\SetFileOwner\\ReadMe.txt");
33        PrintOwner(g_pszPath);
34        CSid SecurityIdentifer;
35        ATLENSURE_THROW(SecurityIdentifer.LoadAccount(_T("Guest")), AtlHresultFromLastError());
36        _tprintf(_T("New Owner: %s\\%s\n"), SecurityIdentifer.Domain(), SecurityIdentifer.AccountName());
37        ATLENSURE_SUCCEEDED(HRESULT_FROM_WIN32(SetNamedSecurityInfo(const_cast<LPTSTR>(g_pszPath), SE_FILE_OBJECT, OWNER_SECURITY_INFORMATION, const_cast<SID*>((const SID*) SecurityIdentifer), NULL, NULL, NULL)));
38        PrintOwner(g_pszPath);
39        return 0;
40}
41
Note: See TracBrowser for help on using the repository browser.