Forum Discussion

LionelG's avatar
LionelG
Copper Contributor
Nov 14, 2024

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:

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
   ' 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
         ReadCmdLine = Buffer
      End If
       
End Function

Before the KB I had : "C:\PROGRA~2\MICROS~2\Office16\EXCEL.EXE excelfile.xlsm /parameter"

After the KB I have : ""C:\PROGRA~2\MICROS~2\Office16\EXCEL.EXE" excelfile.xlsm"

  • NikolinoDE's avatar
    NikolinoDE
    Gold Contributor

    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 Function

    Consider 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 Sub

    Here 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 Function

    The 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.

  • LionelG's avatar
    LionelG
    Copper Contributor

    Thanks NikolinoDE


    Nice try 😉, the extra quotes was not the big deal.

    The big problem is the missing part "/parameter"
    I don't think there is a workaround for that, except uninstall the KB.

     

    It's more to inform potential unlucky users working on the old excel 2016 like me, I hope M$ plans a "Patch for the patch" 😅.

    There is other known issue with this KB.

    • NikolinoDE's avatar
      NikolinoDE
      Gold Contributor

      Thank you for clarifying the nature of the main problem: the missing command line arguments, such as /parameter, and not just the handling of quotes. The problem seems to be deeper and probably rooted in how Windows or Excel interpret command line input after KB5002653.

      A patch provided by Microsoft to fix or reverse this behavior would be the optimal solution. Until then, unfortunately, I have not offered any other solution suggestion or workaround.

      Unfortunately, as you noted, the only effective solution so far may be to uninstall KB5002653 if possible, especially if you are using an older version of Excel (such as Excel 2016). This is not ideal for long-term stability or security, so it should be approached with caution.

      Hope that maybe someone here in the forum knows another workaround or solution and can help you out.

       

      I wish you all the best in your endeavors.

      • LionelG's avatar
        LionelG
        Copper Contributor

        Thanks for your time, have a nice day.

    • GregM's avatar
      GregM
      Copper Contributor

      Hello, do you have any news about this issue ?

      On my computer, the KB5002653 has been uninstalled and the KB4484305 (patch of the patch), installed. But parameters are still ignored by getCommandLineW. I guess Microsoft patched the Excel add-ins issue related to this KB, but not ours.

       

      I figured that writing the command line like this was solving the issue for one simple parameter :

      "excel.exe /e /parameter /r macro.xlsm"

      But in my case, I have 2 parameters that are paths with spaces, and GetCommandLineW stops parsing at the last space in parameters. I tried many many ways to surround parameters with quotes but nothing works.

      My command line that used to work until the KB :

      "excel.exe" /R "macro.xlsm" /e/%param1%;%param2%

       

      If anyone has a solution/information, I'm all ears :) Thank you.

Resources