Home > database >  Logoff remote system using c
Logoff remote system using c

Time:09-26

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?

CodePudding user response:

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

CodePudding user response:

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.

  • Related