Skip to content

powershell should run ShellExecuteEx on STA thread #2969

Closed

Description

[Edited by Dongbo Wang (@daxian-dbw)]

PR #3281 adds support to ShellExecute in powershell core when it's running on Windows full SKUs. With that fix, Start-Process is able to run MSI in powershell core.

However, it's not a complete solution. It calls ShellExecuteEx directly in MTA thread instead of from STA thread as suggested in MSDN. Therefore, the current solution may not work for some shell extensions that require COM.

In .NET Core, managed threads are all eagerly initialized with MTA mode, so to call ShellExecuteEx from a STA thread, we need to create a native thread using CreateThread function and initialize COM with STA on that thread. In #3281, we are calling ShellExecuteEx directly on MTA thread, and it works for things like opening a folder in File Explorer, opening a file with the application that is associated with its extension in shell, opening URL in web browser and etc, but it's not guaranteed to work in all ShellExecution scenarios.

Will keep this issue open to track the "invoke-on-STA-thread" work.


PS:4> Start-Process .\PowerShell_6.0.0-alpha.17-win10-win2016-x64.msi -PassThru

 NPM(K)    PM(M)      WS(M)     CPU(s)     Id  SI ProcessName
 ------    -----      -----     ------     --  -- -----------
      9     6.15       7.55       0.03  20092   3 msiexec

PS:5> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      6.0.0-alpha
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
PSEdition                      Core
GitCommitId                    v6.0.0-alpha.16-54-gc86a287726fe366895723dfcc7739f93038615ea
CLRVersion
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
SerializationVersion           1.1.0.1
BuildVersion                   3.0.0.0

Activity

  1. iSazonov commented on Jan 6, 2017

    @iSazonov
    Collaborator

    I test this for .txt - it don't work too. Seems windows type associations don't work here at all.

  2. mattpwhite commented on Jan 11, 2017

    @mattpwhite

    The .NET Core version of System.Diagnostics.Process doesn't support using ShellExecute() to start processes. The .NET Framework version does and does so by default, with an alternate path that uses CreateProcess(). ShellExecute is what makes starting a file launch its associated executable (and other stuff like starting a URL launching your default browser). Have to imagine this is a breaking change for a lot of existing scripts (and that it's probably not trivial to fix).

    https://referencesource.microsoft.com/#System/services/monitoring/system/diagnosticts/ProcessStartInfo.cs,48

    https://github.com/dotnet/corefx/blob/912cba3675a15718515417e7e9b46504c0303fbc/src/System.Diagnostics.Process/src/System/Diagnostics/ProcessStartInfo.Windows.cs#L85

  3. daxian-dbw commented on Mar 11, 2017

    @daxian-dbw
    Member

    #3281 is to add ShellExecute support in powershell core.
    It's not a complete solution however. It calls ShellExecuteEx directly in MTA thread instead of from STA thread as suggested in MSDN. Therefore, the current solution may not work for some shell extensions that require COM.

    In .NET Core, managed threads are all eagerly initialized with MTA mode, so to call ShellExecuteEx from a STA thread, we need to create a native thread using CreateThread function and initialize COM with STA on that thread. In #3281, we are calling ShellExecuteEx directly on MTA thread, and it works for things like opening a folder in File Explorer, opening a file with the application that is associated with its extension in shell, opening URL in web browser and etc, but it's not guaranteed to work in all ShellExecution scenarios.

    I will keep this issue open even after merging that PR to track the "invoke-on-STA-thread" work.

  4. changed the title [-]Start-Process will not execute an MSI[/-] [+]powershell should run `ShellExecuteEx` on STA thread[/+] on Mar 14, 2017
  5. modified the milestones: , 6.0.0-beta1 on Mar 15, 2017
  6. iSazonov commented on Mar 18, 2017

    @iSazonov
    Collaborator

    CoreFX issue https://github.com/dotnet/corefx/issues/522 milestone is 1.0.0-rtm so I set "Waiting - NetStandard20"

  7. joeyaiello commented on Mar 27, 2017

    @joeyaiello
    ContributorAuthor

    Ilya (@iSazonov) So I'll be the first to admit that this thread is totally over my head, but 1.0.0-rtm in the CoreFX repo implies that that work already shipped in .NET Core 1.0 RTM.

    Is .NET Standard 2.0 still relevant here?

  8. iSazonov commented on Mar 27, 2017

    @iSazonov
    Collaborator

    I can't still find full support STA in CoreFX 😕

  9. daxian-dbw commented on Mar 27, 2017

    @daxian-dbw
    Member

    The CoreFX issue was not addressed. It was closed as I quoted here:

    We should consider this when we have a concrete motivating scenario that requires it.

  10. jeffbi commented on Mar 27, 2017

    @jeffbi

    If it helps motivate them, without STA we cannot use IGroupPolicyObject (#3353)

  11. daxian-dbw commented on Mar 28, 2017

    @daxian-dbw
    Member

    jeffbi Please leave a comment in the CoreFX issue. Maybe even reactivate the issue if you think it's a concrete scenario that should be supported in .NET Core.

  12. 4 remaining items

  13. modified the milestones: , on May 15, 2017
  14. modified the milestones: , on May 16, 2017
  15. modified the milestones: , on Jul 26, 2017
  16. SteveL-MSFT commented on Jul 26, 2017

    @SteveL-MSFT
    Member

    STA is now in corefx

  17. daxian-dbw commented on Jul 26, 2017

    @daxian-dbw
    Member

    Sweet! We need to clean up ApartmentState related workarounds in powershell now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions