using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Security.Principal;
using System.Management;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace TDHost
{
public partial class ToolRemoteProtect : Form
{
public ToolRemoteProtect()
{
InitializeComponent();
}
public struct USER_INFO_0
{
public string Username;
}
[DllImport("Netapi32.dll")]
extern static int NetUserEnum(
[MarshalAs(UnmanagedType.LPWStr)]
string servername,
int level,
int filter,
out IntPtr bufptr,
int prefmaxlen,
out int entriesread,
out int totalentries,
out int resume_handle);
[DllImport("Netapi32.dll")]
extern static int NetApiBufferFree(IntPtr Buffer);
private void button1_Click(object sender, EventArgs e)
{
string name = System.Environment.UserName;
string name1 = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
MessageBox.Show(name1);
Console.WriteLine("处理器数量:" + Environment.ProcessorCount);
Console.WriteLine("操作系统版本:" + Environment.OSVersion);
Console.WriteLine("公共语言运行时版本(CLR):" + Environment.Version);
Console.WriteLine("机器名:" + Environment.MachineName);
Console.WriteLine("用户名:" + Environment.UserName);
GetCurrentUser();
AppDomain domain = Thread.GetDomain();
domain.SetPrincipalPolicy(System.Security.Principal.PrincipalPolicy.WindowsPrincipal);
WindowsPrincipal principal = (WindowsPrincipal)Thread.CurrentPrincipal;
bool User = principal.IsInRole(WindowsBuiltInRole.User);
if (User)
{
MessageBox.Show("普通用户");
}
else
{
MessageBox.Show("不是普通用户");
}
}
private static string GetProcessUserName(int pID)
{
string theUser = null;
SelectQuery query1 = new SelectQuery("Select * from Win32_Process WHERE processID=" + pID);
ManagementObjectSearcher searcher1 = new ManagementObjectSearcher(query1);
try
{
foreach (ManagementObject disk in searcher1.Get())
{
ManagementBaseObject inPar = null;
ManagementBaseObject outPar = null;
inPar = disk.GetMethodParameters("GetOwner");
outPar = disk.InvokeMethod("GetOwner", inPar, null);
theUser = outPar["User"].ToString();
break;
}
}
catch
{
theUser = "SYSTEM";
}
return theUser;
}
public static string GetCurrentUser()
{
string currentUser = null;
foreach (Process p in Process.GetProcesses())
{
currentUser = GetProcessUserName(p.Id);
if (currentUser != "SYSTEM" && currentUser != null && currentUser != string.Empty)
break;
}
return currentUser;
}
private void button2_Click(object sender, EventArgs e)
{
int EntriesRead;
int TotalEntries;
int Resume;
IntPtr bufPtr;
NetUserEnum(null, 0, 2, out bufPtr, -1, out EntriesRead,
out TotalEntries, out Resume);
if (EntriesRead > 0)
{
USER_INFO_0[] Users = new USER_INFO_0[EntriesRead];
IntPtr iter = bufPtr;
for (int i = 0; i < EntriesRead; i++)
{
Users[i] = (USER_INFO_0)Marshal.PtrToStructure(iter,
typeof(USER_INFO_0));
iter = (IntPtr)((int)iter + Marshal.SizeOf(typeof(USER_INFO_0)));
textBox1.AppendText(Users[i].Username + "\r\n");
}
NetApiBufferFree(bufPtr);
}
}
}
}