File size: 33,964 Bytes
8c763fb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 | // Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Management.Automation;
using System.Runtime.InteropServices;
namespace Microsoft.PowerShell.Commands
{
/// <summary>
/// A cmdlet to retrieve time zone information.
/// </summary>
[Cmdlet(VerbsCommon.Get, "TimeZone", DefaultParameterSetName = "Name",
HelpUri = "https://go.microsoft.com/fwlink/?LinkId=2096904")]
[OutputType(typeof(TimeZoneInfo))]
[Alias("gtz")]
public class GetTimeZoneCommand : PSCmdlet
{
#region Parameters
/// <summary>
/// A list of the local time zone ids that the cmdlet should look up.
/// </summary>
[Parameter(Mandatory = true, ValueFromPipelineByPropertyName = true, ParameterSetName = "Id")]
[SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")]
public string[] Id { get; set; }
/// <summary>
/// Specifies that the cmdlet should produce a collection of the
/// TimeZoneInfo objects that are available on the system.
/// </summary>
[Parameter(Mandatory = true, ParameterSetName = "ListAvailable")]
public SwitchParameter ListAvailable { get; set; }
/// <summary>
/// A list of the local time zone names that the cmdlet should look up.
/// </summary>
[Parameter(Position = 0, ValueFromPipeline = true, ParameterSetName = "Name")]
[SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")]
public string[] Name { get; set; }
#endregion Parameters
/// <summary>
/// Implementation of the ProcessRecord method for Get-TimeZone.
/// </summary>
protected override void ProcessRecord()
{
// make sure we've got the latest time zone settings
TimeZoneInfo.ClearCachedData();
if (ListAvailable)
{
// output the list of all available time zones
WriteObject(TimeZoneInfo.GetSystemTimeZones(), true);
}
else if (this.ParameterSetName.Equals("Id", StringComparison.OrdinalIgnoreCase))
{
// lookup each time zone id
foreach (string tzid in Id)
{
try
{
WriteObject(TimeZoneInfo.FindSystemTimeZoneById(tzid));
}
catch (TimeZoneNotFoundException e)
{
WriteError(new ErrorRecord(e, TimeZoneHelper.TimeZoneNotFoundError,
ErrorCategory.InvalidArgument, "Id"));
}
}
}
else // ParameterSetName == "Name"
{
if (Name != null)
{
// lookup each time zone name (or wildcard pattern)
foreach (string tzname in Name)
{
TimeZoneInfo[] timeZones = TimeZoneHelper.LookupSystemTimeZoneInfoByName(tzname);
if (timeZones.Length > 0)
{
// manually process each object in the array, so if there is only a single
// entry then the returned type is TimeZoneInfo and not TimeZoneInfo[], and
// it can be pipelined to Set-TimeZone more easily
foreach (TimeZoneInfo timeZone in timeZones)
{
WriteObject(timeZone);
}
}
else
{
string message = string.Format(CultureInfo.InvariantCulture,
TimeZoneResources.TimeZoneNameNotFound, tzname);
Exception e = new TimeZoneNotFoundException(message);
WriteError(new ErrorRecord(e, TimeZoneHelper.TimeZoneNotFoundError,
ErrorCategory.InvalidArgument, "Name"));
}
}
}
else
{
// return the current system local time zone
WriteObject(TimeZoneInfo.Local);
}
}
}
}
#if !UNIX
/// <summary>
/// A cmdlet to set the system's local time zone.
/// </summary>
[Cmdlet(VerbsCommon.Set, "TimeZone",
SupportsShouldProcess = true,
DefaultParameterSetName = "Name",
HelpUri = "https://go.microsoft.com/fwlink/?LinkId=2097056")]
[OutputType(typeof(TimeZoneInfo))]
[Alias("stz")]
public class SetTimeZoneCommand : PSCmdlet
{
#region string constants
private const string TimeZoneTarget = "Local System";
#endregion string constants
#region Parameters
/// <summary>
/// The name of the local time zone that the system should use.
/// </summary>
[Parameter(Mandatory = true, ParameterSetName = "Id", ValueFromPipelineByPropertyName = true)]
public string Id { get; set; }
/// <summary>
/// A TimeZoneInfo object identifying the local time zone that the system should use.
/// </summary>
[Parameter(Mandatory = true, Position = 0, ParameterSetName = "InputObject", ValueFromPipeline = true)]
public TimeZoneInfo InputObject { get; set; }
/// <summary>
/// The name of the local time zone that the system should use.
/// </summary>
[Parameter(Mandatory = true, Position = 0, ParameterSetName = "Name")]
public string Name { get; set; }
/// <summary>
/// Request return of the new local time zone as a TimeZoneInfo object.
/// </summary>
[Parameter]
public SwitchParameter PassThru { get; set; }
#endregion Parameters
/// <summary>
/// Implementation of the ProcessRecord method for Set-TimeZone.
/// </summary>
[SuppressMessage("Microsoft.Usage", "CA2208:InstantiateArgumentExceptionsCorrectly", Justification = "Since Name is not a parameter of this method, it confuses FXCop. It is the appropriate value for the exception.")]
protected override void ProcessRecord()
{
// make sure we've got fresh data, in case the requested time zone was added
// to the system (registry) after our process was started
TimeZoneInfo.ClearCachedData();
// acquire a TimeZoneInfo if one wasn't supplied.
if (this.ParameterSetName.Equals("Id", StringComparison.OrdinalIgnoreCase))
{
try
{
InputObject = TimeZoneInfo.FindSystemTimeZoneById(Id);
}
catch (TimeZoneNotFoundException e)
{
ThrowTerminatingError(new ErrorRecord(
e,
TimeZoneHelper.TimeZoneNotFoundError,
ErrorCategory.InvalidArgument,
"Id"));
}
}
else if (this.ParameterSetName.Equals("Name", StringComparison.OrdinalIgnoreCase))
{
// lookup the time zone name and make sure we have one (and only one) match
TimeZoneInfo[] timeZones = TimeZoneHelper.LookupSystemTimeZoneInfoByName(Name);
if (timeZones.Length == 0)
{
string message = string.Format(CultureInfo.InvariantCulture,
TimeZoneResources.TimeZoneNameNotFound, Name);
Exception e = new TimeZoneNotFoundException(message);
ThrowTerminatingError(new ErrorRecord(e,
TimeZoneHelper.TimeZoneNotFoundError,
ErrorCategory.InvalidArgument,
"Name"));
}
else if (timeZones.Length > 1)
{
string message = string.Format(CultureInfo.InvariantCulture,
TimeZoneResources.MultipleMatchingTimeZones, Name);
ThrowTerminatingError(new ErrorRecord(
new PSArgumentException(message, "Name"),
TimeZoneHelper.MultipleMatchingTimeZonesError,
ErrorCategory.InvalidArgument,
"Name"));
}
else
{
InputObject = timeZones[0];
}
}
else // ParameterSetName == "InputObject"
{
try
{
// a TimeZoneInfo object was supplied, so use it to make sure we can find
// a backing system time zone, otherwise it's an error condition
InputObject = TimeZoneInfo.FindSystemTimeZoneById(InputObject.Id);
}
catch (TimeZoneNotFoundException e)
{
ThrowTerminatingError(new ErrorRecord(
e,
TimeZoneHelper.TimeZoneNotFoundError,
ErrorCategory.InvalidArgument,
"InputObject"));
}
}
if (ShouldProcess(TimeZoneTarget))
{
bool acquireAccess = false;
try
{
// check to see if permission to set the time zone is already enabled for this process
if (!HasAccess)
{
// acquire permissions to set the timezone
SetAccessToken(true);
acquireAccess = true;
}
}
catch (Win32Exception e)
{
ThrowTerminatingError(new ErrorRecord(e,
TimeZoneHelper.InsufficientPermissionsError,
ErrorCategory.PermissionDenied, null));
}
try
{
// construct and populate a new DYNAMIC_TIME_ZONE_INFORMATION structure
NativeMethods.DYNAMIC_TIME_ZONE_INFORMATION dtzi = new();
dtzi.Bias -= (int)InputObject.BaseUtcOffset.TotalMinutes;
dtzi.StandardName = InputObject.StandardName;
dtzi.DaylightName = InputObject.DaylightName;
dtzi.TimeZoneKeyName = InputObject.Id;
// Request time zone transition information for the current year
NativeMethods.TIME_ZONE_INFORMATION tzi = new();
if (!NativeMethods.GetTimeZoneInformationForYear((ushort)DateTime.Now.Year, ref dtzi, ref tzi))
{
ThrowWin32Error();
}
// copy over the transition times
dtzi.StandardBias = tzi.StandardBias;
dtzi.StandardDate = tzi.StandardDate;
dtzi.DaylightBias = tzi.DaylightBias;
dtzi.DaylightDate = tzi.DaylightDate;
// set the new local time zone for the system
if (!NativeMethods.SetDynamicTimeZoneInformation(ref dtzi))
{
ThrowWin32Error();
}
// broadcast a WM_SETTINGCHANGE notification message to all top-level windows so that they
// know to update their notion of the current system time (and time zone) if applicable
int result = 0;
NativeMethods.SendMessageTimeout((IntPtr)NativeMethods.HWND_BROADCAST, NativeMethods.WM_SETTINGCHANGE,
(IntPtr)0, "intl", NativeMethods.SMTO_ABORTIFHUNG, 5000, ref result);
// clear the time zone data or this PowerShell session
// will not recognize the new time zone settings
TimeZoneInfo.ClearCachedData();
if (PassThru.IsPresent)
{
// return the TimeZoneInfo object for the (new) current local time zone
WriteObject(TimeZoneInfo.Local);
}
}
catch (Win32Exception e)
{
ThrowTerminatingError(new ErrorRecord(e,
TimeZoneHelper.SetTimeZoneFailedError,
ErrorCategory.FromStdErr, null));
}
finally
{
if (acquireAccess)
{
// reset the permissions
SetAccessToken(false);
}
}
}
else
{
if (PassThru.IsPresent)
{
// show the user the time zone settings that would have been used.
WriteObject(InputObject);
}
}
}
#region Helper functions
/// <summary>
/// True if the current process has access to change the time zone setting.
/// </summary>
protected bool HasAccess
{
get
{
bool hasAccess = false;
// open the access token for the current process
IntPtr hToken = IntPtr.Zero;
IntPtr hProcess = NativeMethods.GetCurrentProcess();
if (!NativeMethods.OpenProcessToken(hProcess,
NativeMethods.TOKEN_ADJUST_PRIVILEGES | NativeMethods.TOKEN_QUERY, ref hToken))
{
ThrowWin32Error();
}
try
{
// setup the privileges being checked
NativeMethods.PRIVILEGE_SET ps = new()
{
PrivilegeCount = 1,
Control = 1,
Luid = 0,
Attributes = NativeMethods.SE_PRIVILEGE_ENABLED,
};
// lookup the Luid of the SeTimeZonePrivilege
if (!NativeMethods.LookupPrivilegeValue(null, NativeMethods.SE_TIME_ZONE_NAME, ref ps.Luid))
{
ThrowWin32Error();
}
// set the privilege for the open access token
if (!NativeMethods.PrivilegeCheck(hToken, ref ps, ref hasAccess))
{
ThrowWin32Error();
}
}
finally
{
NativeMethods.CloseHandle(hToken);
}
return (hasAccess);
}
}
/// <summary>
/// Set the SeTimeZonePrivilege, which controls access to the SetDynamicTimeZoneInformation API.
/// </summary>
/// <param name="enable">Set to true to enable (or false to disable) the privilege.</param>
protected void SetAccessToken(bool enable)
{
// open the access token for the current process
IntPtr hToken = IntPtr.Zero;
IntPtr hProcess = NativeMethods.GetCurrentProcess();
if (!NativeMethods.OpenProcessToken(hProcess,
NativeMethods.TOKEN_ADJUST_PRIVILEGES | NativeMethods.TOKEN_QUERY, ref hToken))
{
ThrowWin32Error();
}
try
{
// setup the privileges being requested
NativeMethods.TOKEN_PRIVILEGES tp = new()
{
PrivilegeCount = 1,
Luid = 0,
Attributes = (enable ? NativeMethods.SE_PRIVILEGE_ENABLED : 0),
};
// lookup the Luid of the SeTimeZonePrivilege
if (!NativeMethods.LookupPrivilegeValue(null, NativeMethods.SE_TIME_ZONE_NAME, ref tp.Luid))
{
ThrowWin32Error();
}
// set the privilege for the open access token
if (!NativeMethods.AdjustTokenPrivileges(hToken, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero))
{
ThrowWin32Error();
}
}
finally
{
NativeMethods.CloseHandle(hToken);
}
}
/// <summary>
/// Get the Win32 error code from GetLastError and throw an exception.
/// </summary>
protected void ThrowWin32Error()
{
int error = Marshal.GetLastWin32Error();
throw new Win32Exception(error);
}
#endregion Helper functions
#region Win32 interop helper
internal static class NativeMethods
{
#region Native DLL locations
private const string SetDynamicTimeZoneApiDllName = "api-ms-win-core-timezone-l1-1-0.dll";
private const string GetTimeZoneInformationForYearApiDllName = "api-ms-win-core-timezone-l1-1-0.dll";
private const string GetCurrentProcessApiDllName = "api-ms-win-downlevel-kernel32-l1-1-0.dll";
private const string OpenProcessTokenApiDllName = "api-ms-win-downlevel-advapi32-l1-1-1.dll";
private const string LookupPrivilegeTokenApiDllName = "api-ms-win-downlevel-advapi32-l4-1-0.dll";
private const string PrivilegeCheckApiDllName = "api-ms-win-downlevel-advapi32-l1-1-1.dll";
private const string AdjustTokenPrivilegesApiDllName = "api-ms-win-downlevel-advapi32-l1-1-1.dll";
private const string CloseHandleApiDllName = "api-ms-win-downlevel-kernel32-l1-1-0.dll";
private const string SendMessageTimeoutApiDllName = "ext-ms-win-rtcore-ntuser-window-ext-l1-1-0.dll";
#endregion Native DLL locations
#region Win32 SetDynamicTimeZoneInformation imports
/// <summary>
/// Used to marshal win32 SystemTime structure to managed code layer.
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct SystemTime
{
/// <summary>
/// The year.
/// </summary>
[MarshalAs(UnmanagedType.U2)]
public short Year;
/// <summary>
/// The month.
/// </summary>
[MarshalAs(UnmanagedType.U2)]
public short Month;
/// <summary>
/// The day of the week.
/// </summary>
[MarshalAs(UnmanagedType.U2)]
public short DayOfWeek;
/// <summary>
/// The day of the month.
/// </summary>
[MarshalAs(UnmanagedType.U2)]
public short Day;
/// <summary>
/// The hour.
/// </summary>
[MarshalAs(UnmanagedType.U2)]
public short Hour;
/// <summary>
/// The minute.
/// </summary>
[MarshalAs(UnmanagedType.U2)]
public short Minute;
/// <summary>
/// The second.
/// </summary>
[MarshalAs(UnmanagedType.U2)]
public short Second;
/// <summary>
/// The millisecond.
/// </summary>
[MarshalAs(UnmanagedType.U2)]
public short Milliseconds;
}
/// <summary>
/// Used to marshal win32 DYNAMIC_TIME_ZONE_INFORMATION structure to managed code layer.
/// </summary>
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct DYNAMIC_TIME_ZONE_INFORMATION
{
/// <summary>
/// The current bias for local time translation on this computer, in minutes.
/// </summary>
[MarshalAs(UnmanagedType.I4)]
public int Bias;
/// <summary>
/// A description for standard time.
/// </summary>
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x20)]
public string StandardName;
/// <summary>
/// A SystemTime structure that contains a date and local time when the transition from daylight saving time to standard time occurs on this operating system.
/// </summary>
public SystemTime StandardDate;
/// <summary>
/// The bias value to be used during local time translations that occur during standard time.
/// </summary>
[MarshalAs(UnmanagedType.I4)]
public int StandardBias;
/// <summary>
/// A description for daylight saving time (DST).
/// </summary>
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x20)]
public string DaylightName;
/// <summary>
/// A SystemTime structure that contains a date and local time when the transition from standard time to daylight saving time occurs on this operating system.
/// </summary>
public SystemTime DaylightDate;
/// <summary>
/// The bias value to be used during local time translations that occur during daylight saving time.
/// </summary>
[MarshalAs(UnmanagedType.I4)]
public int DaylightBias;
/// <summary>
/// The name of the time zone registry key on the local computer.
/// </summary>
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x80)]
public string TimeZoneKeyName;
/// <summary>
/// Indicates whether dynamic daylight saving time is disabled.
/// </summary>
[MarshalAs(UnmanagedType.U1)]
public bool DynamicDaylightTimeDisabled;
}
/// <summary>
/// Used to marshal win32 TIME_ZONE_INFORMATION structure to managed code layer.
/// </summary>
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct TIME_ZONE_INFORMATION
{
/// <summary>
/// The current bias for local time translation on this computer, in minutes.
/// </summary>
[MarshalAs(UnmanagedType.I4)]
public int Bias;
/// <summary>
/// A description for standard time.
/// </summary>
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x20)]
public string StandardName;
/// <summary>
/// A SystemTime structure that contains a date and local time when the transition from daylight saving time to standard time occurs on this operating system.
/// </summary>
public SystemTime StandardDate;
/// <summary>
/// The bias value to be used during local time translations that occur during standard time.
/// </summary>
[MarshalAs(UnmanagedType.I4)]
public int StandardBias;
/// <summary>
/// A description for daylight saving time (DST).
/// </summary>
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x20)]
public string DaylightName;
/// <summary>
/// A SystemTime structure that contains a date and local time when the transition from standard time to daylight saving time occurs on this operating system.
/// </summary>
public SystemTime DaylightDate;
/// <summary>
/// The bias value to be used during local time translations that occur during daylight saving time.
/// </summary>
[MarshalAs(UnmanagedType.I4)]
public int DaylightBias;
}
/// <summary>
/// PInvoke SetDynamicTimeZoneInformation API.
/// </summary>
/// <param name="lpTimeZoneInformation">A DYNAMIC_TIME_ZONE_INFORMATION structure representing the desired local time zone.</param>
/// <returns></returns>
[DllImport(SetDynamicTimeZoneApiDllName, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetDynamicTimeZoneInformation([In] ref DYNAMIC_TIME_ZONE_INFORMATION lpTimeZoneInformation);
[DllImport(GetTimeZoneInformationForYearApiDllName, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetTimeZoneInformationForYear([In] ushort wYear, [In] ref DYNAMIC_TIME_ZONE_INFORMATION pdtzi, ref TIME_ZONE_INFORMATION ptzi);
#endregion Win32 SetDynamicTimeZoneInformation imports
#region Win32 AdjustTokenPrivilege imports
/// <summary>
/// Definition of TOKEN_QUERY constant from Win32 API.
/// </summary>
public const int TOKEN_QUERY = 0x00000008;
/// <summary>
/// Definition of TOKEN_ADJUST_PRIVILEGES constant from Win32 API.
/// </summary>
public const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
/// <summary>
/// Definition of SE_PRIVILEGE_ENABLED constant from Win32 API.
/// </summary>
public const int SE_PRIVILEGE_ENABLED = 0x00000002;
/// <summary>
/// Definition of SE_TIME_ZONE_NAME constant from Win32 API.
/// </summary>
public const string SE_TIME_ZONE_NAME = "SeTimeZonePrivilege"; // https://msdn.microsoft.com/library/bb530716(VS.85).aspx
/// <summary>
/// PInvoke GetCurrentProcess API.
/// </summary>
/// <returns></returns>
[DllImport(GetCurrentProcessApiDllName, ExactSpelling = true)]
public static extern IntPtr GetCurrentProcess();
/// <summary>
/// PInvoke OpenProcessToken API.
/// </summary>
/// <param name="ProcessHandle"></param>
/// <param name="DesiredAccess"></param>
/// <param name="TokenHandle"></param>
/// <returns></returns>
[DllImport(OpenProcessTokenApiDllName, SetLastError = true, CharSet = CharSet.Unicode, BestFitMapping = false)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool OpenProcessToken(IntPtr ProcessHandle, int DesiredAccess, ref IntPtr TokenHandle);
/// <summary>
/// PInvoke LookupPrivilegeValue API.
/// </summary>
/// <param name="lpSystemName"></param>
/// <param name="lpName"></param>
/// <param name="lpLuid"></param>
/// <returns></returns>
[DllImport(LookupPrivilegeTokenApiDllName, SetLastError = true, CharSet = CharSet.Unicode, BestFitMapping = false)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool LookupPrivilegeValue(string lpSystemName, string lpName, ref long lpLuid);
/// <summary>
/// PInvoke PrivilegeCheck API.
/// </summary>
/// <param name="ClientToken"></param>
/// <param name="RequiredPrivileges"></param>
/// <param name="pfResult"></param>
/// <returns></returns>
[DllImport(PrivilegeCheckApiDllName, SetLastError = true, CharSet = CharSet.Unicode, BestFitMapping = false)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool PrivilegeCheck(IntPtr ClientToken, ref PRIVILEGE_SET RequiredPrivileges, ref bool pfResult);
/// <summary>
/// PInvoke AdjustTokenPrivilege API.
/// </summary>
/// <param name="TokenHandle"></param>
/// <param name="DisableAllPrivileges"></param>
/// <param name="NewState"></param>
/// <param name="BufferLength"></param>
/// <param name="PreviousState"></param>
/// <param name="ReturnLength"></param>
/// <returns></returns>
[DllImport(AdjustTokenPrivilegesApiDllName, SetLastError = true, CharSet = CharSet.Unicode, BestFitMapping = false)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool AdjustTokenPrivileges(IntPtr TokenHandle, bool DisableAllPrivileges,
ref TOKEN_PRIVILEGES NewState, int BufferLength, IntPtr PreviousState, IntPtr ReturnLength);
/// <summary>
/// PInvoke CloseHandle API.
/// </summary>
/// <param name="hObject"></param>
/// <returns></returns>
[DllImport(CloseHandleApiDllName, ExactSpelling = true, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool CloseHandle(IntPtr hObject);
/// <summary>
/// Used to marshal win32 PRIVILEGE_SET structure to managed code layer.
/// </summary>
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct PRIVILEGE_SET
{
public int PrivilegeCount;
public int Control;
public long Luid;
public int Attributes;
}
/// <summary>
/// Used to marshal win32 TOKEN_PRIVILEGES structure to managed code layer.
/// </summary>
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct TOKEN_PRIVILEGES
{
public int PrivilegeCount;
public long Luid;
public int Attributes;
}
#endregion Win32 AdjustTokenPrivilege imports
#region Win32 SendMessage imports
/// <summary>
/// Definition of WM_SETTINGCHANGE constant from Win32 API.
/// </summary>
public const int WM_SETTINGCHANGE = 0x001A;
/// <summary>
/// Definition of HWND_BROADCAST constant from Win32 API.
/// </summary>
public const int HWND_BROADCAST = (-1);
/// <summary>
/// Definition of SMTO_ABORTIFHUNG constant from Win32 API.
/// </summary>
public const int SMTO_ABORTIFHUNG = 0x0002;
/// <summary>
/// PInvoke SendMessageTimeout API.
/// </summary>
/// <param name="hWnd"></param>
/// <param name="Msg"></param>
/// <param name="wParam"></param>
/// <param name="lParam"></param>
/// <param name="fuFlags"></param>
/// <param name="uTimeout"></param>
/// <param name="lpdwResult"></param>
/// <returns></returns>
[DllImport(SendMessageTimeoutApiDllName, SetLastError = true, CharSet = CharSet.Unicode)]
public static extern IntPtr SendMessageTimeout(IntPtr hWnd, int Msg, IntPtr wParam, string lParam, int fuFlags, int uTimeout, ref int lpdwResult);
#endregion Win32 SendMessage imports
}
#endregion Win32 interop helper
}
#endif
/// <summary>
/// Static Helper class for working with system time zones.
/// </summary>
internal static class TimeZoneHelper
{
#region Error Ids
internal const string TimeZoneNotFoundError = "TimeZoneNotFound";
internal const string MultipleMatchingTimeZonesError = "MultipleMatchingTimeZones";
internal const string InsufficientPermissionsError = "InsufficientPermissions";
internal const string SetTimeZoneFailedError = "SetTimeZoneFailed";
#endregion Error Ids
/// <summary>
/// Find the system time zone by checking first against StandardName and then,
/// if no matches were found, against the DaylightName.
/// </summary>
/// <param name="name">The name (or wildcard pattern) of the system time zone to find.</param>
/// <returns>A TimeZoneInfo object array containing information about the specified system time zones.</returns>
internal static TimeZoneInfo[] LookupSystemTimeZoneInfoByName(string name)
{
WildcardPattern namePattern = new(name, WildcardOptions.IgnoreCase);
List<TimeZoneInfo> tzi = new();
// get the available system time zones
ReadOnlyCollection<TimeZoneInfo> zones = TimeZoneInfo.GetSystemTimeZones();
// check against the standard and daylight names for each TimeZoneInfo
foreach (TimeZoneInfo zone in zones)
{
if (namePattern.IsMatch(zone.StandardName) || namePattern.IsMatch(zone.DaylightName))
{
tzi.Add(zone);
}
}
return (tzi.ToArray());
}
}
}
|