Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ internal static void CreateElevatedEntry(string title)
// Check startupInfo first to know if the current shell is interactive and owns a window before proceeding
Comment thread
bergmeister marked this conversation as resolved.
Resolved
Comment thread
bergmeister marked this conversation as resolved.
Resolved
Comment thread
bergmeister marked this conversation as resolved.
Resolved
Comment thread
bergmeister marked this conversation as resolved.
Resolved
Comment thread
bergmeister marked this conversation as resolved.
Resolved
// This check is fast (less than 1ms) and allows for quick-exit
GetStartupInfo(out StartUpInfo startupInfo);
Console.WriteLine("GetStartupInfo");
var STARTF_USESHOWWINDOW = 0x00000001;
var SW_HIDE = 0;
if (((startupInfo.dwFlags & STARTF_USESHOWWINDOW) == 1) && (startupInfo.wShowWindow != SW_HIDE))
Expand All @@ -27,16 +28,20 @@ internal static void CreateElevatedEntry(string title)
const uint CLSCTX_INPROC_SERVER = 1;
var IID_IUnknown = new Guid("00000000-0000-0000-C000-000000000046");
var hResult = CoCreateInstance(ref CLSID_DestinationList, null, CLSCTX_INPROC_SERVER, ref IID_IUnknown, out object pCustDestListobj);
Console.WriteLine("CoCreateInstance");
if (hResult < 0)
{
Console.WriteLine($"Creating ICustomDestinationList failed with HResult '{hResult}'.");
Debug.Fail($"Creating ICustomDestinationList failed with HResult '{hResult}'.");
return;
}

var pCustDestList = (ICustomDestinationList)pCustDestListobj;
Console.WriteLine("BeginList");
hResult = pCustDestList.BeginList(out uint uMaxSlots, new Guid(@"92CA9DCD-5622-4BBA-A805-5E9F541BD8C9"), out object pRemovedItems);
if (hResult < 0)
{
Console.WriteLine($"BeginList on ICustomDestinationList failed with HResult '{hResult}'.");
Debug.Fail($"BeginList on ICustomDestinationList failed with HResult '{hResult}'.");
return;
}
Expand All @@ -55,16 +60,20 @@ internal static void CreateElevatedEntry(string title)
shellLinkDataList.SetFlags(flags);
var PKEY_TITLE = new PropertyKey(new Guid("{F29F85E0-4FF9-1068-AB91-08002B27B3D9}"), 2);
hResult = nativePropertyStore.SetValue(ref PKEY_TITLE, new PropVariant(title));
Console.WriteLine("SetValue");
if (hResult < 0)
{
Console.WriteLine($"SetValue on IPropertyStore with title '{title}' failed with HResult '{hResult}'.");
pCustDestList.AbortList();
Debug.Fail($"SetValue on IPropertyStore with title '{title}' failed with HResult '{hResult}'.");
return;
}

hResult = nativePropertyStore.Commit();
Console.WriteLine("Commit");
if (hResult < 0)
{
Console.WriteLine($"Commit on IPropertyStore failed with HResult '{hResult}'.");
pCustDestList.AbortList();
Debug.Fail($"Commit on IPropertyStore failed with HResult '{hResult}'.");
return;
Expand All @@ -76,26 +85,32 @@ internal static void CreateElevatedEntry(string title)
const uint CLSCTX_INPROC = CLSCTX_INPROC_SERVER | CLSCTX_INPROC_HANDLER;
var ComSvrInterface_GUID = new Guid(@"555E2D2B-EE00-47AA-AB2B-39F953F6B339");
hResult = CoCreateInstance(ref CLSID_EnumerableObjectCollection, null, CLSCTX_INPROC, ref IID_IUnknown, out object instance);
Console.WriteLine("CoCreateInstance");
if (hResult < 0)
{
Console.WriteLine($"Creating IObjectCollection failed with HResult '{hResult}'.");
pCustDestList.AbortList();
Debug.Fail($"Creating IObjectCollection failed with HResult '{hResult}'.");
return;
}

var pShortCutCollection = (IObjectCollection)instance;
pShortCutCollection.AddObject((IShellLinkW)nativePropertyStore);
Console.WriteLine("AddObject");

// Add collection to custom destination list and commit the result
hResult = pCustDestList.AddUserTasks((IObjectArray)pShortCutCollection);
Console.WriteLine("AddUserTasks");
if (hResult < 0)
{
Console.WriteLine($"AddUserTasks on ICustomDestinationList failed with HResult '{hResult}'.");
pCustDestList.AbortList();
Debug.Fail($"AddUserTasks on ICustomDestinationList failed with HResult '{hResult}'.");
return;
}

pCustDestList.CommitList();
Console.WriteLine("CommitList");
}
}
}
Expand Down
22 changes: 11 additions & 11 deletions src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,19 +200,19 @@ internal static int Start(
// The JumpList does persist as long as the filepath of the executable does not change but there
// could be disruptions to it like e.g. the bi-annual Windows update, we decided to
// not over-optimize this and always create the JumpList as a non-blocking background task instead.
Task.Run(() =>
try
{
try
{
TaskbarJumpList.CreateElevatedEntry(ConsoleHostStrings.RunAsAdministrator);
}
catch
{
// Sporadic failures have been observed in some environments. Since the JumpList persists once it
// has been registered, there is no harm suppressing the occasional failure.
}
// APIs are STA only and until issue 7216 is not resolved, PowerShell operates still in MTA mode.
var thread = new Thread(() => TaskbarJumpList.CreateElevatedEntry(ConsoleHostStrings.RunAsAdministrator));
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
}
catch (Exception exception)
{
// Sporadic failures have been observed in some environments. Since the JumpList persists once it
// has been registered, there is no harm suppressing the occasional failure.
Console.WriteLine($"Exception '{exception}' with stack trace {exception.StackTrace} successfully caught. Please report this in issue 9295.");
}
);
#endif

// First check for and handle PowerShell running in a server mode.
Expand Down