File size: 31,696 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 | // Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
// ----------------------------------------------------------------------
// Contents: Entry points for managed PowerShell plugin worker used to
// host powershell in a WSMan service.
// ----------------------------------------------------------------------
using System.Threading;
using System.Runtime.InteropServices;
using System.Diagnostics.CodeAnalysis;
using System.Management.Automation.Internal;
using System.Globalization;
namespace System.Management.Automation.Remoting
{
// TODO: Does this comment still apply?
// The following complex delegate + native function pointer model is used
// because of a problem with GCRoot. GCRoots cannot hold reference to the
// AppDomain that created it. In the IIS hosting scenario, there may be
// cases where multiple AppDomains exist in the same hosting process. In such
// cases if GCRoot is used, CLR will pick up the first AppDomain in the list
// to get the managed handle. Delegates are not just function pointers, they
// also contain a reference to the AppDomain that created it. However the catch
// is that delegates must be marshalled into their respective unmanaged function
// pointers (otherwise we end up storing the delegate into a GCRoot).
/// <summary>
/// </summary>
/// <param name="pluginContext">PVOID.</param>
/// <param name="requestDetails">WSMAN_PLUGIN_REQUEST*.</param>
/// <param name="flags">DWORD.</param>
/// <param name="extraInfo">PCWSTR.</param>
/// <param name="startupInfo">WSMAN_SHELL_STARTUP_INFO*.</param>
/// <param name="inboundShellInformation">WSMAN_DATA*.</param>
internal delegate void WSManPluginShellDelegate(
IntPtr pluginContext,
IntPtr requestDetails,
int flags,
[MarshalAs(UnmanagedType.LPWStr)] string extraInfo,
IntPtr startupInfo,
IntPtr inboundShellInformation);
/// <summary>
/// </summary>
/// <param name="pluginContext">PVOID.</param>
/// <param name="shellContext">PVOID.</param>
internal delegate void WSManPluginReleaseShellContextDelegate(
IntPtr pluginContext,
IntPtr shellContext);
/// <summary>
/// </summary>
/// <param name="pluginContext">PVOID.</param>
/// <param name="requestDetails">WSMAN_PLUGIN_REQUEST*.</param>
/// <param name="flags">DWORD.</param>
/// <param name="shellContext">PVOID.</param>
/// <param name="commandContext">PVOID optional.</param>
/// <param name="inboundConnectInformation">WSMAN_DATA* optional.</param>
internal delegate void WSManPluginConnectDelegate(
IntPtr pluginContext,
IntPtr requestDetails,
int flags,
IntPtr shellContext,
IntPtr commandContext,
IntPtr inboundConnectInformation);
/// <summary>
/// </summary>
/// <param name="pluginContext">PVOID.</param>
/// <param name="requestDetails">WSMAN_PLUGIN_REQUEST*.</param>
/// <param name="flags">DWORD.</param>
/// <param name="shellContext">PVOID.</param>
/// <param name="commandLine">PCWSTR.</param>
/// <param name="arguments">WSMAN_COMMAND_ARG_SET*.</param>
internal delegate void WSManPluginCommandDelegate(
IntPtr pluginContext,
IntPtr requestDetails,
int flags,
IntPtr shellContext,
[MarshalAs(UnmanagedType.LPWStr)] string commandLine,
IntPtr arguments);
/// <summary>
/// Delegate that is passed to native layer for callback on operation shutdown notifications.
/// </summary>
/// <param name="shutdownContext">IntPtr.</param>
internal delegate void WSManPluginOperationShutdownDelegate(
IntPtr shutdownContext);
/// <summary>
/// </summary>
/// <param name="pluginContext">PVOID.</param>
/// <param name="shellContext">PVOID.</param>
/// <param name="commandContext">PVOID.</param>
internal delegate void WSManPluginReleaseCommandContextDelegate(
IntPtr pluginContext,
IntPtr shellContext,
IntPtr commandContext);
/// <summary>
/// </summary>
/// <param name="pluginContext">PVOID.</param>
/// <param name="requestDetails">WSMAN_PLUGIN_REQUEST*.</param>
/// <param name="flags">DWORD.</param>
/// <param name="shellContext">PVOID.</param>
/// <param name="commandContext">PVOID.</param>
/// <param name="stream">PCWSTR.</param>
/// <param name="inboundData">WSMAN_DATA*.</param>
internal delegate void WSManPluginSendDelegate(
IntPtr pluginContext,
IntPtr requestDetails,
int flags,
IntPtr shellContext,
IntPtr commandContext,
[MarshalAs(UnmanagedType.LPWStr)] string stream,
IntPtr inboundData);
/// <summary>
/// </summary>
/// <param name="pluginContext">PVOID.</param>
/// <param name="requestDetails">WSMAN_PLUGIN_REQUEST*.</param>
/// <param name="flags">DWORD.</param>
/// <param name="shellContext">PVOID.</param>
/// <param name="commandContext">PVOID optional.</param>
/// <param name="streamSet">WSMAN_STREAM_ID_SET* optional.</param>
internal delegate void WSManPluginReceiveDelegate(
IntPtr pluginContext,
IntPtr requestDetails,
int flags,
IntPtr shellContext,
IntPtr commandContext,
IntPtr streamSet);
/// <summary>
/// </summary>
/// <param name="pluginContext">PVOID.</param>
/// <param name="requestDetails">WSMAN_PLUGIN_REQUEST*.</param>
/// <param name="flags">DWORD.</param>
/// <param name="shellContext">PVOID.</param>
/// <param name="commandContext">PVOID optional.</param>
/// <param name="code">PCWSTR.</param>
internal delegate void WSManPluginSignalDelegate(
IntPtr pluginContext,
IntPtr requestDetails,
int flags,
IntPtr shellContext,
IntPtr commandContext,
[MarshalAs(UnmanagedType.LPWStr)] string code);
/// <summary>
/// Callback that handles shell shutdown notification events.
/// </summary>
/// <param name="state"></param>
/// <param name="timedOut"></param>
internal delegate void WaitOrTimerCallbackDelegate(
IntPtr state,
bool timedOut);
/// <summary>
/// </summary>
/// <param name="pluginContext">PVOID.</param>
internal delegate void WSManShutdownPluginDelegate(
IntPtr pluginContext);
/// <summary>
/// </summary>
internal sealed class WSManPluginEntryDelegates : IDisposable
{
#region Private Members
// Holds the delegate pointers in a structure that has identical layout to the native structure.
private readonly WSManPluginEntryDelegatesInternal _unmanagedStruct = new WSManPluginEntryDelegatesInternal();
internal WSManPluginEntryDelegatesInternal UnmanagedStruct
{
get { return _unmanagedStruct; }
}
// Flag: Has Dispose already been called?
private bool _disposed = false;
/// <summary>
/// GC handle which prevents garbage collector from collecting this delegate.
/// </summary>
private GCHandle _pluginShellGCHandle;
private GCHandle _pluginReleaseShellContextGCHandle;
private GCHandle _pluginCommandGCHandle;
private GCHandle _pluginReleaseCommandContextGCHandle;
private GCHandle _pluginSendGCHandle;
private GCHandle _pluginReceiveGCHandle;
private GCHandle _pluginSignalGCHandle;
private GCHandle _pluginConnectGCHandle;
private GCHandle _shutdownPluginGCHandle;
private GCHandle _WSManPluginOperationShutdownGCHandle;
#endregion
#region Constructor
/// <summary>
/// Initializes the delegate struct for later use.
/// </summary>
internal WSManPluginEntryDelegates()
{
populateDelegates();
}
#endregion
#region IDisposable Methods
/// <summary>
/// Internal implementation of Dispose pattern callable by consumers.
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Protected implementation of Dispose pattern.
/// </summary>
/// <param name="disposing"></param>
private void Dispose(bool disposing)
{
if (_disposed)
return;
// Free any unmanaged objects here.
this.CleanUpDelegates();
_disposed = true;
}
/// <summary>
/// Finalizes an instance of the <see cref="WSManPluginEntryDelegates"/> class.
/// </summary>
~WSManPluginEntryDelegates()
{
Dispose(false);
}
#endregion IDisposable Methods
/// <summary>
/// Creates delegates and populates the managed version of the
/// structure that will be passed to unmanaged callers.
/// </summary>
private void populateDelegates()
{
// if a delegate is re-located by a garbage collection, it will not affect
// the underlaying managed callback, so Alloc is used to add a reference
// to the delegate, allowing relocation of the delegate, but preventing
// disposal. Using GCHandle without pinning reduces fragmentation potential
// of the managed heap.
{
WSManPluginShellDelegate pluginShell = new WSManPluginShellDelegate(WSManPluginManagedEntryWrapper.WSManPluginShell);
_pluginShellGCHandle = GCHandle.Alloc(pluginShell);
// marshal the delegate to a unmanaged function pointer so that AppDomain reference is stored correctly.
// Populate the outgoing structure so the caller has access to the entry points
_unmanagedStruct.wsManPluginShellCallbackNative = Marshal.GetFunctionPointerForDelegate(pluginShell);
}
{
WSManPluginReleaseShellContextDelegate pluginReleaseShellContext = new WSManPluginReleaseShellContextDelegate(WSManPluginManagedEntryWrapper.WSManPluginReleaseShellContext);
_pluginReleaseShellContextGCHandle = GCHandle.Alloc(pluginReleaseShellContext);
_unmanagedStruct.wsManPluginReleaseShellContextCallbackNative = Marshal.GetFunctionPointerForDelegate(pluginReleaseShellContext);
}
{
WSManPluginCommandDelegate pluginCommand = new WSManPluginCommandDelegate(WSManPluginManagedEntryWrapper.WSManPluginCommand);
_pluginCommandGCHandle = GCHandle.Alloc(pluginCommand);
_unmanagedStruct.wsManPluginCommandCallbackNative = Marshal.GetFunctionPointerForDelegate(pluginCommand);
}
{
WSManPluginReleaseCommandContextDelegate pluginReleaseCommandContext = new WSManPluginReleaseCommandContextDelegate(WSManPluginManagedEntryWrapper.WSManPluginReleaseCommandContext);
_pluginReleaseCommandContextGCHandle = GCHandle.Alloc(pluginReleaseCommandContext);
_unmanagedStruct.wsManPluginReleaseCommandContextCallbackNative = Marshal.GetFunctionPointerForDelegate(pluginReleaseCommandContext);
}
{
WSManPluginSendDelegate pluginSend = new WSManPluginSendDelegate(WSManPluginManagedEntryWrapper.WSManPluginSend);
_pluginSendGCHandle = GCHandle.Alloc(pluginSend);
_unmanagedStruct.wsManPluginSendCallbackNative = Marshal.GetFunctionPointerForDelegate(pluginSend);
}
{
WSManPluginReceiveDelegate pluginReceive = new WSManPluginReceiveDelegate(WSManPluginManagedEntryWrapper.WSManPluginReceive);
_pluginReceiveGCHandle = GCHandle.Alloc(pluginReceive);
_unmanagedStruct.wsManPluginReceiveCallbackNative = Marshal.GetFunctionPointerForDelegate(pluginReceive);
}
{
WSManPluginSignalDelegate pluginSignal = new WSManPluginSignalDelegate(WSManPluginManagedEntryWrapper.WSManPluginSignal);
_pluginSignalGCHandle = GCHandle.Alloc(pluginSignal);
_unmanagedStruct.wsManPluginSignalCallbackNative = Marshal.GetFunctionPointerForDelegate(pluginSignal);
}
{
WSManPluginConnectDelegate pluginConnect = new WSManPluginConnectDelegate(WSManPluginManagedEntryWrapper.WSManPluginConnect);
_pluginConnectGCHandle = GCHandle.Alloc(pluginConnect);
_unmanagedStruct.wsManPluginConnectCallbackNative = Marshal.GetFunctionPointerForDelegate(pluginConnect);
}
{
WSManShutdownPluginDelegate shutdownPlugin = new WSManShutdownPluginDelegate(WSManPluginManagedEntryWrapper.ShutdownPlugin);
_shutdownPluginGCHandle = GCHandle.Alloc(shutdownPlugin);
_unmanagedStruct.wsManPluginShutdownPluginCallbackNative = Marshal.GetFunctionPointerForDelegate(shutdownPlugin);
}
if (!Platform.IsWindows)
{
WSManPluginOperationShutdownDelegate pluginShutDownDelegate = new WSManPluginOperationShutdownDelegate(WSManPluginManagedEntryWrapper.WSManPSShutdown);
_WSManPluginOperationShutdownGCHandle = GCHandle.Alloc(pluginShutDownDelegate);
_unmanagedStruct.wsManPluginShutdownCallbackNative = Marshal.GetFunctionPointerForDelegate(pluginShutDownDelegate);
}
}
/// <summary>
/// </summary>
private void CleanUpDelegates()
{
// Free GCHandles so that the memory they point to may be unpinned (garbage collected)
if (_pluginShellGCHandle.IsAllocated)
{
_pluginShellGCHandle.Free();
_pluginReleaseShellContextGCHandle.Free();
_pluginCommandGCHandle.Free();
_pluginReleaseCommandContextGCHandle.Free();
_pluginSendGCHandle.Free();
_pluginReceiveGCHandle.Free();
_pluginSignalGCHandle.Free();
_pluginConnectGCHandle.Free();
_shutdownPluginGCHandle.Free();
if (!Platform.IsWindows)
{
_WSManPluginOperationShutdownGCHandle.Free();
}
}
}
/// <summary>
/// Structure definition to match the native one.
/// NOTE: The layout of this structure must be IDENTICAL between here and PwrshPluginWkr_Ptrs in pwrshplugindefs.h!
/// </summary>
[StructLayout(LayoutKind.Sequential)]
internal class WSManPluginEntryDelegatesInternal
{
/// <summary>
/// WsManPluginShutdownPluginCallbackNative.
/// </summary>
internal IntPtr wsManPluginShutdownPluginCallbackNative;
/// <summary>
/// WSManPluginShellCallbackNative.
/// </summary>
internal IntPtr wsManPluginShellCallbackNative;
/// <summary>
/// WSManPluginReleaseShellContextCallbackNative.
/// </summary>
internal IntPtr wsManPluginReleaseShellContextCallbackNative;
/// <summary>
/// WSManPluginCommandCallbackNative.
/// </summary>
internal IntPtr wsManPluginCommandCallbackNative;
/// <summary>
/// WSManPluginReleaseCommandContextCallbackNative.
/// </summary>
internal IntPtr wsManPluginReleaseCommandContextCallbackNative;
/// <summary>
/// WSManPluginSendCallbackNative.
/// </summary>
internal IntPtr wsManPluginSendCallbackNative;
/// <summary>
/// WSManPluginReceiveCallbackNative.
/// </summary>
internal IntPtr wsManPluginReceiveCallbackNative;
/// <summary>
/// WSManPluginSignalCallbackNative.
/// </summary>
internal IntPtr wsManPluginSignalCallbackNative;
/// <summary>
/// WSManPluginConnectCallbackNative.
/// </summary>
internal IntPtr wsManPluginConnectCallbackNative;
/// <summary>
/// WSManPluginCommandCallbackNative.
/// </summary>
internal IntPtr wsManPluginShutdownCallbackNative;
}
}
/// <summary>
/// Class containing the public static managed entry functions that are callable from outside
/// the module.
/// </summary>
public sealed class WSManPluginManagedEntryWrapper
{
/// <summary>
/// Constructor is private because it only contains static members and properties.
/// </summary>
private WSManPluginManagedEntryWrapper() { }
/// <summary>
/// Immutable container that holds the delegates and their unmanaged pointers.
/// </summary>
internal static readonly WSManPluginEntryDelegates workerPtrs = new WSManPluginEntryDelegates();
#region Managed Entry Points
/// <summary>
/// Called only once after the assembly is loaded..This is used to perform
/// various initializations.
/// </summary>
/// <param name="wkrPtrs">IntPtr to WSManPluginEntryDelegates.WSManPluginEntryDelegatesInternal.</param>
/// <returns>0 = Success, 1 = Failure.</returns>
public static int InitPlugin(
IntPtr wkrPtrs)
{
if (wkrPtrs == IntPtr.Zero)
{
return WSManPluginConstants.ExitCodeFailure;
}
Marshal.StructureToPtr<WSManPluginEntryDelegates.WSManPluginEntryDelegatesInternal>(workerPtrs.UnmanagedStruct, wkrPtrs, false);
return WSManPluginConstants.ExitCodeSuccess;
}
/// <summary>
/// Called only once during shutdown. This is used to perform various deinitializations.
/// </summary>
/// <param name="pluginContext">PVOID.</param>
public static void ShutdownPlugin(
IntPtr pluginContext)
{
WSManPluginInstance.PerformShutdown(pluginContext);
workerPtrs?.Dispose();
}
/// <summary>
/// </summary>
/// <param name="pluginContext">PVOID.</param>
/// <param name="requestDetails">WSMAN_PLUGIN_REQUEST*.</param>
/// <param name="flags">DWORD.</param>
/// <param name="shellContext">PVOID.</param>
/// <param name="commandContext">PVOID optional.</param>
/// <param name="inboundConnectInformation">WSMAN_DATA* optional.</param>
public static void WSManPluginConnect(
IntPtr pluginContext,
IntPtr requestDetails,
int flags,
IntPtr shellContext,
IntPtr commandContext,
IntPtr inboundConnectInformation)
{
if (pluginContext == IntPtr.Zero)
{
WSManPluginInstance.ReportOperationComplete(
requestDetails,
WSManPluginErrorCodes.NullPluginContext,
StringUtil.Format(
RemotingErrorIdStrings.WSManPluginNullPluginContext,
"pluginContext",
"WSManPluginConnect")
);
return;
}
WSManPluginInstance.PerformWSManPluginConnect(pluginContext, requestDetails, flags, shellContext, commandContext, inboundConnectInformation);
}
/// <summary>
/// </summary>
/// <param name="pluginContext">PVOID.</param>
/// <param name="requestDetails">WSMAN_PLUGIN_REQUEST*.</param>
/// <param name="flags">DWORD.</param>
/// <param name="extraInfo">PCWSTR.</param>
/// <param name="startupInfo">WSMAN_SHELL_STARTUP_INFO*.</param>
/// <param name="inboundShellInformation">WSMAN_DATA*.</param>
public static void WSManPluginShell(
IntPtr pluginContext,
IntPtr requestDetails,
int flags,
[MarshalAs(UnmanagedType.LPWStr)] string extraInfo,
IntPtr startupInfo,
IntPtr inboundShellInformation)
{
if (pluginContext == IntPtr.Zero)
{
WSManPluginInstance.ReportOperationComplete(
requestDetails,
WSManPluginErrorCodes.NullPluginContext,
StringUtil.Format(
RemotingErrorIdStrings.WSManPluginNullPluginContext,
"pluginContext",
"WSManPluginShell")
);
return;
}
#if(DEBUG)
// In debug builds, allow remote runspaces to wait for debugger attach
if (Environment.GetEnvironmentVariable("__PSRemoteRunspaceWaitForDebugger", EnvironmentVariableTarget.Machine) != null)
{
bool debuggerAttached = false;
while (!debuggerAttached)
{
System.Threading.Thread.Sleep(100);
}
}
#endif
WSManPluginInstance.PerformWSManPluginShell(pluginContext, requestDetails, flags, extraInfo, startupInfo, inboundShellInformation);
}
/// <summary>
/// </summary>
/// <param name="pluginContext">PVOID.</param>
/// <param name="shellContext">PVOID.</param>
public static void WSManPluginReleaseShellContext(
IntPtr pluginContext,
IntPtr shellContext)
{
// NO-OP..as our plugin does not own the memory related
// to shellContext and so there is nothing to release
}
/// <summary>
/// </summary>
/// <param name="pluginContext">PVOID.</param>
/// <param name="requestDetails">WSMAN_PLUGIN_REQUEST*.</param>
/// <param name="flags">DWORD.</param>
/// <param name="shellContext">PVOID.</param>
/// <param name="commandLine">PCWSTR.</param>
/// <param name="arguments">WSMAN_COMMAND_ARG_SET* optional.</param>
public static void WSManPluginCommand(
IntPtr pluginContext,
IntPtr requestDetails,
int flags,
IntPtr shellContext,
[MarshalAs(UnmanagedType.LPWStr)] string commandLine,
IntPtr arguments)
{
if (pluginContext == IntPtr.Zero)
{
WSManPluginInstance.ReportOperationComplete(
requestDetails,
WSManPluginErrorCodes.NullPluginContext,
StringUtil.Format(
RemotingErrorIdStrings.WSManPluginNullPluginContext,
"Plugin Context",
"WSManPluginCommand")
);
return;
}
WSManPluginInstance.PerformWSManPluginCommand(pluginContext, requestDetails, flags, shellContext, commandLine, arguments);
}
/// <summary>
/// Operation shutdown notification that was registered with the native layer for each of the shellCreate operations.
/// </summary>
/// <param name="shutdownContext">IntPtr.</param>
public static void WSManPSShutdown(
IntPtr shutdownContext)
{
GCHandle gch = GCHandle.FromIntPtr(shutdownContext);
EventWaitHandle eventHandle = (EventWaitHandle)gch.Target;
eventHandle.Set();
gch.Free();
}
/// <summary>
/// </summary>
/// <param name="pluginContext">PVOID.</param>
/// <param name="shellContext">PVOID.</param>
/// <param name="commandContext">PVOID.</param>
public static void WSManPluginReleaseCommandContext(
IntPtr pluginContext,
IntPtr shellContext,
IntPtr commandContext)
{
// NO-OP..as our plugin does not own the memory related
// to commandContext and so there is nothing to release.
}
/// <summary>
/// </summary>
/// <param name="pluginContext">PVOID.</param>
/// <param name="requestDetails">WSMAN_PLUGIN_REQUEST*.</param>
/// <param name="flags">DWORD.</param>
/// <param name="shellContext">PVOID.</param>
/// <param name="commandContext">PVOID.</param>
/// <param name="stream">PCWSTR.</param>
/// <param name="inboundData">WSMAN_DATA*.</param>
public static void WSManPluginSend(
IntPtr pluginContext,
IntPtr requestDetails,
int flags,
IntPtr shellContext,
IntPtr commandContext,
[MarshalAs(UnmanagedType.LPWStr)] string stream,
IntPtr inboundData)
{
if (pluginContext == IntPtr.Zero)
{
WSManPluginInstance.ReportOperationComplete(
requestDetails,
WSManPluginErrorCodes.NullPluginContext,
StringUtil.Format(
RemotingErrorIdStrings.WSManPluginNullPluginContext,
"Plugin Context",
"WSManPluginSend")
);
return;
}
WSManPluginInstance.PerformWSManPluginSend(pluginContext, requestDetails, flags, shellContext, commandContext, stream, inboundData);
}
/// <summary>
/// </summary>
/// <param name="pluginContext">PVOID.</param>
/// <param name="requestDetails">WSMAN_PLUGIN_REQUEST*.</param>
/// <param name="flags">DWORD.</param>
/// <param name="shellContext">PVOID.</param>
/// <param name="commandContext">PVOID optional.</param>
/// <param name="streamSet">WSMAN_STREAM_ID_SET* optional.</param>
public static void WSManPluginReceive(
IntPtr pluginContext,
IntPtr requestDetails,
int flags,
IntPtr shellContext,
IntPtr commandContext,
IntPtr streamSet)
{
if (pluginContext == IntPtr.Zero)
{
WSManPluginInstance.ReportOperationComplete(
requestDetails,
WSManPluginErrorCodes.NullPluginContext,
StringUtil.Format(
RemotingErrorIdStrings.WSManPluginNullPluginContext,
"Plugin Context",
"WSManPluginReceive")
);
return;
}
WSManPluginInstance.PerformWSManPluginReceive(pluginContext, requestDetails, flags, shellContext, commandContext, streamSet);
}
/// <summary>
/// </summary>
/// <param name="pluginContext">PVOID.</param>
/// <param name="requestDetails">WSMAN_PLUGIN_REQUEST*.</param>
/// <param name="flags">DWORD.</param>
/// <param name="shellContext">PVOID.</param>
/// <param name="commandContext">PVOID optional.</param>
/// <param name="code">PCWSTR.</param>
public static void WSManPluginSignal(
IntPtr pluginContext,
IntPtr requestDetails,
int flags,
IntPtr shellContext,
IntPtr commandContext,
[MarshalAs(UnmanagedType.LPWStr)] string code)
{
if ((pluginContext == IntPtr.Zero) || (shellContext == IntPtr.Zero))
{
WSManPluginInstance.ReportOperationComplete(
requestDetails,
WSManPluginErrorCodes.NullPluginContext,
StringUtil.Format(
RemotingErrorIdStrings.WSManPluginNullPluginContext,
"Plugin Context",
"WSManPluginSignal")
);
return;
}
WSManPluginInstance.PerformWSManPluginSignal(pluginContext, requestDetails, flags, shellContext, commandContext, code);
}
/// <summary>
/// Callback used to register with thread pool to notify when a plugin operation shuts down.
/// Conforms to:
/// public delegate void WaitOrTimerCallback( Object state, bool timedOut )
/// </summary>
/// <param name="operationContext">PVOID.</param>
/// <param name="timedOut">BOOLEAN.</param>
/// <returns></returns>
public static void PSPluginOperationShutdownCallback(
object operationContext,
bool timedOut)
{
if (operationContext == null)
{
return;
}
WSManPluginOperationShutdownContext context = (WSManPluginOperationShutdownContext)operationContext;
context.isShuttingDown = true;
WSManPluginInstance.PerformCloseOperation(context);
}
#endregion
}
/// <summary>
/// This is a thin wrapper around WSManPluginManagedEntryWrapper.InitPlugin()
/// so that it can be called from native COM code in a non-static context.
///
/// This was done to get around an FXCop error: AvoidStaticMembersInComVisibleTypes.
/// </summary>
public sealed class WSManPluginManagedEntryInstanceWrapper : IDisposable
{
#region IDisposable
// Flag: Has Dispose already been called?
private bool _disposed = false;
/// <summary>
/// Internal implementation of Dispose pattern callable by consumers.
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Protected implementation of Dispose pattern.
/// </summary>
/// <param name="disposing"></param>
private void Dispose(bool disposing)
{
if (_disposed)
return;
// Free any unmanaged objects here.
_initDelegateHandle.Free();
_disposed = true;
}
/// <summary>
/// Finalizes an instance of the <see cref="WSManPluginManagedEntryInstanceWrapper"/> class.
/// </summary>
~WSManPluginManagedEntryInstanceWrapper()
{
Dispose(false);
}
#endregion
#region Delegate Handling
/// <summary>
/// Matches signature for WSManPluginManagedEntryWrapper.InitPlugin.
/// </summary>
/// <param name="wkrPtrs"></param>
/// <returns></returns>
private delegate int InitPluginDelegate(
IntPtr wkrPtrs);
/// <summary>
/// Prevents the delegate object from being garbage collected so it can be passed to the native code.
/// </summary>
private GCHandle _initDelegateHandle;
/// <summary>
/// Entry point for native code that cannot call static methods.
/// </summary>
/// <returns>A function pointer for the static entry point for the WSManPlugin initialization function.</returns>
public IntPtr GetEntryDelegate()
{
InitPluginDelegate initDelegate = new InitPluginDelegate(WSManPluginManagedEntryWrapper.InitPlugin);
_initDelegateHandle = GCHandle.Alloc(initDelegate);
return Marshal.GetFunctionPointerForDelegate(initDelegate);
}
#endregion
}
}
|