Announcement

Collapse
No announcement yet.

GetOpenFileName

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • GetOpenFileName

    Hi there...

    Here we go:
    I wrote a custom control which is compareable with an edit control and
    it is called CEE. So much for the class CEECLIENT
    The code is taken partwise from the PBNote example.

    Now the problem:
    If I use the GetOpenFileName to select a text file and load this
    file into my custom control by sending the message %CEE32EM_INSERTTXTFILE
    the control is dead after a few seconds.
    If I do not use the GetOpenFilename and load the text file directly
    the control is working.

    Is there something wrong in the following snippet? - Any ideas?

    Oh, if I use the predefined function OpenFileDialog() from the
    'comdlg32.inc' file the result is the same.
    .
    .
    .
    CASE %IDM_NEW
    hMdi = CreateMdiChild("CEECLIENT", hWndClient, "", 0)
    ShowWindow hMdi, %SW_SHOW
    'EXIT FUNCTION

    CASE %IDM_OPEN
    Path = "c:\windows\desktop\"
    f = "email.txt"
    Style = %OFN_FILEMUSTEXIST OR %OFN_HIDEREADONLY OR %OFN_LONGNAMES

    DIM ofn AS OPENFILENAME
    DIM fa AS ASCIIZ * %MAX_PATH
    DIM pa AS ASCIIZ * %MAX_PATH
    DIM flt AS ASCIIZ * 256
    DIM tit AS ASCIIZ * 256

    fa = f + CHR$(0)
    pa = path + CHR$(0)
    flt = "Textfiles (*.txt)|*.txt|All files (*.*)|*.*" + CHR$(0)
    REPLACE "|" WITH CHR$(0) IN flt
    tit = "Open file" + CHR$(0)

    ofn.lStructSize = SIZEOF(ofn)
    ofn.hwndowner = hWndMain
    ofn.lpstrInitialDir = %NULL
    ofn.lpstrFile = VARPTR(fa)
    ofn.nMaxFile = %MAX_PATH
    ofn.lpstrFilter = VARPTR(flt)
    ofn.nFilterIndex = 1
    ofn.hInstance = hInst
    ofn.Flags = Style
    ofn.lpstrTitle = VARPTR(tit)
    retVal = GetOpenFileName(ofn)


    IF retVal THEN
    f = fa
    hMdi = CreateMdiChild("CEECLIENT", hWndClient, "", 0)
    ShowWindow hMdi, %SW_SHOW
    CALL UpdateWindow(hMdi)
    retVal = SendMessage(GetEdit, %CEE32EM_INSERTTXTFILE, STRPTR(f), LEN(f))
    END IF

    .
    .
    .

    l8er
    ---Tom


    ------------------
    [email protected]

  • #2
    Thomas, with this type of problem, we really need to see compilable code in order to suggest a solution. Unfortunately, not many people have the time to reconstruct your app from scratch to help you solve such an unusual problem, especially given that you are using a self-written custom control...

    Thanks!


    ------------------
    Lance
    PowerBASIC Support
    mailto:[email protected][email protected]</A>
    Lance
    mailto:[email protected]

    Comment


    • #3
      Offhand, the bit you've posted looks reasonable, although you probably
      want %MAX_PATH in place of 256 for flt and tit. Also, I
      don't think you really want this line:
      Code:
      REPLACE "|" WITH CHR$(0) IN flt

      ------------------
      Tom Hanlin
      PowerBASIC Staff

      Comment


      • #4
        Tom---

        %MAX_PATH for flt and tit doesn't fix the problem either but I
        know that before.

        For the REPLACE command you are right I don't want it but it is needed.

        I would like to know if somebody else ever had a problem with the
        GetOpenFileName function. It is driving me nuts. My control works
        great but if I use this function to select a file it dies.
        ...don't know why.

        ---Thomas


        ------------------
        [email protected]

        Comment


        • #5
          Thomas;

          Likely the Filename string being returned is not what you expect.

          Put a messagebox after you use the Open Dialog and see what the
          filename string returned looks like. It is possible you are not filtering
          out some characters (like NULL), so the string you pass to your
          custom control isn't a proper pathname and filename. Your custom
          control should "test" to make sure the filename is a proper filename
          and then if the file actially exists.

          Here is some code to demonstrate how to get the filename out of
          the buffer used in the GetOpenFileName function call.


          Code:
          LOCAL ZFile AS ASCIIZ*2048  
          
          ...
          
          ' When defining the values for the TYPE for GetOpenFileName
          ' a Buffer must be defined. I use the zFile variable above for this
          
          ...
          
          TMP.lpstrFile=VARPTR(zFile)  '   returns path name
          
          ...
          
          ' This is how I return back the string from the string buffer
          OKFlag&=0
          IF GetOpenFileName(TMP) THEN OKFlag&=1
          IF OKFlag& THEN
             RV$=PEEK$(VARPTR(zFile), SIZEOF(zFile))
             P&=INSTR(RV$,CHR$(0)+CHR$(0))
             IF P&>0 THEN RV$=LEFT$(RV$, P&-1)
          END IF


          ------------------
          Chris Boss
          Computer Workshop
          Developer of "EZGUI"
          http://cwsof.com
          http://twitter.com/EZGUIProGuy

          Comment


          • #6
            Thomas;

            I believe your problem is with the string buffer you are creating
            for use with GetOpenFileName.

            You use a variable f , but I don't see it defined anywhere.
            I am assuming it is a variable length string. The string variable has
            to be set with empty spaces to make the buffer large enough to return
            the entire pathname of the file.

            Something like :

            Code:
            f="email.txt"+chr$(0)+chr$(0)+string$(2000, " ")
            would do !

            I like to use a Fixed string variable that is plenty large enough.

            Also you aren't testing the string buffer for the "double" NULL
            (CHR$(0)) found at the end of the string , as the end of string
            marker.

            If the buffer you use for the return string is too small, then the
            pathname text return gets clipped.

            Remember the old saying, "Garbage IN, Garbage Out" !

            If you pass your custom control an erroneous pathname and filename
            because you didn't handle the buffer for GetOpenFileName properly,
            then your code in your custom control will likely crash.



            ------------------
            Chris Boss
            Computer Workshop
            Developer of "EZGUI"
            http://cwsof.com
            http://twitter.com/EZGUIProGuy

            Comment


            • #7
              Chris---

              Nope for the f

              Like I sad already everyhing works fine as long as I do not use the
              GetOpenFileName function,

              if I type retVal = 1 - instead of
              retVal = GetOpenFileName(ofn)

              (I mean I could also use "If GetOpenFileName(ofn) Then")

              Take a peek where f is already filled with a path and filename

              Here is a little snippet of the message pump from the control
              ---
              CASE %CEE32EM_INSERTTEXTFILE

              e$ = Peek$(wParam, lParam)
              e$ = Trim$(e$) 'just in case

              debugprint e$ + String$(Len(e$))
              ---
              As you can see I check the string and the length of it and is
              correct and the file is beeing loaded.
              But, after a few seconds the control is dead and that gives
              me the rest.

              l8er
              ---Tom


              ------------------
              [email protected]

              Comment

              Working...
              X