Forum Discussion
LionelG
Nov 14, 2024Copper Contributor
KB5002653 issue with the Kernel Function GetCommandLineW
Hello, Just to warn about this specific issue, since the installation of the KB5002653 , From an Excel 2016 VBA the function GetCommandLineW give a truncated/different result. Sample Code: ...
VBA_94
Apr 30, 2025Copper Contributor
LionelG,
This bug still exists, so I think I have a rough workaround.
In the main routine, there is an If block for Excel 2016, then there is a workaround function that gets the length of the command line and adds enough length to it to read the longest switches. After that the missing quote is added where it was truncated.
Workaround function:
'TODO: Trim extra length, rather than relying on CommandLineToArgvW
Private Function GetOffice2016CommandLine(ByVal pointer_vba As LongPtr) As String
Const BytesPerCharacter As Long = 2&
Const ExtendCommandLineCharacters As Long = 16& 'Extend by maximum switch length only - could cause rare crashes
Dim bBuffer() As Byte
Dim lTruncatedStringLength As Long
Dim lStringLength As Long
Dim lBufferLength As Long
Dim tString As String
If pointer_vba <> 0& Then
lTruncatedStringLength = lstrlen(pointer_vba)
lStringLength = lTruncatedStringLength + ExtendCommandLineCharacters
If lStringLength > 0& Then
lBufferLength = lStringLength * BytesPerCharacter
ReDim bBuffer(0& To (lBufferLength - 1&)) As Byte
APICommandLine.CopyMemory bBuffer(0&), ByVal pointer_vba, lBufferLength
tString = bBuffer
'add missing final quote
Mid$(tString, lTruncatedStringLength + 1&, 1&) = Chr(34) 'quote
GetOffice2016CommandLine = tString
End If
End If
End FunctionIn main function:
'get a pointer to the current process' command line
lCommandLinePointer = APICommandLine.GetCommandLine()
If Val(Application.Version) = 16# Then
If Val(Application.Build) >= 5474# Then 'KB5002653
If Val(Application.Build) < 10000# Then '?Patched in Office 365, 2019, 2021, 2024 etc.
tCommandLine = APICommandLine.PointerToString(lCommandLinePointer)
'a switch is not found
If InStr(1&, tCommandLine, " /", vbBinaryCompare) = 0& Then
'get an extended copy of the truncated command line
tCommandLine = APICommandLine.GetOffice2016CommandLine(lCommandLinePointer)
'get a pointer to the Unicode command line string copy
lCommandLinePointer = StrPtr(tCommandLine)
End If
End If
End If
End If
'get a pointer to the command line arguments
lCommandLineArgumentsPointer = APICommandLine.CommandLineToArgv(lCommandLinePointer, lCommandLineArgumentsCount)