2010-02-08 22 views
5

क्या विंडोज में कोई एपीआई लिनक्स के chown जैसा है?विंडोज़ में फ़ाइल मालिक बदलें

+0

विंडोज सुरक्षा मॉडल वास्तव में फ़ाइल स्वामित्व के आसपास आधारित नहीं है जिस तरह यूनिक्स फ़ाइल सिस्टम हैं, इसलिए यह वास्तव में एक उपकरण नहीं है जो अक्सर आवश्यक होता है। –

उत्तर

3

यहाँ से लिया: http://www.perlmonks.org/?node_id=70562

// #includes omitted for the sake of sanity 
    HANDLE token; 
    char *filename = "somefile.txt"; 
    char *newuser = "someuser"; 
    DWORD len; 
    PSECURITY_DESCRIPTOR security = NULL; 
    PSID sidPtr = NULL; 
    int retValue = 1; 

    // Get the privileges you need 
    if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &token)) { 
     SetPrivilege(token, "SeTakeOwnershipPrivilege", 1); 
     SetPrivilege(token, "SeSecurityPrivilege", 1); 
     SetPrivilege(token, "SeBackupPrivilege", 1); 
     SetPrivilege(token, "SeRestorePrivilege", 1); 
    } else retValue = 0; 

    // Create the security descriptor 
    if (retValue) { 
     GetFileSecurity(filename, OWNER_SECURITY_INFORMATION, security, 0, &len); 
     security = (PSECURITY_DESCRIPTOR)malloc(len); 
     if (!InitializeSecurityDescriptor(security, SECURITY_DESCRIPTOR_REVISION)) 
      retValue = 0; 
    } 

    // Get the sid for the username 
    if (retValue) { 
     char domainbuf[4096]; 
     DWORD sidSize = 0; 
     DWORD bufSize = 4096; 
     SID_NAME_USE sidUse; 
     LookupAccountName(NULL, newuser, sidPtr, &sidSize, domainbuf, &bufSize, &sidUse); 
     sid = (PSID)malloc(sidSize); 
     if (!LookupAccountName(NULL, string, (PSID)sid, &sidSize, domainbuf, &bufSize, &sidUse)) 
      retValue = 0; 
     } 
    } 

    // Set the sid to be the new owner 
    if (retValue && !SetSecurityDescriptorOwner(security, sidPtr, 0)) 
     retValue = 0; 

    // Save the security descriptor 
    if (retValue) 
     retValue = SetFileSecurity(filename, OWNER_SECURITY_INFORMATION, security); 
    if (security) free(security); 
    if (sid) free(sid); 
    return retValue; 

`

+3

पवित्र नरक, सब कुछ एक सरल 'चोटी' कॉल को बदलने के लिए! –

+0

क्या आपने अस्पष्टता के लिए फ़ंक्शन रिटर्न प्रकार, नाम और पैरामीटर को छोड़ दिया था? –

1

आप मिल सकती है cacls or icacls commands उपयोगी ... वे वास्तव में उपयोग करने के लिए हालांकि

आप में थोड़ा और अधिक जानकारी दे सकते हैं सीधा नहीं कर रहे हैं आप क्या करने की कोशिश कर रहे हैं?

संबंधित मुद्दे