[API]
LocalAlloc
LocalFree
RtlMoveMemory

[INCLUDE]
LocalAllocFromString
GetFileAttributes

[DEFINE]
Private Const OPENFILENAME_DataSize As Long = 32760&
Private Const OPENFILENAME_DataSizeW As Long = (OPENFILENAME_DataSize) * 2&

Public Type OPENFILENAME
  hWndOwner As Long
  Filter As String
  FilterIndex As Long
  Title As String
  InitialDirFile As String
  ' Save Mode Only:
#If GetSaveFileName Then
  DefExt As String
  ErrExistsTitle As String
  ErrExistsText As String
  ErrROTitle As String
  ErrROText As String
#End If
  End Type

Private Type OPENFILENAME_Real
  lStructSize As Long
  hWndOwner As Long
  hInstance As Long
  lpstrFilter As Long
  lpstrCustomFilter As Long
  nMaxCustFilter As Long
  nFilterIndex As Long
  lpstrFile As Long
  nMaxFile As Long
  lpstrFileTitle As Long
  nMaxFileTitle As Long
  lpstrInitialDir As Long
  lpstrTitle As Long
  Flags As Long
  nFileOffset As Integer
  nFileExtension As Integer
  lpstrDefExt As Long
  lCustData As Long
  lpfnHook As Long
  lpTemplateName As Long
#If GetSaveFileName Then
  Ext_SaveName As String
#End If
  End Type

[CODE]
Private Sub OPENFILENAME_Patch(ByRef xType1 As OPENFILENAME, ByRef xType2 As OPENFILENAME_Real)
  Dim xChar1 As String
  Dim xLong1 As Long
  
  xChar1 = xType1.Filter
  If Not Right(xChar1, 1&) = vbNullChar Then
    xChar1 = xChar1 & vbNullChar & vbNullChar
    ElseIf Not Right(xChar1, 2&) = vbNullChar & vbNullChar Then
    xChar1 = xChar1 & vbNullChar
    End If
  xType2.lpstrFilter = LocalAllocFromString(xChar1)
  #If GetSaveFileName Then
    xType2.lCustData = Len(xChar1)
    #End If
  If Not Len(xType1.Title) = 0& Then
    xType2.lpstrTitle = LocalAllocFromString(xType1.Title)
    End If
  xType2.nFilterIndex = xType1.FilterIndex
  If xType2.nFilterIndex < 1& Then xType2.nFilterIndex = 1&
  xLong1 = (1& - CLng(IsUnicode)) * (OPENFILENAME_DataSize + 2&)
  xType2.lpstrFile = LocalAlloc(0&, xLong1)
  xType2.lpstrFileTitle = LocalAlloc(0&, xLong1)
  If Not xType2.lpstrFile = 0& Then
    xLong1 = 0&
    Call RtlMoveMemory(ByVal xType2.lpstrFile, ByVal VarPtr(xLong1), 2&)
    End If
  xType2.nMaxFile = OPENFILENAME_DataSize
  xType2.nMaxFileTitle = OPENFILENAME_DataSize
  xType2.lStructSize = 76&
  xType2.hWndOwner = xType1.hWndOwner
  xType2.hInstance = App.hInstance

  xChar1 = xType1.InitialDirFile
  If Not Len(xChar1) = 0& Then
    xLong1 = GetFileAttributes(xChar1)
    If Not (xLong1 = -1& Or (xLong1 And &H10&) = &H10&) Then
      xLong1 = InStrRev(xChar1, "\")
      If Not xLong1 = 0& Then
        #If GetSaveFileName Then
          xType2.Ext_SaveName = Mid(xChar1, xLong1 + 1&)
          #End If
        xChar1 = Left(xChar1, xLong1 - 1&)
        End If
#If GetSaveFileName Then
      Else
      xType2.Ext_SaveName = xChar1
#End If
      End If
    End If
  If Not Len(xChar1) = 0& Then
    xType2.lpstrInitialDir = LocalAllocFromString(xChar1)
    End If
  End Sub

Private Sub OPENFILENAME_Free(ByRef xType2 As OPENFILENAME_Real)
  With xType2
    If Not .lpstrFilter = 0& Then Call LocalFree(.lpstrFilter)
    If Not .lpstrTitle = 0& Then Call LocalFree(.lpstrTitle)
    If Not .lpstrFile = 0& Then Call LocalFree(.lpstrFile)
    If Not .lpstrFileTitle = 0& Then Call LocalFree(.lpstrFileTitle)
    If Not .lpstrInitialDir = 0& Then Call LocalFree(.lpstrInitialDir)
    End With
  End Sub
