2010-02-08 14 views
5

¿Hay una API en Windows similar a la de chown de Linux?Cambiar el propietario del archivo en Windows

+0

El modelo de seguridad de Windows no se basa realmente en la propiedad del archivo de la misma manera que los sistemas de archivos UNIX, por lo que esta no es realmente una herramienta que se necesita con mucha frecuencia. –

Respuesta

3

Tomado de aquí: 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

¡Santo infierno, todo eso para reemplazar una simple llamada 'chown'! –

+0

¿Omitió el tipo de devolución de función, nombre y parámetros por el bien de vaguery? –

1

Usted puede encontrar el cacls or icacls commands útil ... No son exactamente fácil de usar, aunque

¿Puede dar un poco más de información en lo que estás tratando de hacer?

Cuestiones relacionadas