123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using System.Runtime.InteropServices;
- using System.Text;
- using System.Threading.Tasks;
- using UnityEngine;
- namespace WindowsHandler
- {
- public class User32API
- {
- private static Hashtable processWnd = null;
- static User32API()
- {
- if (processWnd == null)
- {
- processWnd = new Hashtable();
- }
- }
- [StructLayout(LayoutKind.Sequential)]
- public struct ProcessEntry32
- {
- public uint dwSize;
- public uint cntUsage;
- public uint th32ProcessID;
- public IntPtr th32DefaultHeapID;
- public uint th32ModuleID;
- public uint cntThreads;
- public uint th32ParentProcessID;
- public int pcPriClassBase;
- public uint dwFlags;
- [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
- public string szExeFile;
- }
- //得到窗口客户区大小
- [DllImport("user32.dll")]
- public static extern int GetClientRect(IntPtr hwnd, out Rect lpRect);
- //得到窗口大小和坐标
- [DllImport("user32.dll")]
- public static extern int GetWindowRect(IntPtr hwnd, out Rect lpRect);
- /// <summary>
- /// 一个窗口区域的结构体
- /// </summary>
- public struct Rect
- {
- public int Left;
- public int Top;
- public int Right;
- public int Bottom;
- }
- public delegate bool WNDENUMPROC(IntPtr hwnd, uint lParam);
- [DllImport("user32.dll")]
- public static extern bool ShowWindow(IntPtr hwnd, int nCmdShow);
- [DllImport("user32.dll")]
- public static extern long GetWindowThreadProcessId(IntPtr hWnd, ref uint lpdwProcessId);
- [DllImport("KERNEL32.DLL ")]
- public static extern IntPtr CreateToolhelp32Snapshot(uint flags, uint processid);
- [DllImport("KERNEL32.DLL ")]
- public static extern int CloseHandle(IntPtr handle);
- [DllImport("KERNEL32.DLL ")]
- public static extern int Process32First(IntPtr handle, ref ProcessEntry32 pe);
- [DllImport("KERNEL32.DLL ")]
- public static extern int Process32Next(IntPtr handle, ref ProcessEntry32 pe);
- [DllImport("User32.dll", EntryPoint = "SendMessage")]
- private static extern int SendMessage(int hWnd, int Msg, int wParam, string lParam);
- [DllImport("user32", EntryPoint = "IsWindow")]
- private static extern bool IsWindow(IntPtr hWnd);
- [DllImport("user32.dll")]
- public static extern int SetParent(IntPtr hWndChild, IntPtr hWndParent);
- [DllImport("user32.dll", ExactSpelling = true, CharSet = CharSet.Auto)]
- public static extern IntPtr GetParent(IntPtr hWnd);
- [DllImport("user32.dll", EntryPoint = "SetWindowPos")]
- public static extern bool SetWindowPos(IntPtr hWnd, int hWndInsertAfter,
- int X, int Y, int cx, int cy, uint uFlags);
- [DllImport("kernel32.dll", EntryPoint = "SetLastError")]
- public static extern void SetLastError(uint dwErrCode);
- [DllImport("user32.dll", EntryPoint = "EnumWindows", SetLastError = true)]
- public static extern bool EnumWindows(WNDENUMPROC lpEnumFunc, uint lParam);
- [DllImport("user32.dll", EntryPoint = "FindWindow")]
- public static extern IntPtr FindWindow(string IpClassName, string IpWindowName);
- public static IntPtr GetHandleByProcessName(string ProcessName)
- {
- List<ProcessEntry32> list = new List<ProcessEntry32>();
- IntPtr handle = CreateToolhelp32Snapshot(0x2, 0);
- IntPtr hh = IntPtr.Zero;
- if ((int)handle > 0)
- {
- ProcessEntry32 pe32 = new ProcessEntry32();
- pe32.dwSize = (uint)Marshal.SizeOf(pe32);
- int bMore = Process32First(handle, ref pe32);
-
- while (bMore == 1)
- {
- IntPtr temp = Marshal.AllocHGlobal((int)pe32.dwSize);
- Marshal.StructureToPtr(pe32, temp, true);
- ProcessEntry32 pe = (ProcessEntry32)Marshal.PtrToStructure(temp, typeof(ProcessEntry32));
- Marshal.FreeHGlobal(temp);
- list.Add(pe);
- UnityEngine.Debug.LogError(pe.szExeFile);
- if (pe.szExeFile == ProcessName)
- {
- bMore = 2;
- hh = GetCurrentWindowHandle(pe.th32ProcessID);
- ShowWindow( hh,2);
- Application.Quit();
- break;
- }
- bMore = Process32Next(handle, ref pe32);
- }
- }
- return hh;
- }
- [MonoPInvokeCallback]
- public static IntPtr GetCurrentWindowHandle(uint proid)
- {
- IntPtr ptrWnd = IntPtr.Zero;
- uint uiPid = proid;
- object objWnd = processWnd[uiPid];
- if (objWnd != null)
- {
- ptrWnd = (IntPtr)objWnd;
- if (ptrWnd != IntPtr.Zero && IsWindow(ptrWnd)) // 从缓存中获取句柄
- {
- return ptrWnd;
- }
- else
- {
- ptrWnd = IntPtr.Zero;
- }
- }
-
- bool bResult = EnumWindows(new WNDENUMPROC(EnumWindowsProc), uiPid);
- // 枚举窗口返回 false 并且没有错误号时表明获取成功
- if (!bResult && Marshal.GetLastWin32Error() == 0)
- {
- objWnd = processWnd[uiPid];
- if (objWnd != null)
- {
- ptrWnd = (IntPtr)objWnd;
- }
-
- }
-
-
- return ptrWnd;
- }
- [MonoPInvokeCallback]
- public static bool EnumWindowsProc(IntPtr hwnd, uint lParam)
- {
- uint uiPid = 0;
- if (GetParent(hwnd) == IntPtr.Zero)
- {
- GetWindowThreadProcessId(hwnd, ref uiPid);
- if (uiPid == lParam) // 找到进程对应的主窗口句柄
- {
- processWnd.Add(uiPid, hwnd); // 把句柄缓存起来
- SetLastError(0); // 设置无错误
- return false; // 返回 false 以终止枚举窗口
- }
- }
- return true;
- }
- }
- }
- public class MonoPInvokeCallbackAttribute : Attribute
- {
- public MonoPInvokeCallbackAttribute() { }
- }
|