Forum Discussion
KB5002653 issue with the Kernel Function GetCommandLineW
The issue you are experiencing with the GetCommandLineW function being affected by the installation of KB5002653 indicates a change in how command-line arguments are being parsed or returned by Windows after this update. This can cause the result of the function to be truncated or formatted differently, impacting VBA macros that rely on this function for processing command-line arguments.
Potential Solutions
Modify VBA Code.
Example of modification:
Function CleanCmdLine(cmdLine As String) As String
' Remove leading and trailing quotes if present
If Left(cmdLine, 1) = """" And Right(cmdLine, 1) = """" Then
cmdLine = Mid(cmdLine, 2, Len(cmdLine) - 2)
End If
CleanCmdLine = cmdLine
End FunctionConsider using different APIs or approaches that might be less affected by the update. For instance, using Command$ in VBA directly may yield different results:
Sub GetCommandUsingVBA()
MsgBox "Command line parameters: " & Command$
End SubHere is an attempt at a customized version of your code that handles the potential problems with extra quotes or format changes in the command line string returned by GetCommandLineW.
Declare Function GetCommandLine Lib "kernel32" Alias "GetCommandLineW" () As Long
Declare Function lstrlenW Lib "kernel32" (ByVal lpString As Long) As Long
Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (MyDest As Any, MySource As Any, ByVal MySize As Long)
Function ReadCmdLine() As String
Dim pCmdLine As Long ' Pointer to the string
Dim Buffer() As Byte
Dim StrLen As Long
Dim RawCmdLine As String
' Get the pointer to the command line string
pCmdLine = GetCommandLine
StrLen = lstrlenW(pCmdLine) * 2
If StrLen Then
ReDim Buffer(0 To (StrLen - 1)) As Byte
CopyMemory Buffer(0), ByVal pCmdLine, StrLen
RawCmdLine = Buffer
End If
' Clean up potential extra quotes and formatting issues
RawCmdLine = CleanCmdLine(RawCmdLine)
ReadCmdLine = RawCmdLine
End Function
Function CleanCmdLine(cmdLine As String) As String
' Remove leading and trailing quotes if present
If Left(cmdLine, 1) = """" And Right(cmdLine, 1) = """" Then
cmdLine = Mid(cmdLine, 2, Len(cmdLine) - 2)
End If
' Further trim or adjust as necessary to handle known formatting issues
cmdLine = Replace(cmdLine, """", "") ' Remove internal quotes if needed
CleanCmdLine = cmdLine
End FunctionThe text and steps were edited with the help of AI.
My answers are voluntary and without guarantee!
Hope this will help you.
Was the answer useful? Mark as best response and Like it!
This will help all forum participants.