[API]
GetCommandLineA
GetCommandLineW

[INCLUDE]
PeekS

[CODE]
'(1)      C:\Q0504E\Programs\Dev\VBasic6\*.aby         path only
'""       C:\Q0504E\Programs\Dev\VBasic6\vb6.*.aby     (default)
'blank(0) C:\Q0504E\Programs\Dev\VBasic6\vb6.exe*.aby  real extension

Public Function AppBase(Optional ByVal ExtensionToAdd As String = vbNullChar) As String
  #If InIDE Then
    Dim xChar1 As String
  
    xChar1 = App.Path
    If Not Right(xChar1, 1&) = "\" Then xChar1 = xChar1 + "\"
    If ExtensionToAdd = Chr(1) Then
      AppBase = xChar1
      Else
      If ExtensionToAdd = vbNullChar Then ExtensionToAdd = "exe"
      AppBase = xChar1 + App.EXEName + "." + ExtensionToAdd
      End If
    #Else

    Static xLong1 As Long
    Static xChar1 As String
    Static xChar2 As String
    Static xChar3 As String
    
    If xLong1 = 0& Then
      If IsUnicode Then
        xChar2 = PeekS(GetCommandLineW)
        Else
        xChar2 = PeekS(GetCommandLineA)
        End If
      If Left(xChar2, 1&) = Chr(34&) Then
        xChar2 = Mid(xChar2, 2&)
        xLong1 = Len(xChar2)
        If Not xLong1 = 0& Then
          xChar2 = Left(xChar2, xLong1 - 1&)
          End If
        xLong1 = InStr(xChar2, Chr(34&))
        Else
        xLong1 = InStr(xChar2, " ")
        End If
      If Not xLong1 = 0& Then
        xChar2 = Left(xChar2, xLong1 - 1&)
        End If
    
      xLong1 = InStrRev(xChar2, "\")
      AppBase = Mid(xChar2, xLong1 + 1&)
      xChar2 = Left(xChar2, xLong1)
      xChar3 = xChar2
      xLong1 = InStrRev(AppBase, ".")
      If xLong1 = 0& Then
        xChar1 = AppBase & "."
        Else
        xChar1 = Left(AppBase, xLong1)
        End If
      Select Case xChar1
        Case LCase(xChar1)
          xLong1 = 1&
        Case UCase(xChar1)
          xLong1 = 2&
        Case Else
          xLong1 = 3&
        End Select
      xChar1 = xChar2 & xChar1
      xChar2 = xChar2 & AppBase
      End If
    
    Select Case ExtensionToAdd
      Case vbNullChar
        AppBase = xChar2
      Case Chr(1&)
        AppBase = xChar3
      Case LCase(ExtensionToAdd), UCase(ExtensionToAdd)
        If xLong1 = 2& Then
          AppBase = xChar1 & UCase(ExtensionToAdd)
          ElseIf xLong1 = 1& Then
          AppBase = xChar1 & LCase(ExtensionToAdd)
          Else
          AppBase = xChar1 & ExtensionToAdd
          End If
      Case Else
        AppBase = xChar1 & ExtensionToAdd
      End Select
    #End If
  End Function
