Use basic window MSDN API.
Basic on [C++] Build Services by window.
When the service is completed, the window message is received in the sub thread, so we create two thread.
DWORD WINAPI ServiceSubThread(LPVOID lpParam)
{
OutputDebugString(_T("=================Service Thread Start========================");
thread_class c_thread;
HANDLE m_threads[THREAD_MAIN_ID] = {};
DWORD threadIDs[THREAD_MAIN_ID] = {};
LPTHREAD_START_ROUTINE threadProcs[THREAD_MAIN_ID] = { c_thread.threadMain0, c_thread.threadMain1 };
DWORD_PTR mask = 0;
for (int i = 0; i < THREAD_MAIN_ID; ++i)
{
m_threads[i] = CreateThread(NULL, 0, threadProcs[i], &c_thread, CREATE_SUSPENDED, &threadIDs[i]);
mask = 1 << i;
SetThreadAffinityMask(m_threads[i], mask);
}
for (int i = 0; i < THREAD_MAIN_ID; ++i)
{
ResumeThread(m_threads[i]);
}
WaitForMultipleObjects(THREAD_MAIN_ID, m_threads, TRUE, INFINITE);
for (int i = 0; i < THREAD_MAIN_ID; ++i)
{
CloseHandle(m_threads[i]);
}
OutputDebugString(_T("=================Service Thread End========================");
return ERROR_SUCCESS;
}
In the threadMain0, We want the do Listen Window Massage.
DWORD WINAPI thread_class::threadMain0(LPVOID param)
{
thread_class* This = static_cast<thread_class>(param);
This->ListenSsession();
return 0;
}
The threadMain1 go to see [C++] Detection login or unlogin session by service [2/2].By the ListenSsession, create contains window class by WNDCLASSEX, And creates an overlapped by CreateWindowEx, then registers the specified window to receive session to know which status by login or other.
VOID thread_class::ListenSsession()
{
try
{
HWND hwndMain;
//you need init WNDCLASSEX create MainWndProc
InitWindowClass();
//you need create CreateWindowEx
CreateWindowHWND(w_hInstance, hwndMain);
bool rv = false;
rv = WTSRegisterSessionNotification(hwndMain, NOTIFY_FOR_ALL_SESSIONS);
if (!rv)
{
OutputDebugString(_T("Registers the specified window to receive session Fail");
}
MSG msg = MSG();
while (true)
{
ListenStart(msg);
}
}
catch (std::exception ex)
{
wprintf(L"Exception : %s", ex.what());
}
return VOID();
}
By the MainWndProc, by the WM_WTSSESSION_CHANGE to notifies applications of changes in session state.
LRESULT CALLBACK MainWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_WTSSESSION_CHANGE:
{
switch (wParam)
{
case WTS_CONSOLE_CONNECT:
OutputDebugString(_T("CONSOLE_CONNECT");
break;
case WTS_CONSOLE_DISCONNECT:
OutputDebugString(_T("CONSOLE_DISCONNECT");
break;
case WTS_REMOTE_CONNECT:
OutputDebugString(_T("REMOTE_CONNECT");
break;
case WTS_REMOTE_DISCONNECT:
OutputDebugString(_T("REMOTE_DISCONNECT");
break;
case WTS_SESSION_LOGON:
OutputDebugString(_T("SESSION_LOGON");
break;
case WTS_SESSION_LOGOFF:
OutputDebugString(_T("SESSION_LOGOFF");
break;
case WTS_SESSION_LOCK:
OutputDebugString(_T("SESSION_LOCK");
break;
case WTS_SESSION_UNLOCK:
OutputDebugString(_T("SESSION_UNLOCK");
break;
case WTS_SESSION_REMOTE_CONTROL:
OutputDebugString(_T("SESSION_REMOTE_CONTROL");
break;
default:
break;
}
}
default:
// Call the default window handler
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
沒有留言:
張貼留言