@@ -38,11 +38,7 @@ public unsafe string retrieveTargetPath(string path)
38
38
fixed ( char * bufferChar = buffer )
39
39
{
40
40
( ( 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 ) ;
46
42
}
47
43
48
44
// To set the app description
@@ -54,9 +50,7 @@ public unsafe string retrieveTargetPath(string path)
54
50
fixed ( char * buffer1Char = buffer1 )
55
51
{
56
52
( ( 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 ) ;
60
54
}
61
55
}
62
56
catch ( COMException e )
@@ -71,9 +65,7 @@ public unsafe string retrieveTargetPath(string path)
71
65
fixed ( char * buffer2Char = buffer2 )
72
66
{
73
67
( ( 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 ) ;
77
69
}
78
70
}
79
71
@@ -82,5 +74,13 @@ public unsafe string retrieveTargetPath(string path)
82
74
83
75
return target ;
84
76
}
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
+ }
85
85
}
86
86
}
0 commit comments