62,243
社区成员




internal static class NativeMethods
{
// WIN32 API
[DllImport("kernel32.dll")]
internal static extern IntPtr GetCurrentThread();
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("advapi32.dll", SetLastError = true)]
internal static extern bool ImpersonateSelf(int level);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("advapi32.dll", SetLastError = true)]
internal static extern bool OpenThreadToken(IntPtr thread, uint desiredAccess, [MarshalAs(UnmanagedType.Bool)] bool openAsSelf, ref IntPtr handle);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("advapi32.dll", SetLastError = true)]
internal static extern bool RevertToSelf();
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("advapi32.dll", SetLastError = true)]
internal static extern bool SetThreadToken(IntPtr thread, IntPtr token);
}
....
IntPtr currentToken=IntPtr.Zero;
IntPtr currentThread = NativeMethods.GetCurrentThread();
NativeMethods.OpenThreadToken(currentThread, 4, true, ref currentToken);
NativeMethods.SetThreadToken(IntPtr.Zero, IntPtr.Zero);
NativeMethods.ImpersonateSelf(2);
Process proc = new Process();
proc.StartInfo.FileName = "***.exe";
proc.start();
string output=proc.StandardOutput.ReadToEnd();
NativeMethods.RevertToSelf();
NativeMethods.SetThreadToken(IntPtr.Zero, currentToken);
return output;
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("advapi32.dll", SetLastError=true)]
internal static extern bool ImpersonateSelf(int level);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("advapi32.dll", SetLastError=true)]
internal static extern bool RevertToSelf();
...
ImpersonateSelf(2)
Process proc = new Process();
proc.StartInfo.FileName = "***.exe";
proc.start();
string output=proc.StandardOutput.ReadToEnd();
RevertToSelf()
return output;