ExpandEnvironmentStringsForUser() API does NOT expand the environment variable %USERNAME% on Windows 7.

ExpandEnvironmentStringsForUser() API does not expand the environment variable %USERNAME% on Windows 7 and Windows Server 2008 R2, if hToken value is obtained  only by using TOKEN_IMPERSONATE | TOKEN_QUERY flags for OpenProcessToken.  To expand the environment variables like %USERNAME%  on Windows 7 & Windows Server 2008 R2, you also need to add TOKEN_DUPLICATE as well. 

For application that may run on all versions of Windows, it is recommended that you obtain the token handle with all 3 access rights: TOKEN_QUERY, TOKEN_DUPLICATE and TOKEN_IMPERSONATE

Example:

  
 OpenProcessToken(hProcess,TOKEN_IMPERSONATE|TOKEN_QUERY|TOKEN_DUPLICATE,& 
 hProcessToken);
 if (ret == FALSE)
 return GetLastError();
 ExpandEnvironmentStringsForUser(hProcessToken, L"%USERNAME%", (LPWSTR)envBuf, 
 ENV_BUF_SIZE);

-Shree

Windows SDK