2010-02-08 4 views
5

Existe-t-il une API dans Windows similaire à chown de Linux?Modifier le propriétaire du fichier dans Windows

+0

Le modèle de sécurité Windows n'est pas vraiment basé sur la propriété des fichiers, contrairement aux systèmes de fichiers UNIX. Ce n'est donc pas un outil très souvent nécessaire. –

Répondre

3

glanées ici: 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

Saint-Hell, tout ça pour remplacer un simple appel «chown»! –

+0

Avez-vous omis le type de retour de la fonction, le nom et les paramètres pour des raisons de vaguerie? –

1

Vous pourriez trouver utile cacls or icacls commands ... Ils ne sont pas exactement simple à utiliser si

Pouvez-vous fournir un peu plus d'informations sur ce que vous essayez de faire?

Questions connexes