33using System . Runtime . InteropServices ;
44using Windows . Win32 ;
55using Windows . Win32 . Foundation ;
6- using Windows . Win32 . System . LibraryLoader ;
76
87namespace Flow . Launcher . Plugin . Program . Programs
98{
@@ -21,46 +20,28 @@ public static class ShellLocalization
2120 /// <returns>The localized name as string or <see cref="string.Empty"/>.</returns>
2221 public static unsafe string GetLocalizedName ( string path )
2322 {
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 )
23+ int retCode = PInvoke . SHCreateItemFromParsingName ( path , null , typeof ( Windows . Win32 . UI . Shell . IShellItem ) . GUID , out object shellItemObj ) ;
24+ if ( retCode != 0 || shellItemObj is not Windows . Win32 . UI . Shell . IShellItem shellItem )
2925 {
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- }
26+ return string . Empty ;
6127 }
6228
63- return string . Empty ;
29+ try
30+ {
31+ PWSTR displayName ;
32+ shellItem . GetDisplayName ( Windows . Win32 . UI . Shell . SIGDN . SIGDN_NORMALDISPLAY , & displayName ) ;
33+ string filename = displayName . ToString ( ) ;
34+ PInvoke . CoTaskMemFree ( displayName ) ;
35+ return filename ;
36+ }
37+ catch
38+ {
39+ return string . Empty ;
40+ }
41+ finally
42+ {
43+ Marshal . ReleaseComObject ( shellItem ) ;
44+ }
6445 }
6546
6647 /// <summary>
0 commit comments