@@ -38,11 +38,7 @@ public unsafe string retrieveTargetPath(string path)
3838 fixed ( char * bufferChar = buffer )
3939 {
4040 ( ( IShellLinkW ) link ) . GetPath ( ( PWSTR ) bufferChar , MAX_PATH , & data , ( uint ) SLGP_FLAGS . SLGP_SHORTPATH ) ;
41-
42- // Truncate the buffer to the actual length of the string
43- int validLength = Array . IndexOf ( buffer , '\0 ' ) ;
44- if ( validLength < 0 ) validLength = MAX_PATH ;
45- target = new string ( buffer , 0 , validLength ) ;
41+ target = GetStringFromBuffer ( buffer , MAX_PATH ) ;
4642 }
4743
4844 // To set the app description
@@ -54,9 +50,7 @@ public unsafe string retrieveTargetPath(string path)
5450 fixed ( char * buffer1Char = buffer1 )
5551 {
5652 ( ( IShellLinkW ) link ) . GetDescription ( ( PWSTR ) buffer1Char , MAX_PATH ) ;
57- int validLength = Array . IndexOf ( buffer1 , '\0 ' ) ;
58- if ( validLength < 0 ) validLength = MAX_PATH ;
59- description = new string ( buffer1 , 0 , validLength ) ;
53+ description = GetStringFromBuffer ( buffer1 , MAX_PATH ) ;
6054 }
6155 }
6256 catch ( COMException e )
@@ -71,9 +65,7 @@ public unsafe string retrieveTargetPath(string path)
7165 fixed ( char * buffer2Char = buffer2 )
7266 {
7367 ( ( IShellLinkW ) link ) . GetArguments ( ( PWSTR ) buffer2Char , MAX_PATH ) ;
74- int validLength = Array . IndexOf ( buffer2 , '\0 ' ) ;
75- if ( validLength < 0 ) validLength = MAX_PATH ;
76- arguments = new string ( buffer2 , 0 , validLength ) ;
68+ arguments = GetStringFromBuffer ( buffer2 , MAX_PATH ) ;
7769 }
7870 }
7971
@@ -82,5 +74,13 @@ public unsafe string retrieveTargetPath(string path)
8274
8375 return target ;
8476 }
77+
78+ private static unsafe string GetStringFromBuffer ( char [ ] buffer , int maxLength )
79+ {
80+ // Truncate the buffer to the actual length of the string
81+ int validLength = Array . IndexOf ( buffer , '\0 ' ) ;
82+ if ( validLength < 0 ) validLength = maxLength ;
83+ return new string ( buffer , 0 , validLength ) ;
84+ }
8585 }
8686}
0 commit comments