protected override void ProcessRecord() { base.ProcessRecord(); // initialize SYSTEMTIME // int structMemLen = Marshal.SizeOf(typeof(SYSTEMTIME)); int structMemLen = Marshal.SizeOf(typeof(NativeMethods.SYSTEMTIME)); // 20140312 // byte[] buffer = new byte[structMemLen]; var buffer = new byte[structMemLen]; // SYSTEMTIME sysTime = new SYSTEMTIME(Date); // 20140312 // NativeMethods.SYSTEMTIME sysTime = new NativeMethods.SYSTEMTIME(Date); var sysTime = new NativeMethods.SYSTEMTIME(Date); // get memory size of SYSTEMTIME IntPtr dataPtr = Marshal.AllocHGlobal(structMemLen); Marshal.StructureToPtr(sysTime, dataPtr, true); Marshal.Copy(dataPtr, buffer, 0, structMemLen); Marshal.FreeHGlobal(dataPtr); IntPtr hndProc = IntPtr.Zero; IntPtr lpAddress = IntPtr.Zero; // 20140312 // int procId = InputObject.Current.ProcessId; // int inputHandle = InputObject.Current.NativeWindowHandle; int procId = InputObject.GetCurrent().ProcessId; int inputHandle = InputObject.GetCurrent().NativeWindowHandle; try { // inject new SYSTEMTIME into process memory injectMemory(procId, buffer, out hndProc, out lpAddress); // set DateTime to object via pointer to injected SYSTEMTIME NativeMethods.SendMessage((IntPtr)inputHandle, DTM_SETSYSTEMTIME, (IntPtr)GDT_VALID, lpAddress); } finally { // release memory and close handle if (lpAddress != (IntPtr)0 || lpAddress != IntPtr.Zero) { // we don't really care about the result because if release fails there is nothing we can do about it // bool relState = NativeMethods.VirtualFreeEx(hndProc, lpAddress, 0, FreeType.Release); bool relState = NativeMethods.VirtualFreeEx(hndProc, lpAddress, 0, NativeMethods.FreeType.Release); } if (hndProc != (IntPtr)0 || hndProc != IntPtr.Zero) { NativeMethods.CloseHandle(hndProc); } } }
// ------------------------------------------------------ // // Private methods // // ------------------------------------------------------ #region Private Methods private static NativeMethods.SYSTEMTIME ConvertDateTimeToSystemTime(DateTime dateTime) { NativeMethods.SYSTEMTIME systemTime = new NativeMethods.SYSTEMTIME(); systemTime.wYear = (ushort)dateTime.Year; systemTime.wMonth = (ushort)dateTime.Month; systemTime.wDay = (ushort)dateTime.Day; systemTime.wHour = (ushort)dateTime.Hour; systemTime.wMinute = (ushort)dateTime.Minute; systemTime.wSecond = (ushort)dateTime.Second; systemTime.wMilliseconds = (ushort)dateTime.Millisecond; return(systemTime); }
/// <devdoc> /// Takes a DateTime value and returns a SYSTEMTIME struct /// Note: 1 second granularity /// </devdoc> internal static NativeMethods.SYSTEMTIME DateTimeToSysTime(DateTime time) { NativeMethods.SYSTEMTIME sys = new NativeMethods.SYSTEMTIME(); sys.wYear = (short)time.Year; sys.wMonth = (short)time.Month; sys.wDayOfWeek = (short)time.DayOfWeek; sys.wDay = (short)time.Day; sys.wHour = (short)time.Hour; sys.wMinute = (short)time.Minute; sys.wSecond = (short)time.Second; sys.wMilliseconds = 0; return sys; }
/// <include file='doc\MonthCalendar.uex' path='docs/doc[@for="MonthCalendar.HitTest"]/*' /> /// <devdoc> /// Determines which portion of a month calendar control is at /// at a given point on the screen. /// </devdoc> public HitTestInfo HitTest(int x, int y) { NativeMethods.MCHITTESTINFO mchi = new NativeMethods.MCHITTESTINFO(); mchi.pt_x = x; mchi.pt_y = y; mchi.cbSize = Marshal.SizeOf(typeof(NativeMethods.MCHITTESTINFO)); UnsafeNativeMethods.SendMessage(new HandleRef(this, Handle), NativeMethods.MCM_HITTEST, 0, mchi); // If the hit area has an associated valid date, get it // HitArea hitArea = GetHitArea(mchi.uHit); if (HitTestInfo.HitAreaHasValidDateTime(hitArea)) { NativeMethods.SYSTEMTIME sys = new NativeMethods.SYSTEMTIME(); sys.wYear = mchi.st_wYear; sys.wMonth = mchi.st_wMonth; sys.wDayOfWeek = mchi.st_wDayOfWeek; sys.wDay = mchi.st_wDay; sys.wHour = mchi.st_wHour; sys.wMinute = mchi.st_wMinute; sys.wSecond = mchi.st_wSecond; sys.wMilliseconds = mchi.st_wMilliseconds; return new HitTestInfo(new Point(mchi.pt_x, mchi.pt_y), hitArea, DateTimePicker.SysTimeToDateTime(sys)); } else { return new HitTestInfo(new Point(mchi.pt_x, mchi.pt_y), hitArea); } }
/// <include file='doc\MonthCalendar.uex' path='docs/doc[@for="MonthCalendar.GetMonthRange"]/*' /> /// <devdoc> /// </devdoc> /// <internalonly/> private SelectionRange GetMonthRange(int flag) { NativeMethods.SYSTEMTIMEARRAY sa = new NativeMethods.SYSTEMTIMEARRAY(); SelectionRange range = new SelectionRange(); UnsafeNativeMethods.SendMessage(new HandleRef(this, Handle), NativeMethods.MCM_GETMONTHRANGE, flag, sa); NativeMethods.SYSTEMTIME st = new NativeMethods.SYSTEMTIME(); st.wYear = sa.wYear1; st.wMonth = sa.wMonth1; st.wDayOfWeek = sa.wDayOfWeek1; st.wDay = sa.wDay1; range.Start = DateTimePicker.SysTimeToDateTime(st); st.wYear = sa.wYear2; st.wMonth = sa.wMonth2; st.wDayOfWeek = sa.wDayOfWeek2; st.wDay = sa.wDay2; range.End = DateTimePicker.SysTimeToDateTime(st); return range; }
unsafe private bool SetValue(DateTime DateTimeObject) { NativeMethods.SYSTEMTIME NewDateTime = ConvertDateTimeToSystemTime(DateTimeObject); return(XSendMessage.XSend(_hwnd, NativeMethods.DTM_SETSYSTEMTIME, IntPtr.Zero, new IntPtr(&NewDateTime), Marshal.SizeOf(NewDateTime.GetType()))); }
protected override void ProcessRecord() { base.ProcessRecord(); // initialize SYSTEMTIME //int structMemLen = Marshal.SizeOf(typeof(SYSTEMTIME)); int structMemLen = Marshal.SizeOf(typeof(NativeMethods.SYSTEMTIME)); byte[] buffer = new byte[structMemLen]; //SYSTEMTIME sysTime = new SYSTEMTIME(date); NativeMethods.SYSTEMTIME sysTime = new NativeMethods.SYSTEMTIME(date); // get memory size of SYSTEMTIME IntPtr dataPtr = Marshal.AllocHGlobal(structMemLen); Marshal.StructureToPtr(sysTime, dataPtr, true); Marshal.Copy(dataPtr, buffer, 0, structMemLen); Marshal.FreeHGlobal(dataPtr); //IntPtr dataPtr = NativeMethods.AllocHGlobal(structMemLen); //NativeMethods.StructureToPtr(sysTime, dataPtr, true); //NativeMethods.Copy(dataPtr, buffer, 0, structMemLen); //NativeMethods.FreeHGlobal(dataPtr); IntPtr hndProc = IntPtr.Zero; IntPtr lpAddress = IntPtr.Zero; int procId = inputObject.Current.ProcessId; int inputHandle = inputObject.Current.NativeWindowHandle; try { // inject new SYSTEMTIME into process memory injectMemory(procId, buffer, out hndProc, out lpAddress); // set DateTime to object via pointer to injected SYSTEMTIME //SendMessage((IntPtr)inputHandle, DTM_SETSYSTEMTIME, (IntPtr)GDT_VALID, lpAddress); NativeMethods.SendMessage((IntPtr)inputHandle, NativeMethods.DTM_SETSYSTEMTIME, (IntPtr)NativeMethods.GDT_VALID, lpAddress); } finally { // release memory and close handle if (lpAddress != (IntPtr)0 || lpAddress != IntPtr.Zero) { // we don't really care about the result because if release fails there is nothing we can do about it //bool relState = VirtualFreeEx(hndProc, lpAddress, 0, FreeType.Release); bool relState = NativeMethods.VirtualFreeEx(hndProc, lpAddress, 0, NativeMethods.FreeType.Release); } if (hndProc != (IntPtr)0 || hndProc != IntPtr.Zero) { //CloseHandle(hndProc); NativeMethods.CloseHandle(hndProc); } } }