-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNativeMethods.cs
140 lines (131 loc) · 5.34 KB
/
NativeMethods.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
using System;
using System.Drawing;
using System.Runtime.InteropServices;
namespace Sloth
{
static class NativeMethods
{
public struct LastInputInfo
{
public UInt32 cbSize;
public UInt32 dwTime;
}
[DllImport("User32.dll", CallingConvention = CallingConvention.StdCall, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool ShowWindow([In] IntPtr hWnd, [In] int nCmdShow);
[DllImport("user32.dll")]
public static extern bool GetLastInputInfo(ref LastInputInfo plii);
[DllImport("user32.dll")]
public static extern bool SetCursorPos(int X, int Y);
[DllImport("user32.dll")]
public static extern bool GetCursorPos(out Point lpPoint);
[DllImport("Wtsapi32.dll", CharSet = CharSet.Unicode)]
public static extern bool WTSQuerySessionInformationW(IntPtr hServer, uint SessionId, WTS_INFO_CLASS WTSInfoClass, ref IntPtr ppBuffer, ref uint pBytesReturned);
[DllImport("Wtsapi32.dll", CharSet = CharSet.Unicode)]
public static extern void WTSFreeMemory(IntPtr pMemory);
[DllImport("Kernel32.dll", CharSet = CharSet.Unicode)]
public static extern uint WTSGetActiveConsoleSessionId();
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
public const int MOUSEEVENTF_LEFTDOWN = 0x02;
public const int MOUSEEVENTF_LEFTUP = 0x04;
public const int MOUSEEVENTF_RIGHTDOWN = 0x08;
public const int MOUSEEVENTF_RIGHTUP = 0x10;
public static void MouseClick()
{
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
}
public enum WTS_INFO_CLASS
{
WTSInitialProgram,
WTSApplicationName,
WTSWorkingDirectory,
WTSOEMId,
WTSSessionId,
WTSUserName,
WTSWinStationName,
WTSDomainName,
WTSConnectState,
WTSClientBuildNumber,
WTSClientName,
WTSClientDirectory,
WTSClientProductId,
WTSClientHardwareId,
WTSClientAddress,
WTSClientDisplay,
WTSClientProtocolType,
WTSIdleTime,
WTSLogonTime,
WTSIncomingBytes,
WTSOutgoingBytes,
WTSIncomingFrames,
WTSOutgoingFrames,
WTSClientInfo,
WTSSessionInfo,
WTSSessionInfoEx,
WTSConfigInfo,
WTSValidationInfo, // Info Class value used to fetch Validation Information through the WTSQuerySessionInformation
WTSSessionAddressV4,
WTSIsRemoteSession
}
public enum WTS_CONNECTSTATE_CLASS
{
WTSActive, // User logged on to WinStation
WTSConnected, // WinStation connected to client
WTSConnectQuery, // In the process of connecting to client
WTSShadow, // Shadowing another WinStation
WTSDisconnected, // WinStation logged on without client
WTSIdle, // Waiting for client to connect
WTSListen, // WinStation is listening for connection
WTSReset, // WinStation is being reset
WTSDown, // WinStation is down due to error
WTSInit, // WinStation in initialization
}
[StructLayout(LayoutKind.Sequential)]
public struct WTSINFOEXW
{
public int Level;
public WTSINFOEX_LEVEL_W Data;
}
[StructLayout(LayoutKind.Sequential)]
public struct WTSINFOEX_LEVEL_W
{
public WTSINFOEX_LEVEL1_W WTSInfoExLevel1;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct WTSINFOEX_LEVEL1_W
{
public int SessionId;
public WTS_CONNECTSTATE_CLASS SessionState;
public int SessionFlags;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 33)]
public string WinStationName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 21)]
public string UserName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 18)]
public string DomainName;
public LARGE_INTEGER LogonTime;
public LARGE_INTEGER ConnectTime;
public LARGE_INTEGER DisconnectTime;
public LARGE_INTEGER LastInputTime;
public LARGE_INTEGER CurrentTime;
public uint IncomingBytes;
public uint OutgoingBytes;
public uint IncomingFrames;
public uint OutgoingFrames;
public uint IncomingCompressedBytes;
public uint OutgoingCompressedBytes;
}
[StructLayout(LayoutKind.Explicit)]
public struct LARGE_INTEGER //This structure is used in C++ as the union structure. In C#, you need to use FieldOffset to set the relevant memory start address
{
[FieldOffset(0)]
uint LowPart;
[FieldOffset(4)]
int HighPart;
[FieldOffset(0)]
long QuadPart;
}
}
}