Skip to content

Commit 69c0fd8

Browse files
authored
Merge pull request #78 from Flow-Launcher/fix_win32_loading
Fix win32 app loading
2 parents 67847eb + 7e4ac1c commit 69c0fd8

File tree

8 files changed

+329
-119
lines changed

8 files changed

+329
-119
lines changed

Plugins/Flow.Launcher.Plugin.Program/Flow.Launcher.Plugin.Program.csproj

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,6 @@
4646
<HintPath>.\AppxPackagingTlb.dll</HintPath>
4747
<EmbedInteropTypes>True</EmbedInteropTypes>
4848
</Reference>
49-
<Reference Include="ShObjIdlTlb">
50-
<HintPath>.\ShObjIdlTlb.dll</HintPath>
51-
<EmbedInteropTypes>True</EmbedInteropTypes>
52-
</Reference>
5349
</ItemGroup>
5450

5551
<ItemGroup>

Plugins/Flow.Launcher.Plugin.Program/Main.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,6 @@ private void DisableProgram(IProgram programToDelete)
228228

229229
public static void StartProcess(Func<ProcessStartInfo, Process> runProcess, ProcessStartInfo info)
230230
{
231-
bool hide;
232231
try
233232
{
234233
runProcess(info);
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System;
2+
using System.Diagnostics;
3+
using System.Runtime.CompilerServices;
4+
using System.Runtime.InteropServices;
5+
using System.Text.RegularExpressions;
6+
7+
namespace Flow.Launcher.Plugin.Program.Programs
8+
{
9+
public class ApplicationActivationHelper
10+
{
11+
// Reference : https://github.com/MicrosoftEdge/edge-launcher/blob/108e63df0b4cb5cd9d5e45aa7a264690851ec51d/MIcrosoftEdgeLauncherCsharp/Program.cs
12+
public enum ActivateOptions
13+
{
14+
None = 0x00000000,
15+
DesignMode = 0x00000001,
16+
NoErrorUI = 0x00000002,
17+
NoSplashScreen = 0x00000004,
18+
}
19+
20+
/// ApplicationActivationManager
21+
[ComImport, Guid("2e941141-7f97-4756-ba1d-9decde894a3d"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
22+
interface IApplicationActivationManager
23+
{
24+
IntPtr ActivateApplication([In] string appUserModelId, [In] string arguments, [In] ActivateOptions options, [Out] out uint processId);
25+
IntPtr ActivateForFile([In] string appUserModelId, [In] IntPtr /*IShellItemArray* */ itemArray, [In] string verb, [Out] out uint processId);
26+
IntPtr ActivateForProtocol([In] string appUserModelId, [In] IntPtr /* IShellItemArray* */itemArray, [Out] out uint processId);
27+
}
28+
29+
// Application Activation Manager Class
30+
[ComImport, Guid("45BA127D-10A8-46EA-8AB7-56EA9078943C")]
31+
public class ApplicationActivationManager : IApplicationActivationManager
32+
{
33+
34+
[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)/*, PreserveSig*/]
35+
public extern IntPtr ActivateApplication([In] string appUserModelId, [In] string arguments, [In] ActivateOptions options, [Out] out uint processId);
36+
37+
[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
38+
public extern IntPtr ActivateForFile([In] string appUserModelId, [In] IntPtr /*IShellItemArray* */ itemArray, [In] string verb, [Out] out uint processId);
39+
40+
[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
41+
public extern IntPtr ActivateForProtocol([In] string appUserModelId, [In] IntPtr /* IShellItemArray* */itemArray, [Out] out uint processId);
42+
}
43+
}
44+
}
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using System.Runtime.InteropServices;
5+
using System.IO;
6+
using Accessibility;
7+
using System.Runtime.InteropServices.ComTypes;
8+
using System.Security.Policy;
9+
10+
namespace Flow.Launcher.Plugin.Program.Programs
11+
{
12+
class ShellLinkHelper
13+
{
14+
[Flags()]
15+
public enum SLGP_FLAGS
16+
{
17+
SLGP_SHORTPATH = 0x1,
18+
SLGP_UNCPRIORITY = 0x2,
19+
SLGP_RAWPATH = 0x4
20+
}
21+
22+
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
23+
public struct WIN32_FIND_DATAW
24+
{
25+
public uint dwFileAttributes;
26+
public long ftCreationTime;
27+
public long ftLastAccessTime;
28+
public long ftLastWriteTime;
29+
public uint nFileSizeHigh;
30+
public uint nFileSizeLow;
31+
public uint dwReserved0;
32+
public uint dwReserved1;
33+
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
34+
public string cFileName;
35+
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 14)]
36+
public string cAlternateFileName;
37+
}
38+
39+
[Flags()]
40+
public enum SLR_FLAGS
41+
{
42+
SLR_NO_UI = 0x1,
43+
SLR_ANY_MATCH = 0x2,
44+
SLR_UPDATE = 0x4,
45+
SLR_NOUPDATE = 0x8,
46+
SLR_NOSEARCH = 0x10,
47+
SLR_NOTRACK = 0x20,
48+
SLR_NOLINKINFO = 0x40,
49+
SLR_INVOKE_MSI = 0x80
50+
}
51+
52+
53+
// Reference : http://www.pinvoke.net/default.aspx/Interfaces.IShellLinkW
54+
/// The IShellLink interface allows Shell links to be created, modified, and resolved
55+
[ComImport(), InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("000214F9-0000-0000-C000-000000000046")]
56+
interface IShellLinkW
57+
{
58+
/// <summary>Retrieves the path and file name of a Shell link object</summary>
59+
void GetPath([Out(), MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszFile, int cchMaxPath, ref WIN32_FIND_DATAW pfd, SLGP_FLAGS fFlags);
60+
/// <summary>Retrieves the list of item identifiers for a Shell link object</summary>
61+
void GetIDList(out IntPtr ppidl);
62+
/// <summary>Sets the pointer to an item identifier list (PIDL) for a Shell link object.</summary>
63+
void SetIDList(IntPtr pidl);
64+
/// <summary>Retrieves the description string for a Shell link object</summary>
65+
void GetDescription([Out(), MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszName, int cchMaxName);
66+
/// <summary>Sets the description for a Shell link object. The description can be any application-defined string</summary>
67+
void SetDescription([MarshalAs(UnmanagedType.LPWStr)] string pszName);
68+
/// <summary>Retrieves the name of the working directory for a Shell link object</summary>
69+
void GetWorkingDirectory([Out(), MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszDir, int cchMaxPath);
70+
/// <summary>Sets the name of the working directory for a Shell link object</summary>
71+
void SetWorkingDirectory([MarshalAs(UnmanagedType.LPWStr)] string pszDir);
72+
/// <summary>Retrieves the command-line arguments associated with a Shell link object</summary>
73+
void GetArguments([Out(), MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszArgs, int cchMaxPath);
74+
/// <summary>Sets the command-line arguments for a Shell link object</summary>
75+
void SetArguments([MarshalAs(UnmanagedType.LPWStr)] string pszArgs);
76+
/// <summary>Retrieves the hot key for a Shell link object</summary>
77+
void GetHotkey(out short pwHotkey);
78+
/// <summary>Sets a hot key for a Shell link object</summary>
79+
void SetHotkey(short wHotkey);
80+
/// <summary>Retrieves the show command for a Shell link object</summary>
81+
void GetShowCmd(out int piShowCmd);
82+
/// <summary>Sets the show command for a Shell link object. The show command sets the initial show state of the window.</summary>
83+
void SetShowCmd(int iShowCmd);
84+
/// <summary>Retrieves the location (path and index) of the icon for a Shell link object</summary>
85+
void GetIconLocation([Out(), MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszIconPath,
86+
int cchIconPath, out int piIcon);
87+
/// <summary>Sets the location (path and index) of the icon for a Shell link object</summary>
88+
void SetIconLocation([MarshalAs(UnmanagedType.LPWStr)] string pszIconPath, int iIcon);
89+
/// <summary>Sets the relative path to the Shell link object</summary>
90+
void SetRelativePath([MarshalAs(UnmanagedType.LPWStr)] string pszPathRel, int dwReserved);
91+
/// <summary>Attempts to find the target of a Shell link, even if it has been moved or renamed</summary>
92+
void Resolve(ref Accessibility._RemotableHandle hwnd, SLR_FLAGS fFlags);
93+
/// <summary>Sets the path and file name of a Shell link object</summary>
94+
void SetPath([MarshalAs(UnmanagedType.LPWStr)] string pszFile);
95+
}
96+
97+
[ComImport(), Guid("00021401-0000-0000-C000-000000000046")]
98+
public class ShellLink
99+
{
100+
}
101+
102+
// To initialize the app description
103+
public String description = String.Empty;
104+
105+
106+
// Retrieve the target path using Shell Link
107+
public string retrieveTargetPath(string path)
108+
{
109+
var link = new ShellLink();
110+
const int STGM_READ = 0;
111+
((IPersistFile)link).Load(path, STGM_READ);
112+
var hwnd = new _RemotableHandle();
113+
((IShellLinkW)link).Resolve(ref hwnd, 0);
114+
115+
const int MAX_PATH = 260;
116+
StringBuilder buffer = new StringBuilder(MAX_PATH);
117+
118+
var data = new WIN32_FIND_DATAW();
119+
((IShellLinkW)link).GetPath(buffer, buffer.Capacity, ref data, SLGP_FLAGS.SLGP_SHORTPATH);
120+
var target = buffer.ToString();
121+
122+
// To set the app description
123+
if (!String.IsNullOrEmpty(target))
124+
{
125+
buffer = new StringBuilder(MAX_PATH);
126+
((IShellLinkW)link).GetDescription(buffer, MAX_PATH);
127+
description = buffer.ToString();
128+
}
129+
return target;
130+
}
131+
}
132+
}

Plugins/Flow.Launcher.Plugin.Program/Programs/UWP.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
using Windows.ApplicationModel;
1414
using Windows.Management.Deployment;
1515
using AppxPackaing;
16-
using Shell;
1716
using Flow.Launcher.Infrastructure;
1817
using Flow.Launcher.Plugin.Program.Logger;
1918
using IStream = AppxPackaing.IStream;
@@ -350,10 +349,10 @@ public List<Result> ContextMenus(IPublicAPI api)
350349

351350
private async void Launch(IPublicAPI api)
352351
{
353-
var appManager = new ApplicationActivationManager();
352+
var appManager = new ApplicationActivationHelper.ApplicationActivationManager();
354353
uint unusedPid;
355354
const string noArgs = "";
356-
const ACTIVATEOPTIONS noFlags = ACTIVATEOPTIONS.AO_NONE;
355+
const ApplicationActivationHelper.ActivateOptions noFlags = ApplicationActivationHelper.ActivateOptions.None;
357356
await Task.Run(() =>
358357
{
359358
try

0 commit comments

Comments
 (0)