I am looking for an API to logoff current user on remote computer. Two functions ExitWindows and InitiateSystemShutdown seem not exactly what I want. The first one doesn't accept computer name, the second one doesn't have logoff option.is it possible to logoff current user on remote computer ?. Can someone tell me how to achieve this in a C++ program?
Logoff remote system using c++
251 views Asked by Kaarthikeyan K At
3
There are 3 answers
1
On
This is the program specifies the process ID:
DWORD GetProcessIDByName(LPCTSTR szProcessName)
{
STARTUPINFO st;
PROCESS_INFORMATION pi;
PROCESSENTRY32 ps;
HANDLE hSnapshot;
DWORD dwPID = 0;
ZeroMemory(&st, sizeof(STARTUPINFO));
ZeroMemory(&pi, sizeof(PROCESS_INFORMATION));
st.cb = sizeof(STARTUPINFO);
ZeroMemory(&ps, sizeof(PROCESSENTRY32));
ps.dwSize = sizeof(PROCESSENTRY32);
hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hSnapshot == INVALID_HANDLE_VALUE)//
{
return dwPID;
}
if (!Process32First(hSnapshot, &ps))
{
return dwPID;
}
do
{
if (lstrcmpi(ps.szExeFile, szProcessName) == 0)
{
dwPID = ps.th32ProcessID;
}
} while (Process32Next(hSnapshot, &ps));
CloseHandle(hSnapshot);
return dwPID;//
}
You need this line of code in your main function:
DWORD pId = GetProcessIDByName("\\\.exe");
Closing remote system by local machine is easy. Closing local machine by remote machine sounds like a virus. No offense, it is difficult to implement. Maybe you can try using socket to communicate local machine with virtual machine.
0
On
WTSLogoffSession? but the concept of a current user on a remote machine does not really exist, you would have to inspect the sessions with WTSEnumerateSessions+WTSQuerySessionInformation if you want to find a specific user. This only makes sense in a environment where there is a NT domain so you can match against a domain SID. Without a domain, all you can do is match against the username which might be enough for you.
I knew that you want to shutdown system by using exitwindows function. However, if you want to shut down the remote system in your own process, you need to use the exitwindowsEX function and write a program that specifies the process ID. The relevant function references are as follows: https://learn.microsoft.com/zh-cn/windows/win32/shutdown/how-to-shut-down-the-system The following are specific codes:
======================= Caution!!!Please save your important file before running or running in the virtual machine