All pastes #496147 Raw Edit

Unnamed

public text v1 · immutable
·published 2007-05-19 02:37 UTC
rendered paste body
bool TakeOwnership(LPTSTR lpszOwnFile, PSID pSIDOwner);

int main()
{
	if (TakeOwnership(L"test.txt", "testuser") )
	{
		printf("Worked like a champ\n");
		return 0;
	}
	else
	{
		printf("Epic fail\n");
		return 1;
	}
}


bool TakeOwnership(LPTSTR lpszOwnFile, PSID pSIDOwner) 
{

    bool bRetval = FALSE;

    PACL pACL = NULL;
	DWORD dwRes;

    dwRes = SetNamedSecurityInfo(
        lpszOwnFile,                 // name of the object
        SE_FILE_OBJECT,              // type of object
        OWNER_SECURITY_INFORMATION,  // change only the object's DACL
        pSIDOwner, NULL,            // do not change owner or group
        pACL,                        // DACL specified
        NULL);                       // do not change SACL

    if (ERROR_SUCCESS == dwRes) 
    {
        printf("Successfully changed DACL\n");
        bRetval = TRUE;
        // No more processing needed.
        goto Cleanup;
    }

Cleanup:

    if (pSIDOwner)
        FreeSid(pSIDOwner); 

    if (pACL)
       LocalFree(pACL);

    return bRetval;

}