-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ADB: 大规模重构,使用Win32黑科技让主程序启动的ADB服务器能够在主程序关闭时被系统关闭
- Loading branch information
Rcmcpe
committed
Nov 12, 2020
1 parent
f09d65f
commit 25389fd
Showing
5 changed files
with
190 additions
and
33 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
|
||
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1"> | ||
<assemblyIdentity version="1.0.0.0" name="MyApplication.app" /> | ||
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2"> | ||
<security> | ||
<requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3"> | ||
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" /> | ||
</requestedPrivileges> | ||
</security> | ||
</trustInfo> | ||
<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1"> | ||
<application> | ||
<!--To support job objects --> | ||
<supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}" /> | ||
<supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}" /> | ||
</application> | ||
</compatibility> | ||
</assembly> |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
using System; | ||
using System.ComponentModel; | ||
using System.Runtime.InteropServices; | ||
|
||
// ReSharper disable FieldCanBeMadeReadOnly.Global | ||
|
||
namespace REVUnit.AutoArknights.Core | ||
{ | ||
/// <summary> | ||
/// 用来关闭ADB的黑科技。 | ||
/// </summary> | ||
public class Job | ||
{ | ||
private readonly IntPtr _handle; | ||
|
||
public Job() | ||
{ | ||
_handle = CreateJobObject(IntPtr.Zero, Guid.NewGuid().ToString()); | ||
|
||
var info = new JOBOBJECT_BASIC_LIMIT_INFORMATION { LimitFlags = 0x2000 }; | ||
var extendedInfo = new JOBOBJECT_EXTENDED_LIMIT_INFORMATION { BasicLimitInformation = info }; | ||
|
||
int length = Marshal.SizeOf(typeof(JOBOBJECT_EXTENDED_LIMIT_INFORMATION)); | ||
IntPtr extendedInfoPtr = Marshal.AllocHGlobal(length); | ||
|
||
try | ||
{ | ||
Marshal.StructureToPtr(extendedInfo, extendedInfoPtr, false); | ||
if (!SetInformationJobObject(_handle, 9, extendedInfoPtr, | ||
(uint) length)) | ||
throw new Win32Exception(Marshal.GetLastWin32Error()); | ||
} | ||
finally | ||
{ | ||
Marshal.FreeHGlobal(extendedInfoPtr); | ||
} | ||
} | ||
|
||
public void AddProcess(IntPtr handle) | ||
{ | ||
AssignProcessToJobObject(_handle, handle); | ||
} | ||
|
||
[DllImport("kernel32.dll", CharSet = CharSet.Unicode)] | ||
private static extern IntPtr CreateJobObject(IntPtr lpJobAttributes, string? name); | ||
|
||
[DllImport("kernel32.dll")] | ||
private static extern bool SetInformationJobObject(IntPtr job, int infoType, | ||
IntPtr lpJobObjectInfo, uint cbJobObjectInfoLength); | ||
|
||
[DllImport("kernel32.dll", SetLastError = true)] | ||
private static extern bool AssignProcessToJobObject(IntPtr job, IntPtr process); | ||
} | ||
|
||
[StructLayout(LayoutKind.Sequential)] | ||
internal struct IO_COUNTERS | ||
{ | ||
public ulong ReadOperationCount; | ||
public ulong WriteOperationCount; | ||
public ulong OtherOperationCount; | ||
public ulong ReadTransferCount; | ||
public ulong WriteTransferCount; | ||
public ulong OtherTransferCount; | ||
} | ||
|
||
[StructLayout(LayoutKind.Sequential)] | ||
internal struct JOBOBJECT_BASIC_LIMIT_INFORMATION | ||
{ | ||
public long PerProcessUserTimeLimit; | ||
public long PerJobUserTimeLimit; | ||
public uint LimitFlags; | ||
public UIntPtr MinimumWorkingSetSize; | ||
public UIntPtr MaximumWorkingSetSize; | ||
public uint ActiveProcessLimit; | ||
public UIntPtr Affinity; | ||
public uint PriorityClass; | ||
public uint SchedulingClass; | ||
} | ||
|
||
[StructLayout(LayoutKind.Sequential)] | ||
internal struct JOBOBJECT_EXTENDED_LIMIT_INFORMATION | ||
{ | ||
public JOBOBJECT_BASIC_LIMIT_INFORMATION BasicLimitInformation; | ||
public IO_COUNTERS IoInfo; | ||
public UIntPtr ProcessMemoryLimit; | ||
public UIntPtr JobMemoryLimit; | ||
public UIntPtr PeakProcessMemoryUsed; | ||
public UIntPtr PeakJobMemoryUsed; | ||
} | ||
} |