private void OpenSystemContextMenu(MouseButtonEventArgs args) { System.Windows.Point position = args.GetPosition(this); System.Windows.Point screen = this.PointToScreen(position); int num = 36; if (position.Y < num) { IntPtr handle = new WindowInteropHelper(this).Handle; IntPtr systemMenu = NativeUtilities.GetSystemMenu(handle, false); if (base.WindowState != WindowState.Maximized) { NativeUtilities.EnableMenuItem(systemMenu, 61488, 0); } else { NativeUtilities.EnableMenuItem(systemMenu, 61488, 1); } int num1 = NativeUtilities.TrackPopupMenuEx(systemMenu, NativeUtilities.TPM_LEFTALIGN | NativeUtilities.TPM_RETURNCMD, Convert.ToInt32(screen.X + 2), Convert.ToInt32(screen.Y + 2), handle, IntPtr.Zero); if (num1 == 0) { return; } NativeUtilities.PostMessage(handle, 274, new IntPtr(num1), IntPtr.Zero); } }
public static bool Is64Bit(this Process process) { IntPtr proc = NativeMethods.GetProcAddress(NativeMethods.GetModuleHandle("Kernel32.dll"), "IsWow64Process"); IntPtr hProcess = IntPtr.Zero; try { if (proc == IntPtr.Zero) { return(false); } hProcess = NativeUtilities.OpenProcess(process, NativeEnums.ProcessAccess.QueryInformation); bool retVal = false; if (NativeMethods.IsWow64Process(hProcess, out retVal)) { return(!retVal); } else { return(false); } } finally { if (hProcess != IntPtr.Zero) { NativeMethods.CloseHandle(hProcess); } } }
/// <summary> /// Extracts 3-dimensional variable value. <seealso cref="HasVariable3d"/> <seealso cref="GetVariable1d"/> /// </summary> /// <param name="variable3d">variable to extract</param> /// <returns>array with 3 variable values in order X Y Z</returns> public double[] GetVariable3d(Variable3d variable3d) { IntPtr values = NativeNode.Node_GetVariable3d(_nativeInstance, variable3d.NativeVariable); double[] unmarshalledResults = Utils.Utils.UnmarshalNativeDoubles(values, 3); NativeUtilities.Utils_FreeDoubleArray(values); return(unmarshalledResults); }
public void Update() { NativeStructs.SCROLLINFO inf = NativeUtilities.GetScrollInfo(this.TargetControl, Orientation == ScrollOrientation.VerticalScroll ? NativeEnums.SB.VERT : NativeEnums.SB.HORZ); this.Minimum = inf.nMin; this.Page = inf.nPage == 0 ? TargetControl.Height : (int)inf.nPage; this.Maximum = inf.nMax; this.Position = inf.nTrackPos; }
private static void SendMouseInput(int x, int y, NativeEnums.MouseEventFlags flag) { NativeStructs.INPUT mouseInput = new NativeStructs.INPUT(); mouseInput.type = NativeEnums.SendInputEventType.InputMouse; mouseInput.mkhi.mi.dx = NativeUtilities.CalculateAbsoluteCoordinateX(x); mouseInput.mkhi.mi.dy = NativeUtilities.CalculateAbsoluteCoordinateY(y); mouseInput.mkhi.mi.mouseData = 0; mouseInput.mkhi.mi.dwFlags = flag | NativeEnums.MouseEventFlags.Absolute; NativeMethods.SendInput(1, ref mouseInput, Marshal.SizeOf(new NativeStructs.INPUT())); }
public static void CaptureDesktop(string path, imageFormats format = imageFormats.png, byte qualityJpeg = 100) { int screenLeft = int.MaxValue; int screenTop = int.MaxValue; int screenRight = int.MinValue; int screenBottom = int.MinValue; int deviceIndex = 0; while (true) { NativeUtilities.DisplayDevice deviceData = new NativeUtilities.DisplayDevice { cb = Marshal.SizeOf(typeof(NativeUtilities.DisplayDevice)) }; if (NativeUtilities.EnumDisplayDevices(null, deviceIndex, ref deviceData, 0) != 0) { NativeUtilities.DEVMODE devMode = new NativeUtilities.DEVMODE(); if (NativeUtilities.EnumDisplaySettings(deviceData.DeviceName, NativeUtilities.ENUM_CURRENT_SETTINGS, ref devMode)) { screenLeft = Math.Min(screenLeft, devMode.dmPositionX); screenTop = Math.Min(screenTop, devMode.dmPositionY); screenRight = Math.Max(screenRight, devMode.dmPositionX + devMode.dmPelsWidth); screenBottom = Math.Max(screenBottom, devMode.dmPositionY + devMode.dmPelsHeight); } deviceIndex++; } else { break; } } Bitmap bmp = new Bitmap(screenRight - screenLeft, screenBottom - screenTop); using (Graphics g = Graphics.FromImage(bmp)) g.CopyFromScreen(screenLeft, screenTop, 0, 0, bmp.Size); saveToFile(ref bmp, path, format, qualityJpeg); }
public void Click() { NativeUtilities.DispatchMessage(Handle, (int)NativeEnums.BM.CLICK, IntPtr.Zero, IntPtr.Zero); }
/// <summary> /// Releases the specified button at a specific location. /// </summary> /// <param name="button">The button.</param> /// <param name="location">The location to release the button at.</param> /// <exception cref="System.NotImplementedException"></exception> public static void Up(IntPtr hWnd, MouseButtons button, Point location) { NativeUtilities.DispatchMessage(hWnd, (int)MouseButtonToMessage(button, true), IntPtr.Zero, new IntPtr(NativeUtilities.MakeLParam(location.X, location.Y)), false); }
public static unsafe LogicalProcessorInfo GetLogicalProcessorInformation() { int returnLength = 0; bool bResult = NativeMethods.GetLogicalProcessorInformation(null, ref returnLength); if (!bResult && returnLength == 0) { NativeUtilities.ThrowOnWin32Error("GetLogicalProcessorInformation() returned false"); } using (SafeCoTaskMemAllocHandle buffer = SafeCoTaskMemAllocHandle.Alloc((int)returnLength)) { NativeStructs.SYSTEM_LOGICAL_PROCESSOR_INFORMATION *pBuffer = (NativeStructs.SYSTEM_LOGICAL_PROCESSOR_INFORMATION *)buffer.Address; bResult = NativeMethods.GetLogicalProcessorInformation(pBuffer, ref returnLength); int elementSize = sizeof(NativeStructs.SYSTEM_LOGICAL_PROCESSOR_INFORMATION); int count = returnLength / elementSize; int coresCount = 0; int cachesCount = 0; int numaNodesCount = 0; int packagesCount = 0; for (int i = 0; i < count; ++i) { switch (pBuffer[i].Relationship) { case NativeEnums.LOGICAL_PROCESSOR_RELATIONSHIP.RelationCache: ++cachesCount; break; case NativeEnums.LOGICAL_PROCESSOR_RELATIONSHIP.RelationNumaNode: ++numaNodesCount; break; case NativeEnums.LOGICAL_PROCESSOR_RELATIONSHIP.RelationProcessorCore: ++coresCount; break; case NativeEnums.LOGICAL_PROCESSOR_RELATIONSHIP.RelationProcessorPackage: ++packagesCount; break; default: // don't count it break; } } LogicalProcessorCoreInfo[] cores = new LogicalProcessorCoreInfo[coresCount]; LogicalProcessorCacheInfo[] caches = new LogicalProcessorCacheInfo[cachesCount]; LogicalProcessorNumaNodeInfo[] numaNodes = new LogicalProcessorNumaNodeInfo[numaNodesCount]; LogicalProcessorPackageInfo[] packages = new LogicalProcessorPackageInfo[packagesCount]; int coresIndex = 0; int cachesIndex = 0; int numaNodesIndex = 0; int packagesIndex = 0; for (int i = 0; i < count; ++i) { ulong processorMask = pBuffer[i].ProcessorMask.ToUInt64(); switch (pBuffer[i].Relationship) { case NativeEnums.LOGICAL_PROCESSOR_RELATIONSHIP.RelationCache: NativeStructs.CACHE_DESCRIPTOR cacheDescriptor = pBuffer[i].Cache; LogicalProcessorCacheInfo cacheInfo = new LogicalProcessorCacheInfo(processorMask, cacheDescriptor.Level, cacheDescriptor.Associativity, cacheDescriptor.LineSize, cacheDescriptor.Size); caches[cachesIndex] = cacheInfo; ++cachesIndex; break; case NativeEnums.LOGICAL_PROCESSOR_RELATIONSHIP.RelationNumaNode: LogicalProcessorNumaNodeInfo numaNodeInfo = new LogicalProcessorNumaNodeInfo(processorMask, pBuffer[i].NumaNode_NodeNumber); numaNodes[numaNodesIndex] = numaNodeInfo; ++numaNodesIndex; break; case NativeEnums.LOGICAL_PROCESSOR_RELATIONSHIP.RelationProcessorCore: bool sharesFunctionalUnits = (pBuffer[i].ProcessorCore_Flags == 1); LogicalProcessorCoreInfo coreInfo = new LogicalProcessorCoreInfo(processorMask, sharesFunctionalUnits); cores[coresIndex] = coreInfo; ++coresIndex; break; case NativeEnums.LOGICAL_PROCESSOR_RELATIONSHIP.RelationProcessorPackage: LogicalProcessorPackageInfo packageInfo = new LogicalProcessorPackageInfo(processorMask); packages[packagesIndex] = packageInfo; ++packagesIndex; break; default: break; } } LogicalProcessorInfo info = new LogicalProcessorInfo(cores, caches, numaNodes, packages); return(info); } }
private static string GetCpuName() { IntPtr hDiSet = IntPtr.Zero; string cpuName = null; try { hDiSet = NativeMethods.SetupDiGetClassDevsW(ref processorClassGuid, null, IntPtr.Zero, NativeConstants.DIGCF_PRESENT); if (hDiSet == NativeConstants.INVALID_HANDLE_VALUE) { NativeUtilities.ThrowOnWin32Error("SetupDiGetClassDevsW returned INVALID_HANDLE_VALUE"); } bool bResult = false; uint memberIndex = 0; while (true) { NativeStructs.SP_DEVINFO_DATA spDevinfoData = new NativeStructs.SP_DEVINFO_DATA(); spDevinfoData.cbSize = (uint)Marshal.SizeOf(typeof(NativeStructs.SP_DEVINFO_DATA)); bResult = NativeMethods.SetupDiEnumDeviceInfo(hDiSet, memberIndex, ref spDevinfoData); if (!bResult) { int error = Marshal.GetLastWin32Error(); if (error == NativeConstants.ERROR_NO_MORE_ITEMS) { break; } else { throw new Win32Exception("SetupDiEnumDeviceInfo returned false, GetLastError() = " + error.ToString()); } } uint lengthReq = 0; bResult = NativeMethods.SetupDiGetDeviceInstanceIdW(hDiSet, ref spDevinfoData, IntPtr.Zero, 0, out lengthReq); if (bResult) { NativeUtilities.ThrowOnWin32Error("SetupDiGetDeviceInstanceIdW(1) returned true"); } if (lengthReq == 0) { NativeUtilities.ThrowOnWin32Error("SetupDiGetDeviceInstanceIdW(1) returned false, but also 0 for lengthReq"); } IntPtr str = IntPtr.Zero; string regPath = null; try { // Note: We cannot use Memory.Allocate() here because this property is // usually retrieved during app shutdown, during which the heap may not // be available. str = Marshal.AllocHGlobal(checked ((int)(sizeof(char) * (1 + lengthReq)))); bResult = NativeMethods.SetupDiGetDeviceInstanceIdW(hDiSet, ref spDevinfoData, str, lengthReq, out lengthReq); if (!bResult) { NativeUtilities.ThrowOnWin32Error("SetupDiGetDeviceInstanceIdW(2) returned false"); } regPath = Marshal.PtrToStringUni(str); } finally { if (str != IntPtr.Zero) { Marshal.FreeHGlobal(str); str = IntPtr.Zero; } } string keyName = @"SYSTEM\CurrentControlSet\Enum\" + regPath; using (RegistryKey procKey = Registry.LocalMachine.OpenSubKey(keyName, false)) { const string friendlyName = "FriendlyName"; if (procKey != null) { object valueObj = procKey.GetValue(friendlyName); string value = valueObj as string; if (value != null) { cpuName = value; } } } if (cpuName != null) { break; } ++memberIndex; } } finally { if (hDiSet != IntPtr.Zero) { NativeMethods.SetupDiDestroyDeviceInfoList(hDiSet); hDiSet = IntPtr.Zero; } } return(cpuName); }
/// <summary> /// Releases the specified button. /// </summary> /// <param name="hWnd">The window handle.</param> /// <param name="button">The button.</param> /// <exception cref="System.NotImplementedException"></exception> public static void Up(IntPtr hWnd, Keys button) { IntPtr lParam = new IntPtr(0x00000001 | (StUtil.Native.Internal.NativeMethods.MapVirtualKey((uint)button, 0) << 16)); NativeUtilities.DispatchMessage(hWnd, (int)NativeEnums.WM.KEYUP, new IntPtr((int)button), lParam, true); }
protected override void ButtonUp(System.Windows.Forms.MouseButtons button, int x, int y) { NativeEnums.MouseEventFlags flag; switch (button) { case System.Windows.Forms.MouseButtons.Left: flag = NativeEnums.MouseEventFlags.LeftUp; break; case System.Windows.Forms.MouseButtons.Right: flag = NativeEnums.MouseEventFlags.RightUp; break; case System.Windows.Forms.MouseButtons.Middle: flag = NativeEnums.MouseEventFlags.MiddleUp; break; default: throw new NotImplementedException(button.ToString()); } NativeMethods.mouse_event((uint)(flag | NativeEnums.MouseEventFlags.Absolute), (uint)NativeUtilities.CalculateAbsoluteCoordinateX(x), (uint)NativeUtilities.CalculateAbsoluteCoordinateY(y), 0, IntPtr.Zero); }
public override void MoveTo(int x, int y) { NativeMethods.mouse_event((uint)(NativeEnums.MouseEventFlags.Move | NativeEnums.MouseEventFlags.Absolute), (uint)NativeUtilities.CalculateAbsoluteCoordinateX(x), (uint)NativeUtilities.CalculateAbsoluteCoordinateY(y), 0, IntPtr.Zero); }