how do I set lpstrFile of OPENFILENAME
-
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
-
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
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();
-
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();
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
-
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
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!