windows下获得当前用户名的小代码,ring3级别
#include <windows.h> #include <WtsApi32.h> #pragma comment(lib, "WtsApi32.lib") #define Message(X) MessageBox(0, X, "DEBUG", MB_ICONINFORMATION) int getUserName(char *username) { LPTSTR pBuffer = NULL; DWORD dwBufferLen; BOOL bRes = WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE, WTS_CURRENT_SESSION, WTSUserName, &pBuffer, &dwBufferLen); if (bRes == FALSE) return 0; strcpy(username, pBuffer); WTSFreeMemory(pBuffer); Message(username); return 1; } int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow) { char usrName[200]; getUserName(usrName); }
在XP中的运行结果如下: