static void Main(string[] args) { Win32API kernel32 = new Win32API("kernel32.dll"); BeepDelegate Beep = kernel32.Function<BeepDelegate>("Beep"); Beep(440,500); kernel32.Dispose(); }
/// <summary> /// Simulate the mouse event /// </summary> /// <param name="flag"> the event flag</param> /// <param name="dx">the cursor position on the x coordinate</param> /// <param name="dy">the cursor position on the y coordinate</param> /// <param name="data"></param> /// <param name="extraInfo"></param> public void Simulate(Win32API.MouseEventFlag flag, int dx, int dy, uint data, UIntPtr extraInfo) { Win32API.SetCursorPos(dx, dy);//set the cursor to the position //if the mouseeventflag contains move flag ,we just ignore it ,because we have already set the cursor to the position if ((flag & Win32API.MouseEventFlag.Move) == 0) { Win32API.Mouse_Event(flag | Win32API.MouseEventFlag.Absolute, dx, dy, data, extraInfo); } }
private Win32API.KeyEvent JudgeKeyevent(XmlReader reader, Win32API.KeyEvent keyEvent) { while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element && reader.Name == "KeyEvent") { string eventName = reader.ReadInnerXml(); if (eventName == KeyboardEvents.WM_KeyUp.ToString()) { keyEvent.dwFlags = Win32API.KBEventFlag.KeyUp; } else if (eventName == KeyboardEvents.WM_KeyDown.ToString()) { keyEvent.dwFlags = Win32API.KBEventFlag.KeyDown; } break; } } return keyEvent; }
private static string GetFilePath(Win32API.SYSTEM_HANDLE_INFORMATION systemHandleInformation, Process process) { var ipProcessHwnd = Win32API.OpenProcess(Win32API.ProcessAccessFlags.All, false, process.Id); var objBasic = new Win32API.OBJECT_BASIC_INFORMATION(); var objObjectType = new Win32API.OBJECT_TYPE_INFORMATION(); var objObjectName = new Win32API.OBJECT_NAME_INFORMATION(); var strObjectName = ""; var nLength = 0; IntPtr ipTemp, ipHandle; if (!Win32API.DuplicateHandle(ipProcessHwnd, systemHandleInformation.Handle, Win32API.GetCurrentProcess(), out ipHandle, 0, false, Win32API.DUPLICATE_SAME_ACCESS)) return null; IntPtr ipBasic = Marshal.AllocHGlobal(Marshal.SizeOf(objBasic)); Win32API.NtQueryObject(ipHandle, (int)Win32API.ObjectInformationClass.ObjectBasicInformation, ipBasic, Marshal.SizeOf(objBasic), ref nLength); objBasic = (Win32API.OBJECT_BASIC_INFORMATION)Marshal.PtrToStructure(ipBasic, objBasic.GetType()); Marshal.FreeHGlobal(ipBasic); IntPtr ipObjectType = Marshal.AllocHGlobal(objBasic.TypeInformationLength); nLength = objBasic.TypeInformationLength; // this one never locks... while ((uint)(Win32API.NtQueryObject(ipHandle, (int)Win32API.ObjectInformationClass.ObjectTypeInformation, ipObjectType, nLength, ref nLength)) == Win32API.STATUS_INFO_LENGTH_MISMATCH) { if (nLength == 0) { Console.WriteLine("nLength returned at zero! "); return null; } Marshal.FreeHGlobal(ipObjectType); ipObjectType = Marshal.AllocHGlobal(nLength); } objObjectType = (Win32API.OBJECT_TYPE_INFORMATION)Marshal.PtrToStructure(ipObjectType, objObjectType.GetType()); if (Is64Bits()) { ipTemp = new IntPtr(Convert.ToInt64(objObjectType.Name.Buffer.ToString(), 10) >> 32); } else { ipTemp = objObjectType.Name.Buffer; } var strObjectTypeName = Marshal.PtrToStringUni(ipTemp, objObjectType.Name.Length >> 1); Marshal.FreeHGlobal(ipObjectType); if (strObjectTypeName != "File") return null; nLength = objBasic.NameInformationLength; var ipObjectName = Marshal.AllocHGlobal(nLength); // ...this call sometimes hangs. Is a Windows error. while ((uint)(Win32API.NtQueryObject(ipHandle, (int)Win32API.ObjectInformationClass.ObjectNameInformation, ipObjectName, nLength, ref nLength)) == Win32API.STATUS_INFO_LENGTH_MISMATCH) { Marshal.FreeHGlobal(ipObjectName); if (nLength == 0) { Console.WriteLine("nLength returned at zero! " + strObjectTypeName); return null; } ipObjectName = Marshal.AllocHGlobal(nLength); } objObjectName = (Win32API.OBJECT_NAME_INFORMATION)Marshal.PtrToStructure(ipObjectName, objObjectName.GetType()); if (Is64Bits()) { ipTemp = new IntPtr(Convert.ToInt64(objObjectName.Name.Buffer.ToString(), 10) >> 32); } else { ipTemp = objObjectName.Name.Buffer; } if (ipTemp != IntPtr.Zero) { var baTemp = new byte[nLength]; try { Marshal.Copy(ipTemp, baTemp, 0, nLength); strObjectName = Marshal.PtrToStringUni(Is64Bits() ? new IntPtr(ipTemp.ToInt64()) : new IntPtr(ipTemp.ToInt32())); } catch (AccessViolationException) { return null; } finally { Marshal.FreeHGlobal(ipObjectName); Win32API.CloseHandle(ipHandle); } } string path = GetRegularFileNameFromDevice(strObjectName); try { return path; } catch { return null; } }
public static HandleDetails GetHandleDetails (Win32API.SYSTEM_HANDLE_INFORMATION sYSTEM_HANDLE_INFORMATION, Process process, bool onlyLoadFileHandles) { var handleDetails = new HandleDetails(); try { handleDetails.HandleId = sYSTEM_HANDLE_INFORMATION.Handle; handleDetails.HandleHex = ((int)sYSTEM_HANDLE_INFORMATION.Handle).hex(); IntPtr m_ipProcessHwnd = Win32API.OpenProcess(Win32API.ProcessAccessFlags.All, false, process.Id); IntPtr ipHandle = IntPtr.Zero; var objBasic = new Win32API.OBJECT_BASIC_INFORMATION(); IntPtr ipBasic = IntPtr.Zero; var objObjectType = new Win32API.OBJECT_TYPE_INFORMATION(); IntPtr ipObjectType = IntPtr.Zero; var objObjectName = new Win32API.OBJECT_NAME_INFORMATION(); IntPtr ipObjectName = IntPtr.Zero; string strObjectTypeName = ""; string strObjectName = ""; int nLength = 0; int nReturn = 0; IntPtr ipTemp = IntPtr.Zero; if (!Win32API.DuplicateHandle(m_ipProcessHwnd, sYSTEM_HANDLE_INFORMATION.Handle, Win32API.GetCurrentProcess(), out ipHandle, 0, false, Win32API.DUPLICATE_SAME_ACCESS)) return null; ipBasic = Marshal.AllocHGlobal(Marshal.SizeOf(objBasic)); Win32API.NtQueryObject(ipHandle, (int)Win32API.ObjectInformationClass.ObjectBasicInformation, ipBasic, Marshal.SizeOf(objBasic), ref nLength); objBasic = (Win32API.OBJECT_BASIC_INFORMATION)Marshal.PtrToStructure(ipBasic, objBasic.GetType()); Marshal.FreeHGlobal(ipBasic); ipObjectType = Marshal.AllocHGlobal(objBasic.TypeInformationLength); nLength = objBasic.TypeInformationLength; while ((uint)(nReturn = Win32API.NtQueryObject(ipHandle, (int)Win32API.ObjectInformationClass.ObjectTypeInformation, ipObjectType, nLength, ref nLength)) == Win32API.STATUS_INFO_LENGTH_MISMATCH) { Marshal.FreeHGlobal(ipObjectType); ipObjectType = Marshal.AllocHGlobal(nLength); } objObjectType = (Win32API.OBJECT_TYPE_INFORMATION)Marshal.PtrToStructure(ipObjectType, objObjectType.GetType()); if (Is64Bits()) { ipTemp = new IntPtr(Convert.ToInt64(objObjectType.Name.Buffer.ToString(), 10) >> 32); } else { ipTemp = objObjectType.Name.Buffer; } strObjectTypeName = Marshal.PtrToStringUni(ipTemp, objObjectType.Name.Length >> 1); handleDetails.ObjectType = strObjectTypeName; Marshal.FreeHGlobal(ipObjectType); if (onlyLoadFileHandles && strObjectTypeName != "File") return null; nLength = objBasic.NameInformationLength; ipObjectName = Marshal.AllocHGlobal(nLength); while ((uint)(nReturn = Win32API.NtQueryObject(ipHandle, (int)Win32API.ObjectInformationClass.ObjectNameInformation, ipObjectName, nLength, ref nLength)) == Win32API.STATUS_INFO_LENGTH_MISMATCH) { Marshal.FreeHGlobal(ipObjectName); ipObjectName = Marshal.AllocHGlobal(nLength); } objObjectName = (Win32API.OBJECT_NAME_INFORMATION)Marshal.PtrToStructure(ipObjectName, objObjectName.GetType()); if (Is64Bits()) { ipTemp = new IntPtr(Convert.ToInt64(objObjectName.Name.Buffer.ToString(), 10) >> 32); } else { ipTemp = objObjectName.Name.Buffer; } if (ipTemp != IntPtr.Zero) { byte[] baTemp = new byte[nLength]; try { Marshal.Copy(ipTemp, baTemp, 0, nLength); strObjectName = Marshal.PtrToStringUni(Is64Bits() ? new IntPtr(ipTemp.ToInt64()) : new IntPtr(ipTemp.ToInt32())); } catch (AccessViolationException) { return null; } finally { Marshal.FreeHGlobal(ipObjectName); Win32API.CloseHandle(ipHandle); } } string path = GetRegularFileNameFromDevice(strObjectName); handleDetails.Path = path; if (path.valid()) handleDetails.Path = path; } catch (Exception ex) { ex.log(); } return handleDetails; }
//based on original GetFilePath public static HandleDetails GetHandleDetails (Win32API.SYSTEM_HANDLE_INFORMATION sYSTEM_HANDLE_INFORMATION, Process process) { return GetHandleDetails(sYSTEM_HANDLE_INFORMATION,process, true); }
internal void SetClip(Win32API.RECT rc) { Win32API.IntersectClipRect(dc_, rc.left, rc.top, rc.right, rc.bottom); }
private static string GetFilePath(Win32API.SYSTEM_HANDLE_INFORMATION systemHandleInformation, ref IntPtr ipHandle) { var ipProcessHwnd = Win32API.OpenProcess(Win32API.ProcessAccessFlags.All, false, systemHandleInformation.ProcessID); var objBasic = new Win32API.OBJECT_BASIC_INFORMATION(); var objObjectType = new Win32API.OBJECT_TYPE_INFORMATION(); var objObjectName = new Win32API.OBJECT_NAME_INFORMATION(); var strObjectName = ""; var nLength = 0; if (!Win32API.DuplicateHandle(ipProcessHwnd, systemHandleInformation.Handle, Win32API.GetCurrentProcess(), out ipHandle, 0, false, Win32API.DUPLICATE_SAME_ACCESS)) { //Log.Trace("GetFilePath(" + systemHandleInformation.ProcessID + ", 0x" + systemHandleInformation.Handle.ToString("X") + ") : DuplicateHandle(0x" + ipProcessHwnd.ToString("X") + ", 0x" + systemHandleInformation.Handle.ToString("X") + ") returned false."); return null; } IntPtr ipBasic = Marshal.AllocHGlobal(Marshal.SizeOf(objBasic)); Win32API.NtQueryObject(ipHandle, (int)Win32API.ObjectInformationClass.ObjectBasicInformation, ipBasic, Marshal.SizeOf(objBasic), ref nLength); objBasic = (Win32API.OBJECT_BASIC_INFORMATION)Marshal.PtrToStructure(ipBasic, objBasic.GetType()); Marshal.FreeHGlobal(ipBasic); IntPtr ipObjectType = Marshal.AllocHGlobal(objBasic.TypeInformationLength); nLength = objBasic.TypeInformationLength; // this one never locks... while ((uint)(Win32API.NtQueryObject(ipHandle, (int)Win32API.ObjectInformationClass.ObjectTypeInformation, ipObjectType, nLength, ref nLength)) == Win32API.STATUS_INFO_LENGTH_MISMATCH) { if (nLength == 0) { Log.Trace("GetFilePath(" + systemHandleInformation.ProcessID + ", 0x" + systemHandleInformation.Handle.ToString("X") + ") : NtQueryObject(0x" + ipHandle.ToString("X") + ", ObjectTypeInformation) returned !STATUS_INFO_LENGTH_MISMATCH when nLength == 0."); return null; } Marshal.FreeHGlobal(ipObjectType); ipObjectType = Marshal.AllocHGlobal(nLength); } // TODO: check if this code still works on Windows 7, now that UNICODE_STRING doesn't have Pack = 1 defined objObjectType = (Win32API.OBJECT_TYPE_INFORMATION)Marshal.PtrToStructure(ipObjectType, objObjectType.GetType()); // All handles that have these access flags seem to hang the NtQueryObject below. // Original discussion: http://forum.sysinternals.com/handle-name-help-ntqueryobject_topic14435_post68507.html#68507 if (objBasic.GrantedAccess == 0x0012019F || objObjectType.ValidAccess == 0x0012019F || objBasic.GrantedAccess == 0x001A019F || objObjectType.ValidAccess == 0x001A019F) { Log.Trace("Blacklisted access value for handle 0x" + ipHandle.ToString("X") + "; see source code for more information."); return null; } var strObjectTypeName = Marshal.PtrToStringUni(objObjectType.Name.Buffer, objObjectType.Name.Length >> 1); Marshal.FreeHGlobal(ipObjectType); if (strObjectTypeName != "File") { return null; } nLength = objBasic.NameInformationLength; var ipObjectName = Marshal.AllocHGlobal(nLength); // ...this call sometimes hangs due to a Windows error. while ((uint)(Win32API.NtQueryObject(ipHandle, (int)Win32API.ObjectInformationClass.ObjectNameInformation, ipObjectName, nLength, ref nLength)) == Win32API.STATUS_INFO_LENGTH_MISMATCH) { Marshal.FreeHGlobal(ipObjectName); if (nLength == 0) { Log.Trace("GetFilePath(" + systemHandleInformation.ProcessID + ", 0x" + systemHandleInformation.Handle.ToString("X") + ") : NtQueryObject(0x" + ipHandle.ToString("X") + ", ObjectNameInformation) returned !STATUS_INFO_LENGTH_MISMATCH when nLength == 0."); return null; } ipObjectName = Marshal.AllocHGlobal(nLength); } objObjectName = (Win32API.OBJECT_NAME_INFORMATION)Marshal.PtrToStructure(ipObjectName, objObjectName.GetType()); if (objObjectName.Name.Buffer.ToInt64() > 0 && nLength > 0) { var baTemp = new byte[nLength]; try { Marshal.Copy(objObjectName.Name.Buffer, baTemp, 0, nLength); strObjectName = Marshal.PtrToStringUni(objObjectName.Name.Buffer); } catch (AccessViolationException ex) { Log.Trace("GetFilePath(" + systemHandleInformation.ProcessID + ", 0x" + systemHandleInformation.Handle.ToString("X") + ") Error while marshaling file name.", ex); return null; } finally { Marshal.FreeHGlobal(ipObjectName); Win32API.CloseHandle(ipHandle); } } if (strObjectName == null) { return null; } if (strObjectName.StartsWith(@"\Device\Mup\")) { return @"\\" + strObjectName.Substring(12); } try { return GetRegularFileNameFromDevice(strObjectName); } catch { return null; } }
private static unsafe void FillBitmapInfo(ref Win32API.BITMAPINFO bmi, int width, int height, int bpp, int origin) { Debug.Assert(width > 0 && height > 0 && (bpp == 8 || bpp == 24 || bpp == 32)); Win32API.BITMAPINFOHEADER bmih = bmi.Header; using (ScopedGCHandle handle = new ScopedGCHandle(bmih, GCHandleType.Pinned)) { //Win32API.FillMemory(handle.AddrOfPinnedObject(), Marshal.SizeOf(typeof(Win32API.BITMAPINFOHEADER)), 0); bmih.Size = (uint)sizeof(Win32API.BITMAPINFOHEADER); bmih.Width = width; bmih.Height = (origin != 0) ? Math.Abs(height) : -Math.Abs(height); bmih.Planes = 1; bmih.BitCount = (ushort)bpp; bmih.Compression = Win32API.BI_RGB; bmih.ClrImportant = 0; bmih.Compression = 0; bmih.SizeImage = 0; bmih.XPelsPerMeter = 0; bmih.YPelsPerMeter = 0; bmi.Header = bmih; } if (bpp == 8) { Win32API.RGBQUAD[] palette = bmi.Colors; for (int i = 0; i < palette.Length; i++) { palette[i].Blue = palette[i].Green = palette[i].Red = (byte)i; palette[i].Reserved = 0; } bmi.Colors = palette; } }
private static string GetFilePath(Win32API.SYSTEM_HANDLE_INFORMATION systemHandleInformation, ref IntPtr ipHandle) { var ipProcessHwnd = Win32API.OpenProcess(Win32API.ProcessAccessFlags.All, false, systemHandleInformation.ProcessID); var objBasic = new Win32API.OBJECT_BASIC_INFORMATION(); var objObjectType = new Win32API.OBJECT_TYPE_INFORMATION(); var objObjectName = new Win32API.OBJECT_NAME_INFORMATION(); var strObjectName = ""; var nLength = 0; IntPtr ipTemp; if (!Win32API.DuplicateHandle(ipProcessHwnd, systemHandleInformation.Handle, Win32API.GetCurrentProcess(), out ipHandle, 0, false, Win32API.DUPLICATE_SAME_ACCESS)) { Log.Trace("GetFilePath(" + systemHandleInformation.ProcessID + ", 0x" + systemHandleInformation.Handle.ToString("X") + ") : DuplicateHandle(0x" + ipProcessHwnd.ToString("X") + ", 0x" + systemHandleInformation.Handle.ToString("X") + ") returned false."); return null; } IntPtr ipBasic = Marshal.AllocHGlobal(Marshal.SizeOf(objBasic)); Win32API.NtQueryObject(ipHandle, (int)Win32API.ObjectInformationClass.ObjectBasicInformation, ipBasic, Marshal.SizeOf(objBasic), ref nLength); objBasic = (Win32API.OBJECT_BASIC_INFORMATION)Marshal.PtrToStructure(ipBasic, objBasic.GetType()); Marshal.FreeHGlobal(ipBasic); IntPtr ipObjectType = Marshal.AllocHGlobal(objBasic.TypeInformationLength); nLength = objBasic.TypeInformationLength; // this one never locks... while ((uint)(Win32API.NtQueryObject(ipHandle, (int)Win32API.ObjectInformationClass.ObjectTypeInformation, ipObjectType, nLength, ref nLength)) == Win32API.STATUS_INFO_LENGTH_MISMATCH) { if (nLength == 0) { Log.Trace("GetFilePath(" + systemHandleInformation.ProcessID + ", 0x" + systemHandleInformation.Handle.ToString("X") + ") : NtQueryObject(0x" + ipHandle.ToString("X") + ", ObjectTypeInformation) returned !STATUS_INFO_LENGTH_MISMATCH when nLength == 0."); return null; } Marshal.FreeHGlobal(ipObjectType); ipObjectType = Marshal.AllocHGlobal(nLength); } objObjectType = (Win32API.OBJECT_TYPE_INFORMATION)Marshal.PtrToStructure(ipObjectType, objObjectType.GetType()); if (Is64Bits()) { ipTemp = new IntPtr(Convert.ToInt64(objObjectType.Name.Buffer.ToString(), 10) >> 32); } else { ipTemp = objObjectType.Name.Buffer; } var strObjectTypeName = Marshal.PtrToStringUni(ipTemp, objObjectType.Name.Length >> 1); Marshal.FreeHGlobal(ipObjectType); if (strObjectTypeName != "File") return null; nLength = objBasic.NameInformationLength; var ipObjectName = Marshal.AllocHGlobal(nLength); // ...this call sometimes hangs. Is a Windows error. while ((uint)(Win32API.NtQueryObject(ipHandle, (int)Win32API.ObjectInformationClass.ObjectNameInformation, ipObjectName, nLength, ref nLength)) == Win32API.STATUS_INFO_LENGTH_MISMATCH) { Marshal.FreeHGlobal(ipObjectName); if (nLength == 0) { Log.Trace("GetFilePath(" + systemHandleInformation.ProcessID + ", 0x" + systemHandleInformation.Handle.ToString("X") + ") : NtQueryObject(0x" + ipHandle.ToString("X") + ", ObjectNameInformation) returned !STATUS_INFO_LENGTH_MISMATCH when nLength == 0."); return null; } ipObjectName = Marshal.AllocHGlobal(nLength); } objObjectName = (Win32API.OBJECT_NAME_INFORMATION)Marshal.PtrToStructure(ipObjectName, objObjectName.GetType()); if (Is64Bits()) { ipTemp = new IntPtr(Convert.ToInt64(objObjectName.Name.Buffer.ToString(), 10) >> 32); } else { ipTemp = objObjectName.Name.Buffer; } if (ipTemp != IntPtr.Zero) { var baTemp = new byte[nLength]; try { Marshal.Copy(ipTemp, baTemp, 0, nLength); strObjectName = Marshal.PtrToStringUni(Is64Bits() ? new IntPtr(ipTemp.ToInt64()) : new IntPtr(ipTemp.ToInt32())); } catch (AccessViolationException ex) { Log.Trace("GetFilePath(" + systemHandleInformation.ProcessID + ", 0x" + systemHandleInformation.Handle.ToString("X") + ") Error while marshaling file name.", ex); return null; } finally { Marshal.FreeHGlobal(ipObjectName); Win32API.CloseHandle(ipHandle); } } if (strObjectName == null) { return null; } if (strObjectName.StartsWith(@"\Device\Mup\")) { return @"\\" + strObjectName.Substring(12); } try { return GetRegularFileNameFromDevice(strObjectName); } catch { return null; } }
private Win32API.MouseEvent JudgeMouseEvent(Win32API.MouseEvent MouseEvent, string eventName) { switch (eventName) { case "WM_MOUSEMOVE": MouseEvent.dwFlags = Win32API.MouseEventFlag.Move; break; case "WM_LBUTTONDOWN": MouseEvent.dwFlags = Win32API.MouseEventFlag.LeftDown; break; case "WM_LBUTTONUP": MouseEvent.dwFlags = Win32API.MouseEventFlag.LeftUp; break; case "WM_LBUTTONDBLCLK": //MouseEvent.dwFlags = Win32API.MouseEventFlag break; case "WM_RBUTTONDOWN": MouseEvent.dwFlags = Win32API.MouseEventFlag.RightDown; break; case "WM_RBUTTONUP": MouseEvent.dwFlags = Win32API.MouseEventFlag.RightUp; break; case "WM_RBUTTONDBLCLK": break; case "WM_MBUTTONDOWN": MouseEvent.dwFlags = Win32API.MouseEventFlag.MiddleDown; break; case "WM_MBUTTONUP": MouseEvent.dwFlags = Win32API.MouseEventFlag.MiddleUp; break; case "WM_MBUTTONDBLCLK": break; case "WM_MOUSEWHEEL": MouseEvent.dwFlags = Win32API.MouseEventFlag.Wheel; break; default: break; } return MouseEvent; }
/// <summary> /// Simulate the keyboard event /// </summary> /// <param name="bVk">Specifies a virtual-key code. The code must be a value in the range 1 to 254.</param> /// <param name="vScan">Specifies a hardware scan code for the key.</param> /// <param name="dwFlags"></param> /// <param name="dwExtraInfo"></param> public void Simulate(int bVk, int vScan, Win32API.KBEventFlag dwFlags, int dwExtraInfo) { Win32API.Keybd_Event( bVk, vScan, dwFlags, dwExtraInfo); }