Improve code quality

This commit is contained in:
Jack251970 2024-12-14 11:46:09 +08:00
parent a088f91541
commit a899ff83e5

View file

@ -38,11 +38,7 @@ namespace Flow.Launcher.Plugin.Program.Programs
fixed (char* bufferChar = buffer)
{
((IShellLinkW)link).GetPath((PWSTR)bufferChar, MAX_PATH, &data, (uint)SLGP_FLAGS.SLGP_SHORTPATH);
// Truncate the buffer to the actual length of the string
int validLength = Array.IndexOf(buffer, '\0');
if (validLength < 0) validLength = MAX_PATH;
target = new string(buffer, 0, validLength);
target = GetStringFromBuffer(buffer, MAX_PATH);
}
// To set the app description
@ -54,9 +50,7 @@ namespace Flow.Launcher.Plugin.Program.Programs
fixed (char* buffer1Char = buffer1)
{
((IShellLinkW)link).GetDescription((PWSTR)buffer1Char, MAX_PATH);
int validLength = Array.IndexOf(buffer1, '\0');
if (validLength < 0) validLength = MAX_PATH;
description = new string(buffer1, 0, validLength);
description = GetStringFromBuffer(buffer1, MAX_PATH);
}
}
catch (COMException e)
@ -71,9 +65,7 @@ namespace Flow.Launcher.Plugin.Program.Programs
fixed (char* buffer2Char = buffer2)
{
((IShellLinkW)link).GetArguments((PWSTR)buffer2Char, MAX_PATH);
int validLength = Array.IndexOf(buffer2, '\0');
if (validLength < 0) validLength = MAX_PATH;
arguments = new string(buffer2, 0, validLength);
arguments = GetStringFromBuffer(buffer2, MAX_PATH);
}
}
@ -82,5 +74,13 @@ namespace Flow.Launcher.Plugin.Program.Programs
return target;
}
private static unsafe string GetStringFromBuffer(char[] buffer, int maxLength)
{
// Truncate the buffer to the actual length of the string
int validLength = Array.IndexOf(buffer, '\0');
if (validLength < 0) validLength = maxLength;
return new string(buffer, 0, validLength);
}
}
}