using PTMedicalInsurance.Variables;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace PTMedicalInsurance.Common
{
class WinApi
{
private bool IsWindowExist(IntPtr handle)
{
return (!(GetWindow(new HandleRef(this, handle), 4) != IntPtr.Zero) && IsWindowVisible(new HandleRef(this, handle)));
}
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern IntPtr GetWindow(HandleRef hWnd, int uCmd);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern bool IsWindowVisible(HandleRef hWnd);
///
/// 获取应用程序窗口句柄
///
///
///
private IntPtr GetWindowHandle(int processId)
{
var windowHandle = IntPtr.Zero;
EnumThreadWindowsCallback windowsCallback = new EnumThreadWindowsCallback(FindMainWindow);
EnumWindows(windowsCallback, IntPtr.Zero);
//保持循环
GC.KeepAlive(windowsCallback);
bool FindMainWindow(IntPtr handle, IntPtr extraParameter)
{
int num;
GetWindowThreadProcessId(new HandleRef(this, handle), out num);
if (num == processId && IsWindowExist(handle))
{
windowHandle = handle;
return true;
}
return false;
}
return windowHandle;
}
public delegate bool EnumThreadWindowsCallback(IntPtr hWnd, IntPtr lParam);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool EnumWindows(EnumThreadWindowsCallback callback, IntPtr extraData);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern int GetWindowThreadProcessId(HandleRef handle, out int processId);
public static IntPtr GetWindowHandle(string name)
{
Process[] procs = Process.GetProcessesByName(name);
if (procs.Length != 0)
{
return procs[0].MainWindowHandle;
}
return IntPtr.Zero;
}
public static IntPtr GetWindowsHandle(string WindowName)
{
IntPtr hwnd = IntPtr.Zero;
Process[] procs = Process.GetProcessesByName(WindowName);
if (procs.Length != 0)
{
hwnd = procs[0].MainWindowHandle;
//MessageBox.Show("??" + procs[0].Id.ToString() + "??" + procs[0].MainWindowHandle.ToString());
//Global.writeLog("GetWindowsHandle=>ProcessID:" + procs[0].Id + ",ManagedThreadId:" + Thread.CurrentThread.ManagedThreadId.ToString());
//Global.writeLog("GetWindowsHandle=>CurrentProcessID:" + Process.GetCurrentProcess().Id);
}
//Process ps = procs[0];
//Global.writeLog("Start");
//for (int i = 0; i < ps.Threads.Count; i++)
//{
// var thread = ps.Threads[i];
// string str = string.Empty;
// str = str + "标识符(" + i.ToString() + "):"+ thread.Id.ToString() + System.Environment.NewLine;
// //str = str + "标识符:" + thread. + System.Environment.NewLine;
// str = str +"基本优先级:" + thread.BasePriority.ToString() + System.Environment.NewLine;
// str = str +"当前优先级:" + thread.CurrentPriority.ToString() + System.Environment.NewLine;
// str = str +"内存地址:" + thread.StartAddress.ToInt32() + System.Environment.NewLine;
// str = str +"启动时间:" + thread.StartTime.ToString() + System.Environment.NewLine;
// str = str +"使用时间:" + thread.UserProcessorTime.ToString() + System.Environment.NewLine;
// str = str + "当前状态:";
// switch (thread.ThreadState)
// {
// case System.Diagnostics.ThreadState.Initialized:
// str = str +"线程已经初始化但尚未启动";
// break;
// case System.Diagnostics.ThreadState.Ready:
// str = str +"线程准备在下一个可用的处理器上运行";
// break;
// case System.Diagnostics.ThreadState.Running:
// str = str +"当前正在使用处理器";
// break;
// case System.Diagnostics.ThreadState.Standby:
// str = str +"线程将要使用处理器";
// break;
// case System.Diagnostics.ThreadState.Terminated:
// str = str +"线程已完成执行并退出";
// break;
// case System.Diagnostics.ThreadState.Transition:
// str = str +"线程在可以执行钱等待处理器之外的资源";
// break;
// case System.Diagnostics.ThreadState.Unknown:
// str = str +"状态未知";
// break;
// case System.Diagnostics.ThreadState.Wait:
// str = str +"正在等待外围操作完成或者资源释放";
// break;
// default:
// break;
// }
// if (thread.ThreadState == System.Diagnostics.ThreadState.Wait)
// {
// str = str + System.Environment.NewLine +"等待原因:";
// switch (thread.WaitReason)
// {
// case ThreadWaitReason.EventPairHigh:
// str = str +"线程正在等待事件对高";
// break;
// case ThreadWaitReason.EventPairLow:
// str = str +"线程正在等待事件对低";
// break;
// case ThreadWaitReason.ExecutionDelay:
// str = str +"线程执行延迟";
// break;
// case ThreadWaitReason.Executive:
// str = str +"线程正在等待计划程序";
// break;
// case ThreadWaitReason.FreePage:
// str = str +"线程正在等待可用的虚拟内存页";
// break;
// case ThreadWaitReason.LpcReceive:
// str = str +"线程正在等待本地过程调用到达";
// break;
// case ThreadWaitReason.LpcReply:
// str = str +"线程正在等待对本地过程调用的回复到达";
// break;
// case ThreadWaitReason.PageIn:
// str = str +"线程正在等待虚拟内存页到达内存";
// break;
// case ThreadWaitReason.PageOut:
// str = str +"线程正在等待虚拟内存页写入磁盘";
// break;
// case ThreadWaitReason.Suspended:
// str = str +"线程执行暂停";
// break;
// case ThreadWaitReason.SystemAllocation:
// str = str +"线程正在等待系统分配";
// break;
// case ThreadWaitReason.Unknown:
// str = str +"线程因位置原因而等待";
// break;
// case ThreadWaitReason.UserRequest:
// str = str +"线程正在等待用户请求";
// break;
// case ThreadWaitReason.VirtualMemory:
// str = str +"线程正在等待系统分配虚拟内存";
// break;
// default:
// break;
// }
// }
// Global.writeLog("ThreadsInfo:" + procs[0].Id.ToString() + "_" + procs[0].ProcessName, "Count:" + ps.Threads.Count.ToString(), str);
//}
//Global.writeLog("End");
//foreach (Process process in procs)
//{
// if (process.ProcessName.Contains(WindowName))
// {
// MessageBox.Show($"Process_Name = ({process.ProcessName}), Id = {process.Id}");
// }
//}
//return GetCurrentWindowHandle((uint)procs[0].Id);
//Process[] processes = Process.GetProcesses();
//foreach (Process process in processes)
//{
// if (process.ProcessName.Contains(WindowName))
// {
// MessageBox.Show($"ProcessName = ({process.ProcessName}), Id = {process.Id}");
// }
//}
return hwnd;
}
private static Hashtable processWnd = null;
public delegate bool WNDENUMPROC(IntPtr hwnd, uint lParam);
//static User32API()
//{
// if (processWnd == null)
// {
// processWnd = new Hashtable();
// }
//}
[DllImport("user32.dll", EntryPoint = "EnumWindows", SetLastError = true)]
public static extern bool EnumWindows(WNDENUMPROC lpEnumFunc, uint lParam);
[DllImport("user32.dll", EntryPoint = "GetParent", SetLastError = true)]
public static extern IntPtr GetParent(IntPtr hWnd);
[DllImport("user32.dll", EntryPoint = "GetWindowThreadProcessId")]
public static extern uint GetWindowThreadProcessId(IntPtr hWnd, ref uint lpdwProcessId);
[DllImport("user32.dll", EntryPoint = "IsWindow")]
public static extern bool IsWindow(IntPtr hWnd);
[DllImport("user32.dll", EntryPoint = "EnableWindow")]
public static extern bool EnableWindow(IntPtr hWnd, bool bEnable);
[DllImport("user32.dll", EntryPoint = "IsWindowEnabled")]
public static extern bool IsWindowEnabled(IntPtr hWnd);
[DllImport("user32.dll", EntryPoint = "SetWindowPos")]
public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
[DllImport("user32.dll", EntryPoint = "SetForegroundWindow")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
[DllImport("user32.dll")]
private static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
[DllImport("user32.dll")]
private static extern bool EnumChildWindows(IntPtr hWndParent, WNDENUMPROC lpEnumFunc, IntPtr lParam);
private static IntPtr _comWindowHandle = IntPtr.Zero;
private static string _comWindowTitle = "";
///
/// 查找银海COM读卡窗口并置顶
///
/// 父窗口句柄(当前窗体)
/// 找到的窗口句柄
public static IntPtr FindAndSetTopComWindow(IntPtr parentHwnd)
{
_comWindowHandle = IntPtr.Zero;
_comWindowTitle = "";
// 枚举当前窗体的子窗口
EnumChildWindows(parentHwnd, (hwnd, lParam) =>
{
// 使用更大的缓冲区获取完整标题
StringBuilder title = new StringBuilder(512);
GetWindowText(hwnd, title, 512);
string windowTitle = title.ToString().Trim();
// 检查窗口是否可见
if (!IsWindowVisible(new HandleRef(null, hwnd)))
{
return true; // 不可见窗口跳过
}
// 银海读卡窗口标题包含"社会保障卡"或"统一鉴权"
if (windowTitle.Contains("社会保障卡") || windowTitle.Contains("统一鉴权") ||
windowTitle.Contains("电子社保卡"))
{
_comWindowHandle = hwnd;
_comWindowTitle = windowTitle;
// 置顶窗口
SetWindowPos(hwnd, new IntPtr(-1), 0, 0, 0, 0, 0x0001 | 0x0002 | 0x0040); // SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW
SetForegroundWindow(hwnd);
return false; // 停止枚举
}
// 备选:按类名查找
StringBuilder className = new StringBuilder(256);
GetClassName(hwnd, className, 256);
string[] yinhaiClassNames = { "YHCardReader", "YHCard", "CardReader", "ReadCard", "#32770" };
foreach (var name in yinhaiClassNames)
{
if (className.ToString().Contains(name) || className.ToString().Contains("YH") || className.ToString().Contains("Card"))
{
_comWindowHandle = hwnd;
_comWindowTitle = windowTitle;
// 置顶窗口
SetWindowPos(hwnd, new IntPtr(-1), 0, 0, 0, 0, 0x0001 | 0x0002 | 0x0040);
SetForegroundWindow(hwnd);
return false; // 停止枚举
}
}
return true; // 继续枚举
}, IntPtr.Zero);
return _comWindowHandle;
}
///
/// 查找银海COM读卡窗口并置顶(枚举系统所有窗口)
///
/// 父窗口句柄(用于参考,可传0)
/// 找到的窗口句柄
public static IntPtr FindAndSetTopComWindowAll(IntPtr parentHwnd)
{
_comWindowHandle = IntPtr.Zero;
_comWindowTitle = "";
// 枚举系统中所有顶级窗口
EnumWindows((hwnd, lParam) =>
{
// 使用更大的缓冲区获取完整标题
StringBuilder title = new StringBuilder(512);
GetWindowText(hwnd, title, 512);
string windowTitle = title.ToString().Trim();
// 检查窗口是否可见
if (!IsWindowVisible(new HandleRef(null, hwnd)))
{
return true; // 不可见窗口跳过
}
// 银海读卡窗口标题包含"社会保障卡"或"统一鉴权"
if (windowTitle.Contains("社会保障卡") || windowTitle.Contains("统一鉴权") ||
windowTitle.Contains("电子社保卡"))
{
_comWindowHandle = hwnd;
_comWindowTitle = windowTitle;
// 置顶窗口
SetWindowPos(hwnd, new IntPtr(-1), 0, 0, 0, 0, 0x0001 | 0x0002 | 0x0040); // SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW
SetForegroundWindow(hwnd);
return false; // 停止枚举
}
// 备选:按类名查找
StringBuilder className = new StringBuilder(256);
GetClassName(hwnd, className, 256);
string[] yinhaiClassNames = { "YHCardReader", "YHCard", "CardReader", "ReadCard" };
foreach (var name in yinhaiClassNames)
{
if (className.ToString().Contains(name) || className.ToString().Contains("YH"))
{
_comWindowHandle = hwnd;
_comWindowTitle = windowTitle;
// 置顶窗口
SetWindowPos(hwnd, new IntPtr(-1), 0, 0, 0, 0, 0x0001 | 0x0002 | 0x0040);
SetForegroundWindow(hwnd);
return false; // 停止枚举
}
}
return true; // 继续枚举
}, IntPtr.Zero);
return _comWindowHandle;
}
///
/// 获取最后找到的COM窗口标题
///
public static string GetComWindowTitle()
{
return _comWindowTitle;
}
[DllImport("kernel32.dll", EntryPoint = "SetLastError")]
public static extern void SetLastError(uint dwErrCode);
public static IntPtr GetCurrentWindowHandle()
{
IntPtr ptrWnd = IntPtr.Zero;
uint uiPid = (uint)Process.GetCurrentProcess().Id; // 当前进程 ID
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;
}
private static bool EnumWindowsProc(IntPtr hwnd, uint lParam)
{
uint uiPid = 0;
if (GetParent(hwnd) == IntPtr.Zero)
{
GetWindowThreadProcessId(hwnd, ref uiPid);
if (uiPid == lParam) // 找到进程对应的主窗口句柄
{
processWnd[uiPid] = hwnd; // 把句柄缓存起来
SetLastError(0); // 设置无错误
return false; // 返回 false 以终止枚举窗口
}
}
return true;
}
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;
}
}
}