Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. how do I set lpstrFile of OPENFILENAME

how do I set lpstrFile of OPENFILENAME

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    michael thomas
    wrote on last edited by
    #1

    Hi, I am working with the common CFileDialog and I want to set the initial "File name:" editbox to something like "myfile.txt". I know I can do this with the lpszFileName of the CFileDialog class when constructing, but I would like to set it using the OPENFILENAME structure instead. To be honest, this LPTSTR thing is really bugging me now and I am determined to get it to work...but I do need some help :) I have tried to set lpstrFile, but the app crashes. This is how I have tried to set it so far :- //it crashed when I did this CString theString( "This is a test" ); LPTSTR lpsz = new TCHAR[theString.GetLength()+1]; _tcscpy(lpsz, theString); my_file_dlg.m_ofn.lpstrFile = lpsz; //so I tried this and again it crashes CString str("myfile.txt"); LPSTR ptr = str.GetBuffer(11); my_file_dlg.m_ofn.lpstrFile = ptr; Thank you for any pointers or guidance. Michael

    D PJ ArendsP 2 Replies Last reply
    0
    • M michael thomas

      Hi, I am working with the common CFileDialog and I want to set the initial "File name:" editbox to something like "myfile.txt". I know I can do this with the lpszFileName of the CFileDialog class when constructing, but I would like to set it using the OPENFILENAME structure instead. To be honest, this LPTSTR thing is really bugging me now and I am determined to get it to work...but I do need some help :) I have tried to set lpstrFile, but the app crashes. This is how I have tried to set it so far :- //it crashed when I did this CString theString( "This is a test" ); LPTSTR lpsz = new TCHAR[theString.GetLength()+1]; _tcscpy(lpsz, theString); my_file_dlg.m_ofn.lpstrFile = lpsz; //so I tried this and again it crashes CString str("myfile.txt"); LPSTR ptr = str.GetBuffer(11); my_file_dlg.m_ofn.lpstrFile = ptr; Thank you for any pointers or guidance. Michael

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      Have you looked at the CFileDialog constructor?

      CFileDialog dlg(TRUE);
      char szFile\[MAX\_PATH\] = "myfile.txt";
      dlg.m\_ofn.lpstrFile = szFile;
      dlg.m\_ofn.nMaxFile = sizeof(szFile);
      dlg.DoModal();
      
      M 1 Reply Last reply
      0
      • D David Crow

        Have you looked at the CFileDialog constructor?

        CFileDialog dlg(TRUE);
        char szFile\[MAX\_PATH\] = "myfile.txt";
        dlg.m\_ofn.lpstrFile = szFile;
        dlg.m\_ofn.nMaxFile = sizeof(szFile);
        dlg.DoModal();
        
        M Offline
        M Offline
        michael thomas
        wrote on last edited by
        #3

        That worked just fine. Now I feel a little humble (i.e stupid) as to how easy the solution was. Thanks for your help. I think I need to take a break :) Michael

        1 Reply Last reply
        0
        • M michael thomas

          Hi, I am working with the common CFileDialog and I want to set the initial "File name:" editbox to something like "myfile.txt". I know I can do this with the lpszFileName of the CFileDialog class when constructing, but I would like to set it using the OPENFILENAME structure instead. To be honest, this LPTSTR thing is really bugging me now and I am determined to get it to work...but I do need some help :) I have tried to set lpstrFile, but the app crashes. This is how I have tried to set it so far :- //it crashed when I did this CString theString( "This is a test" ); LPTSTR lpsz = new TCHAR[theString.GetLength()+1]; _tcscpy(lpsz, theString); my_file_dlg.m_ofn.lpstrFile = lpsz; //so I tried this and again it crashes CString str("myfile.txt"); LPSTR ptr = str.GetBuffer(11); my_file_dlg.m_ofn.lpstrFile = ptr; Thank you for any pointers or guidance. Michael

          PJ ArendsP Offline
          PJ ArendsP Offline
          PJ Arends
          wrote on last edited by
          #4

          lpstrFile is also used to return your user's selected file(s). The reason your app is crashing is because the buffer you are supplying is too small. Also, you have to set the nMaxFile member to the same size as your buffer. According to MSDN, the buffer should be at least 256 characters.

          TCHAR buffer[256] = {0};
          _tcscpy(buffer, _T("MyFile.txt"));
          my_file_dlg.m_ofn.lpstrFile = buffer;
          my_file_dlg.m_ofn.nMaxFile = 256;


          [

          ](http://www.canucks.com)Sonork 100.11743 Chicken Little "You're obviously a superstar." - Christian Graus about me - 12 Feb '03 Within you lies the power for good - Use it!

          Within you lies the power for good; Use it!

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups