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