Dyndrilliac you have to close hToken even if return value is 'true' since the handle was opened inside that function.
Not being snide ;p My code shared same issue.
Code:
BOOL EnableDebugPrivledges( void )
{
HANDLE hToken;
LUID Luid;
TOKEN_PRIVILEGES tpToken;
BOOL bRet = FALSE;
// enable the SeDebugPrivilege
if( 0 != OpenProcessToken( GetCurrentProcess( ) ,TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken ) &&
0 != LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &Luid ) )
{
tpToken.PrivilegeCount = 1;
tpToken.Privileges[ 0 ].Luid = Luid;
tpToken.Privileges[ 0 ].Attributes = SE_PRIVILEGE_ENABLED;
bRet = 0 != AdjustTokenPrivileges( hToken, FALSE, &tpToken, NULL, NULL, NULL );
CloseHandle( hToken );
}
return bRet;
}
In my ver, If LookupPrivilegeValue fails it will just jmp over and not close the handle.