03 июля 2010

Qt and get current user name

Used as qt script function wrapper:
// Crosplatform getUserName function
#ifdef Q_WS_WIN
#include <windows.h>
#include <lmcons.h>
#elif defined Q_WS_X11
#include <pwd.h>
#endif //#ifdef Q_WS_WIN

QScriptValue getLoginWrapper(QScriptContext* context, QScriptEngine* engine)
  QString userName;
#ifdef Q_WS_WIN
  WCHAR* lpszSystemInfo; // pointer to system information
  DWORD cchBuff = 256; // size of user name
  WCHAR tchBuffer[UNLEN + 1]; // buffer for expanded string

  lpszSystemInfo = tchBuffer;

  // Get and display the user name.
  GetUserNameW(lpszSystemInfo, &cchBuff);

  //Unicode string needs to be converted
  userName = QString::fromWCharArray(lpszSystemInfo);
#elif defined Q_WS_X11
  register struct passwd *pw;
  pw = getpwuid(getuid());
  if (pw)
  userName = pw->pw_name;
#endif //#ifdef Q_WS_WIN

  return userName;

Комментариев нет:

Отправить комментарий