2018年8月9日 星期四

[C++] Detection login or unlogin session by service [2/2]


In receiving window massage, it is necessary to detect the status before booting into the OS.Why do I need to detect it outside because the window massage does not receive any state before sending the message, so I want to get the status that has not been send out by event.

In the threadMain1, We want the do Listen Window Massage.
DWORD WINAPI thread_class::threadMain1(LPVOID param)
{
 thread_class* This = static_cast<thread_class*>(param);
 This->ListenStatusChange();
 return 0;
}

By the ListenStatusChange
VOID thread_class::ListenStatusChange()
{
 //If not login in window 30 sec.
 WTS_Class wts;
 //Read OS user account wts.w_UserName
 wts.ReadConfig(wts.w_UserName);
 while (true)
 {
  bool rv = false;
  rv = wts.IsLogonbyUser(wts.w_UserName, WTSActive);
  if (!rv)
  {
   OutputDebugString(_T("If you have not logged in for 30 seconds");
   Sleep(30000);
   rv = wts.IsLogonbyUser(wts.w_UserName, WTSActive);
   if (!rv)
   {
    OutputDebugString(_T("Which user...");
   }
  }
  else
  {
   break;
  }
  Sleep(30000);
 }
 return VOID();
}

Specified the connection state of Specified the connection state.
We want to do designated account is logged on to the WinStation(WTSActive).
You can select other WTS_CONNECTSTATE_CLASS items, Get the message you want, but before you enter the OS, some connection state is not available.
By the WTS_Class IsLogonbyUser
BOOL WTS_Class::IsLogonbyUser(std::wstring UserName ,WTS_CONNECTSTATE_CLASS WST_Tpye)
{
 if (UserName.empty())
 {
  OutputDebugString(_T("UserName is empty");
 }
 PWTS_SESSION_INFO ppSessionInfo = NULL;
 DWORD     pCount = 0;
 WTS_SESSION_INFO  wts;

 WTSEnumerateSessions(WTS_CURRENT_SERVER_HANDLE,
  0,
  1,
  &ppSessionInfo,
  &pCount); // pCount - Pointer to the variable that receives
      // the number of WTS_SESSION_INFO structures
      // returned in the ppSessionInfo buffer

 struct SessionInfo
 {
  std::wstring SessionName;
  std::wstring UserName;
  WTS_CONNECTSTATE_CLASS SessionType;
 };
 
 std::vector  SessionDataList;
 
 for (DWORD i = 0; i < pCount; i++)
 {
  SessionInfo tamplateData;
  wts = ppSessionInfo[i];
  DWORD     TSSessionId = wts.SessionId;
  LPTSTR     TSpWinStationName = wts.pWinStationName;
  WTS_CONNECTSTATE_CLASS TSState = wts.State;

  //ByPass Services
  if (_wcsicmp(TSpWinStationName, L"Services") == 0)
  {
   continue;
  }
  tamplateData.SessionName = (std::wstring)TSpWinStationName;
  tamplateData.SessionType = TSState;
  tamplateData.UserName = GetWTSInfo(i, WTSUserName);
  SessionDataList.push_back(tamplateData);
 }

 for (size_t Number = 0; Number < SessionDataList.size(); Number++)
 {
  if (_wcsicmp(UserName.c_str(), SessionDataList[Number].UserName.c_str()) == 0 &&
   WST_Tpye == SessionDataList[Number].SessionType)
  {
   return true;
  }
 }
 return false;
}

沒有留言:

張貼留言