{"id":219,"date":"2010-01-12T23:08:47","date_gmt":"2010-01-12T17:38:47","guid":{"rendered":"http:\/\/techtwaddle.net\/?p=219"},"modified":"2011-04-12T23:09:42","modified_gmt":"2011-04-12T17:39:42","slug":"using-the-open-file-and-save-file-dialogs-getopenfilename-and-getsavefilename-apis","status":"publish","type":"post","link":"https:\/\/techtwaddle.co.in\/blog\/2010\/01\/12\/using-the-open-file-and-save-file-dialogs-getopenfilename-and-getsavefilename-apis\/","title":{"rendered":"Using the &#8216;Open File&#8217; and &#8216;Save File&#8217; dialogs, GetOpenFileName and GetSaveFileName APIs"},"content":{"rendered":"<p style=\"text-align: justify;\">Recently, <a href=\"http:\/\/social.msdn.microsoft.com\/Forums\/en-US\/windowsmobiledev\/thread\/28d7a541-d324-42dc-a9d2-045550c969bb?prof=required\">someone on the MSDN forum asked<\/a> if there are any built-in &#8216;Open File&#8217; and &#8216;Save File&#8217; dialogs available in the Windows Mobile platform. The answer, of course, is yes. The APIs that get the task done are <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/aa921282.aspx\">GetOpenFileName()<\/a> and <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/aa931042.aspx\">GetSaveFileName()<\/a>. These APIs allow the user to select directories or files for opening or saving. I wrote a small application to demonstrate how to use these APIs. The application has a multiline edit control and menu options for opening or saving text files (see video at the end). When you open a text file, the files contents are shown on the edit control, and saving to a file writes the contents of the edit control to a user selected text file. Pretty simple.<\/p>\n<p style=\"text-align: justify;\">First, lets take a look at <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/aa921282.aspx\">GetOpenFileName()<\/a> API, the <span style=\"color: rgb(0, 0, 128);\">DoOpenFile()<\/span> method in the application reads the contents of the file and writes it to the edit control, here is the function: (error checking is left out, as usual)<\/p>\n<p>1. Get the full path to the file<br \/>\n2. Read the contents of the file into a buffer<br \/>\n3. Convert the buffer into wide char<br \/>\n4. Display the wide chars on the edit control<\/p>\n<p><span style=\"color: rgb(0, 0, 128);\">BOOL DoOpenFile(HWND hDlg)<br \/>\n{<br \/>\n&nbsp;&nbsp;&nbsp; TCHAR&nbsp;&nbsp; szFile[MAX_PATH] = TEXT(&quot;\\0&quot;);<br \/>\n&nbsp;&nbsp;&nbsp; char&nbsp;&nbsp;&nbsp; *szFileContent = NULL;<br \/>\n&nbsp;&nbsp;&nbsp; OPENFILENAME&nbsp;&nbsp; ofn;<br \/>\n&nbsp;&nbsp;&nbsp; HANDLE hFile = INVALID_HANDLE_VALUE;<br \/>\n&nbsp;&nbsp;&nbsp; DWORD dwFileSize = 0, bytesToRead = 0, bytesRead = 0;<br \/>\n&nbsp;&nbsp;&nbsp; <br \/>\n&nbsp;&nbsp;&nbsp; memset( &amp;(ofn), 0, sizeof(ofn));<br \/>\n&nbsp;&nbsp;&nbsp; ofn.lStructSize&nbsp;&nbsp; = sizeof(ofn);<br \/>\n&nbsp;&nbsp;&nbsp; ofn.hwndOwner = hDlg;<br \/>\n&nbsp;&nbsp;&nbsp; ofn.lpstrFile = szFile;<br \/>\n&nbsp;&nbsp;&nbsp; ofn.nMaxFile = MAX_PATH;<br \/>\n&nbsp;&nbsp;&nbsp; ofn.lpstrFilter = TEXT(&quot;Txt (*.txt)\\0 *.txt\\0&quot;);&nbsp;&nbsp; <br \/>\n&nbsp;&nbsp;&nbsp; ofn.lpstrTitle = TEXT(&quot;Open File&quot;);<br \/>\n&nbsp;&nbsp;&nbsp; ofn.Flags = OFN_EXPLORER;<\/p>\n<p>&nbsp;&nbsp;&nbsp;<span style=\"color: rgb(51, 153, 102);\"> <\/span><span style=\"color: rgb(51, 153, 102);\">\/\/get the filename the user wants to open<\/span><br \/>\n&nbsp;&nbsp;&nbsp; if (GetOpenFileName(&amp;ofn))<br \/>\n&nbsp;&nbsp;&nbsp; {<br \/>\n&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;<span style=\"color: rgb(51, 153, 102);\"> \/\/ofn.lpstrFile contains the full path to the file, get a handle to it<\/span><br \/>\n&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; hFile = CreateFile(ofn.lpstrFile, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);<\/p>\n<p>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if (hFile == INVALID_HANDLE_VALUE)<br \/>\n&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; return FALSE;<\/p>\n<p>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;<span style=\"color: rgb(51, 153, 102);\"> \/\/get file size in bytes<\/span><br \/>\n&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; dwFileSize = GetFileSize(hFile, NULL);<\/p>\n<p>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; szFileContent = (char*)malloc(dwFileSize+1);<br \/>\n&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <br \/>\n&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; memset(szFileContent, 0, dwFileSize+1);<\/p>\n<p>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style=\"color: rgb(51, 153, 102);\">\/\/read the files contents into a buffer<\/span><br \/>\n&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if (ReadFile(hFile, szFileContent, dwFileSize, &amp;bytesRead, NULL))<br \/>\n&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {<br \/>\n&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; TCHAR *wszTemp = NULL;<br \/>\n&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; HWND hEdit = NULL;<\/p>\n<p>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; wszTemp = (TCHAR*)malloc(bytesRead*sizeof(TCHAR));<\/p>\n<p>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style=\"color: rgb(51, 153, 102);\">\/\/convert the chars in buffer to wide chars<\/span><br \/>\n&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; mbstowcs(wszTemp, szFileContent, bytesRead);<br \/>\n&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; hEdit = GetDlgItem(hDlg, IDC_EDIT);<\/p>\n<p>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style=\"color: rgb(51, 153, 102);\">\/\/set the content of edit control<\/span><br \/>\n&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; SetWindowText(hEdit, wszTemp);<\/p>\n<p>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; free(wszTemp);<br \/>\n&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<\/p>\n<p>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style=\"color: rgb(51, 153, 102);\">\/\/free resources<\/span><br \/>\n&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; free(szFileContent);<br \/>\n&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; CloseHandle(hFile);<br \/>\n&nbsp;&nbsp;&nbsp; }<\/span><\/p>\n<p><span style=\"color: rgb(0, 0, 128);\">&nbsp;&nbsp;&nbsp; return TRUE;<br \/>\n}<\/span><\/p>\n<p style=\"text-align: justify;\">The APIs and the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/aa932877.aspx\">OPENFILENAME<\/a> structure are explained well in the MSDN documentation, so I am not going to repeat the same things here. Follow the links.<\/p>\n<p style=\"text-align: justify;\">The <span style=\"color: rgb(0, 0, 128);\">DoSaveFile()<\/span> method in the application allows the user to save the contents of edit control into a file, here is the code:<\/p>\n<p>1. Get the full path to the file<br \/>\n2. Read the contents of edit control into a wide char buffer<br \/>\n3. Convert the wide char into char (multibyte string)<br \/>\n4. Write the chars to the file<\/p>\n<p><span style=\"color: rgb(0, 0, 128);\">BOOL DoSaveFile(HWND hDlg)<br \/>\n{<br \/>\n&nbsp;&nbsp;&nbsp; TCHAR&nbsp;&nbsp; szFile[MAX_PATH] = TEXT(&quot;\\0&quot;);<br \/>\n&nbsp;&nbsp;&nbsp; OPENFILENAME&nbsp;&nbsp; ofn;<br \/>\n&nbsp;&nbsp;&nbsp; HANDLE hFile = INVALID_HANDLE_VALUE;<br \/>\n&nbsp;&nbsp;&nbsp; <br \/>\n&nbsp;&nbsp;&nbsp; memset( &amp;(ofn), 0, sizeof(ofn));<br \/>\n&nbsp;&nbsp;&nbsp; ofn.lStructSize&nbsp;&nbsp; = sizeof(ofn);<br \/>\n&nbsp;&nbsp;&nbsp; ofn.hwndOwner = hDlg;<br \/>\n&nbsp;&nbsp;&nbsp; ofn.lpstrFile = szFile;<br \/>\n&nbsp;&nbsp;&nbsp; ofn.nMaxFile = MAX_PATH;<br \/>\n&nbsp;&nbsp;&nbsp; ofn.lpstrFilter = TEXT(&quot;Text (*.txt)\\0*.txt\\0&quot;);&nbsp;&nbsp; <br \/>\n&nbsp;&nbsp;&nbsp; ofn.lpstrTitle = TEXT(&quot;Save File As&quot;);<br \/>\n&nbsp;&nbsp;&nbsp; ofn.Flags = OFN_HIDEREADONLY; <br \/>\n&nbsp;&nbsp;&nbsp; ofn.lpstrDefExt = TEXT(&quot;txt&quot;);<\/p>\n<p>&nbsp;&nbsp;&nbsp; <span style=\"color: rgb(51, 153, 102);\">\/\/get the filename the user wants to save to<\/span><br \/>\n&nbsp;&nbsp;&nbsp; if (GetSaveFileName(&amp;ofn)) <br \/>\n&nbsp;&nbsp;&nbsp; {<br \/>\n&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; HWND hEdit = NULL;<br \/>\n&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; DWORD dwTextLen = 0, bytesWritten = 0;<br \/>\n&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; TCHAR *wszEditText = NULL;<br \/>\n&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; char *szEditText = NULL;<\/p>\n<p>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style=\"color: rgb(51, 153, 102);\">\/\/ofn.lpstrFile contains the full path of the file, get a handle to it<\/span><br \/>\n&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; hFile = CreateFile(ofn.lpstrFile, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);<\/p>\n<p>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if (hFile == INVALID_HANDLE_VALUE)<br \/>\n&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; return FALSE;<\/p>\n<p>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; hEdit = GetDlgItem(hDlg, IDC_EDIT);<\/p>\n<p>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style=\"color: rgb(51, 153, 102);\">\/\/get the text length of the edit controls contents<\/span><br \/>\n&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; dwTextLen = GetWindowTextLength(hEdit);<\/span><\/p>\n<p><span style=\"color: rgb(0, 0, 128);\">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; wszEditText = (TCHAR*)malloc((dwTextLen+1)*sizeof(TCHAR));<\/span><\/p>\n<p><span style=\"color: rgb(0, 0, 128);\">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; memset(wszEditText, 0, (dwTextLen+1)*sizeof(TCHAR));<\/p>\n<p>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style=\"color: rgb(51, 153, 102);\">\/\/read edit controls contents into buffer<\/span><br \/>\n&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; GetWindowText(hEdit, wszEditText, dwTextLen+1);<\/p>\n<p>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; szEditText = (char*)malloc(dwTextLen+1);<br \/>\n&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <br \/>\n&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style=\"color: rgb(51, 153, 102);\">\/\/convert the wide char read from edit control to char<\/span><br \/>\n&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; wcstombs(szEditText, wszEditText, dwTextLen);<\/p>\n<p>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style=\"color: rgb(51, 153, 102);\">\/\/save the contents into file<\/span><br \/>\n&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if (WriteFile(hFile, szEditText, dwTextLen, &amp;bytesWritten, NULL))<br \/>\n&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {<br \/>\n&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }<\/p>\n<p>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style=\"color: rgb(51, 153, 102);\">\/\/free resources<\/span><br \/>\n&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; free(wszEditText);<br \/>\n&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; free(szEditText);<br \/>\n&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; CloseHandle(hFile);<br \/>\n&nbsp;&nbsp;&nbsp; }<br \/>\n&nbsp;&nbsp;&nbsp; <br \/>\n&nbsp;&nbsp;&nbsp; return TRUE;<br \/>\n}<\/span><\/p>\n<p style=\"text-align: justify;\">Here is a video demo of how things work,&nbsp;&nbsp;<\/p>\n<p><object width=\"425\" height=\"344\"><param name=\"movie\" value=\"http:\/\/www.youtube.com\/v\/MV_0qosWr84&amp;hl=en_US&amp;fs=1&amp;rel=0&amp;color1=0x3a3a3a&amp;color2=0x999999\" \/><param name=\"allowFullScreen\" value=\"true\" \/><param name=\"allowscriptaccess\" value=\"always\" \/><\/object><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Recently, someone on the MSDN forum asked if there are any built-in &#8216;Open File&#8217; and &#8216;Save File&#8217; dialogs available in the Windows Mobile platform. The answer, of course, is yes. The APIs that get the task done are GetOpenFileName() and GetSaveFileName(). These APIs allow the user to select directories or files for opening or saving. &hellip; <a href=\"https:\/\/techtwaddle.co.in\/blog\/2010\/01\/12\/using-the-open-file-and-save-file-dialogs-getopenfilename-and-getsavefilename-apis\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Using the &#8216;Open File&#8217; and &#8216;Save File&#8217; dialogs, GetOpenFileName and GetSaveFileName APIs<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false},"categories":[1],"tags":[],"jetpack_featured_media_url":"","jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p1ktFF-3x","_links":{"self":[{"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/posts\/219"}],"collection":[{"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/comments?post=219"}],"version-history":[{"count":1,"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/posts\/219\/revisions"}],"predecessor-version":[{"id":220,"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/posts\/219\/revisions\/220"}],"wp:attachment":[{"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/media?parent=219"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/categories?post=219"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/tags?post=219"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}