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?
-
5You can't *remotely* logoff from another system. You have to run your logoff code on the local system that you want to logoff from. Which means compiling your code into an executable, uploading that executable to the remote system, and running the executable on that system – Remy Lebeau Sep 21 '22 at 16:04
-
What does current user mean on a remote system? More than one user can be active on a system... – Anders Oct 08 '22 at 12:10
3 Answers
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:
#pragma region
#include<windows.h>
#pragma warning(disable:4996)
BOOL ReSetWindows(DWORD dwFlags, BOOL bForce)
{
if (dwFlags != EWX_LOGOFF && dwFlags != EWX_REBOOT && dwFlags != EWX_SHUTDOWN)
return FALSE;
OSVERSIONINFO osvi = { 0 };
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
if (!GetVersionEx(&osvi))
{
return FALSE;
}
if (osvi.dwPlatformId == VER_PLATFORM_WIN32_NT)
{
//EnableShutDownPriv();
}
dwFlags |= (bForce != FALSE) ? EWX_FORCE : EWX_FORCEIFHUNG;
return ExitWindowsEx(dwFlags, 0);
}
int main()
{
ReSetWindows(EWX_LOGOFF, false);//logoff
//ReSetWindows(EWX_REBOOT, true);//restart
//ReSetWindows(EWX_SHUTDOWN, true);//shutdown
}
======================= Caution!!!Please save your important file before running or running in the virtual machine

- 185
- 5
-
This is only for local machine shutdown , how can use it in remote system ? , Sry, I don't get this one - " write a program that specifies the process ID ", can you pls provide any examples for this one? – Kaarthikeyan K Sep 23 '22 at 12:03
-
1Since you claim that [`ExitWindowsEx`](https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-exitwindowsex) were capable of shutting down a remote system, which of its arguments is used to specify the remote system? – IInspectable Oct 07 '22 at 15:43
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.

- 97,548
- 12
- 110
- 164
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.

- 185
- 5
-
1This does not even attempt to answer the question asked. Clearly down-vote material. – IInspectable Oct 07 '22 at 15:44