The macroscopic entropy and the attractor equations for BPS black holes in four-dimensional N=2 supergravity theories follow from a variational principle for a certain `entropy function. We present this function in the presence of R^2-interactions and non-holomorphic corrections. The variational principle identifies the entropy as a Legendre transform and this motivates the definition of various partition functions corresponding to different ensembles and a hierarchy of corresponding duality invariant inverse Laplace integral representations for the microscopic degeneracies. Whenever the microscopic degeneracies are known the partition functions can be evaluated directly. This is the case for N=4 heterotic CHL black holes, where we demonstrate that the partition functions are consistent with the results obtained on the macroscopic side for black holes that have a non-vanishing classical area. In this way we confirm the presence of a measure in the duality invariant inverse Laplace integrals. Most, but not all, of these results are obtained in the context of semiclassical approximations. For black holes whose area vanishes classically, there remain discrepancies at the semiclassical level and beyond, the nature of which is not fully understood at present.