Dim shared filename99914352566474_        As ZString * 1000000
Dim Shared ofn  As OpenFileName

Function GetSaveFile (ByVal Title As String,ByVal curentdir As String, ByVal Pattern As String = "All files (*.*)"+Chr(0)+"*.*"+Chr(0), ByVal defaultsetpattern As bool=0,ByVal templateName As String = "") As String 
	Dim ofn       As OPENFILENAME
	Dim filename  As ZString * MAX_PATH+1
	Dim fbguiTemp As Integer
	Dim DefExt As String*10
	filename = templateName
	With ofn
		.hwndOwner        = 0
		.lStructSize      = SizeOf(OPENFILENAME)
		.lpstrFilter      = Cast(LPCTSTR,StrPtr(Pattern))
		.lpstrFile        = StrPtr(filename)
		.nMaxFile         = SizeOf(filename)
		.lpstrFileTitle   = Cast(LPTSTR,StrPtr(Title))
		.nMaxFileTitle    = SizeOf(Title)
		.lpstrInitialDir  = Cast(LPCTSTR,StrPtr(curentdir))
		.lpstrTitle       = Cast(LPCTSTR,StrPtr(Title))
		.Flags            = OFN_EXPLORER Or OFN_FILEMUSTEXIST Or OFN_OVERWRITEPROMPT
		If defaultsetpattern=1 Then
			.lpstrDefExt = StrPtr(DefExt)
		EndIf	
	End With
	if( GetSaveFileName( @ofn ) = 0 ) then
		Return ""
	Else
		Return filename
	end If
End Function

Function OpenFileName(ByVal Title As String,ByVal curentdir As String, ByVal Pattern As String = "All files (*.*)"+Chr(0)+"*.*"+Chr(0),ByVal flag As Integer=0, ByVal templateName As String = "") As String
  Dim fbguiTemp As Integer
  filename99914352566474_ = templateName

  With ofn
    .hwndOwner       = 0
    .lStructSize     = SizeOf(OPENFILENAME)
    .lpstrFilter     = Cast(LPCTSTR,StrPtr(Pattern) )
    .lpstrFile       = StrPtr(filename99914352566474_)
    .nFileOffset     = 0
    .nMaxFile        = SizeOf(filename99914352566474_ )
    .lpstrFileTitle  = Cast(LPTSTR,StrPtr(Title))
    .nMaxFileTitle   = SizeOf(Title)
    .lpstrInitialDir = Cast(LPCTSTR,StrPtr(curentdir))
    .lpstrTitle      = Cast(LPCTSTR,StrPtr(Title))
    .Flags           = OFN_EXPLORER Or OFN_FILEMUSTEXIST Or OFN_OVERWRITEPROMPT Or flag
  End With

  if( GetOpenFileName( @ofn ) = 0 ) then
    Return ""
  Else
    Return filename99914352566474_
  end If
End Function

Function PeekS(ByVal Memory As Any Ptr, iLen As Integer = 0) As String 
	Dim sRetString As String
	Dim iCountBytes As Integer
	If iLen = 0 Then
		While TRUE
			If *Cast(Byte Ptr,(Memory+iCountBytes))<>0 Then
				iCountBytes+=1
			Else
				Exit While
			EndIf
		Wend
	Else
		iCountBytes = iLen
	EndIf
	If iCountBytes<>0 Then
		sRetString = Space(iCountBytes)
		CopyMemory(SAdd(sRetString),Cast(Byte Ptr,Memory),iCountBytes)
		Return sRetString
	EndIf
End Function

Function NextSelectedFilename () As String 
  Static e As Byte
  If ofn.nFileOffset< Len(filename99914352566474_) Then
    If e=0 Then
      e=1
      Return filename99914352566474_
    Else
      e=0
      filename99914352566474_=""
      Return ""
    EndIf
  Else
    Static a As Integer
    Static b As Byte
    Static c As Byte
    If b=0  Then
      If c=0 Then
        a=ofn.nFileOffset
        a=Cast(Integer,@filename99914352566474_) + a
      EndIf

      dim aa As string=PeekS(Cast(Any ptr,a))
      'debug aa
      a=a+Len(aa)+1
      If Peek(a) = 0 Then b=1
      c=1
      If Right(filename99914352566474_,1)<>"\" Then
        Return filename99914352566474_ & "\" & aa  
      EndIf
      Return filename99914352566474_  & aa  
    Else
      a=0
      b=0
      c=0
      filename99914352566474_=""
      Return ""
    endif
  EndIf
End Function