Skip to content

Commit 3e32fca

Browse files
Fix localized name get failure
1 parent 8092a44 commit 3e32fca

2 files changed

Lines changed: 13 additions & 40 deletions

File tree

Plugins/Flow.Launcher.Plugin.Program/NativeMethods.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,7 @@ S_OK
77
SLGP_FLAGS
88
WIN32_FIND_DATAW
99
SLR_FLAGS
10-
IShellLinkW
10+
IShellItem
11+
SHCreateItemFromParsingName
12+
IShellLinkW
13+
CoTaskMemFree

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

Lines changed: 9 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
using System;
22
using System.IO;
3-
using System.Runtime.InteropServices;
43
using Windows.Win32;
54
using Windows.Win32.Foundation;
6-
using Windows.Win32.System.LibraryLoader;
75

86
namespace Flow.Launcher.Plugin.Program.Programs
97
{
@@ -21,46 +19,18 @@ public static class ShellLocalization
2119
/// <returns>The localized name as string or <see cref="string.Empty"/>.</returns>
2220
public static unsafe string GetLocalizedName(string path)
2321
{
24-
const int capacity = 1024;
25-
Span<char> buffer = new char[capacity];
26-
27-
// If there is no resource to localize a file name the method returns a non zero value.
28-
fixed (char* bufferPtr = buffer)
22+
int retCode = PInvoke.SHCreateItemFromParsingName(path, null, typeof(Windows.Win32.UI.Shell.IShellItem).GUID, out object shellItemObj);
23+
if (retCode != 0 || shellItemObj is not Windows.Win32.UI.Shell.IShellItem shellItem)
2924
{
30-
int id;
31-
fixed (char* pathPtr = path)
32-
{
33-
var result = PInvoke.SHGetLocalizedName(new PCWSTR(pathPtr), bufferPtr, capacity, &id);
34-
35-
if (result != HRESULT.S_OK)
36-
{
37-
return string.Empty;
38-
}
39-
40-
var resourcePathStr = MemoryMarshal.CreateReadOnlySpanFromNullTerminated(bufferPtr).ToString();
41-
fixed (char* resourcePathPtr = resourcePathStr)
42-
{
43-
_ = PInvoke.ExpandEnvironmentStrings(new PCWSTR(resourcePathPtr), bufferPtr, capacity);
44-
using var handle = PInvoke.LoadLibraryEx(resourcePathStr,
45-
LOAD_LIBRARY_FLAGS.DONT_RESOLVE_DLL_REFERENCES | LOAD_LIBRARY_FLAGS.LOAD_LIBRARY_AS_DATAFILE);
46-
if (handle.IsInvalid)
47-
{
48-
return string.Empty;
49-
}
50-
51-
// not sure about the behavior of Pinvoke.LoadString, so we clear the buffer before using it (so it must be a null-terminated string)
52-
buffer.Clear();
53-
54-
if (PInvoke.LoadString(handle, (uint)id, buffer, capacity) != 0)
55-
{
56-
var lString = MemoryMarshal.CreateReadOnlySpanFromNullTerminated(bufferPtr).ToString();
57-
return lString;
58-
}
59-
}
60-
}
25+
return string.Empty;
6126
}
6227

63-
return string.Empty;
28+
PWSTR displayName;
29+
shellItem.GetDisplayName(Windows.Win32.UI.Shell.SIGDN.SIGDN_NORMALDISPLAY, &displayName);
30+
string filename = displayName.ToString();
31+
PInvoke.CoTaskMemFree(displayName);
32+
33+
return filename;
6434
}
6535

6636
/// <summary>

0 commit comments

Comments
 (0)