•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 402,420 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,017 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 4988 | Replies: 13 | Solved
![]() |
•
•
Join Date: Aug 2005
Location: Seaham, UK
Posts: 184
Reputation:
Rep Power: 4
Solved Threads: 2
Hello again everyone, Im having trouble with this function for saving a file from a richedit control within a child window in my application. I have put in extra messageboxes etc to helo me find the error. when I implement this function with valid parameters I recieve the "Invalid handle value" message box so I gathered that there is something wrong creating the handle to the file. However when I use this function for the first time for example when i create a file and save plain text into it, the function creats the file and writes to it successfully, but when i try again to save into this same file I recieve the "Invalid Handle Value" message. Here is the code for the function...
Thanks in advance...
BOOL SaveFile(HWND owner,HWND hEdit, LPCTSTR pszFileName)
{
HANDLE hFile;
BOOL bSuccess = FALSE;
MessageBox(owner,pszFileName,0,0); // Testing if the parameter from pszFileName has been recieved
// Create the file (overwrite existing files) and assign the handle to hFile
hFile = CreateFile(pszFileName, GENERIC_WRITE, 0, NULL,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
//########## RETURNS INVALID HANDLE VALUE ############
//###### MAYBE PROBLEM WITH CREATING THE FILE ########
//####################################################
// If the handle to the file has been created sucessfully
if(hFile != INVALID_HANDLE_VALUE)
{
DWORD dwTextLength;
// Get text length of edit control within child window for allocating memory
dwTextLength = GetWindowTextLength(hEdit);
// If there is no text then there is nothing to write to the file that has been created
if(dwTextLength > 0)
{
LPSTR pszText;
// Amount of me mory to be allocated = textsize + 1 for null terminator
DWORD dwBufferSize = dwTextLength + 1;
// Allocate memory for the text
pszText = GlobalAlloc(GPTR, dwBufferSize);
if(pszText != NULL)
{
// Get the Text to be saved to the file fromt he edit control
// within the child window.
if(GetWindowText(hEdit, pszText, dwBufferSize))
{
DWORD dwWritten;
// Write the text to the file
if(WriteFile(hFile, pszText, dwTextLength, &dwWritten, NULL))
bSuccess = TRUE;
//Set Title of child window to the filename
SetWindowText(owner, pszFileName);
MessageBox(owner,"File saved",0,0); // Using to find error
}
else
{
MessageBox(owner,"Could not retrieve text",0,MB_ICONERROR); // Using to find error
}
// Free memory allocated for text
GlobalFree(pszText);
}
else
{
MessageBox(owner,"Could not allocate memory",0,MB_ICONERROR); // Using to find error
}
}
else
{
MessageBox(owner,"No text",0,0);// Using to find error
}
// Close the handle to the file if it exists
CloseHandle(hFile);
}
else
{
MessageBox(owner,"Invalid handle value",0,MB_ICONERROR); // Using to find error
}
return bSuccess;
}Thanks in advance...
•
•
Join Date: Jun 2005
Location: Tokyo, Japan
Posts: 1,481
Reputation:
Rep Power: 8
Solved Threads: 98
Why dont you use the GetLastError Function to get the detailed error number? Then you will be able to find why it is failing.
•
•
Join Date: Jun 2005
Location: Tokyo, Japan
Posts: 1,481
Reputation:
Rep Power: 8
Solved Threads: 98
•
•
Join Date: Aug 2005
Location: Seaham, UK
Posts: 184
Reputation:
Rep Power: 4
Solved Threads: 2
That is what I thought as it stated another "Process" ie another application, but it is only being used by my application. The thing is...when i first use the function to save the file it works perfectly, but then when make more changes to the richedit control text and attempt to save it again it does not work...I thought this may be due to forgetting to close a handle to the file somewhere but i cant find any errors.
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,697
Reputation:
Rep Power: 36
Solved Threads: 878
•
•
Join Date: Aug 2005
Location: Seaham, UK
Posts: 184
Reputation:
Rep Power: 4
Solved Threads: 2
In my application I have it so that when i click a save button on the toolbar or menu item it will check to see if the file exists and then decide whether to save the file using the filename from the child window title or create a save as dialog box. The savefileas function creates the dialog box using the OPENFILENAME structure and calling GetSaveFileName, from which i then pass the file specified by the user into my SaveFile function. When I save the file as.. it seems to work every time however, when i call the savefile function say when the file exists it gives this error...ie
Therefore this leads me to think that when i am passing the parameters into savefile() when the file exists, something is going wrong...but when the parameters are passed through the saveas dialog box the parameters are passwed correctly. It is confusing...any suggestions ?
//SaveFile Function...
BOOL SaveFile()
{
//Processing...
}
//SaveFileAs function
BOOL SaveFileAs()
{
OPENFILENAME ofn;
//initialise the ofn structure
char szFile[MAX_PATH];
ofn.lpstrFile = szFile;
//....Initialise rest of structure...
//Pass the structure into GetSaveFileName
//Then call SaveFile function with the filename specified by the user
SaveFile(....szFile);
}Therefore this leads me to think that when i am passing the parameters into savefile() when the file exists, something is going wrong...but when the parameters are passed through the saveas dialog box the parameters are passwed correctly. It is confusing...any suggestions ?
•
•
Join Date: Jun 2005
Location: Tokyo, Japan
Posts: 1,481
Reputation:
Rep Power: 8
Solved Threads: 98
It is difficult to point out the cause from here. It will be better if you set a breakpoint just after the call for the GetSaveFileName function and see if all the fields, especially the file name of the OPENFILENAME structure, are filled properly. You better use the
ofn.Flags = OFN_OVERWRITEPROMPT; Statement before the GetSaveFileName call if you are not doing so already. •
•
Join Date: Aug 2005
Location: Seaham, UK
Posts: 184
Reputation:
Rep Power: 4
Solved Threads: 2
Yes I have set the OPENFILENAME structure correctly...It is the process of saving the file using the text from the child window title that does not work correctly...The procedure to save the file as however works fine. The savefileas procedure uses the savefile() function within it so therefore i dont think there is anything wrong with the savefile() function itself...rather the way I am passing parameters to it when not using the saveas procedure.
•
•
Join Date: Jun 2005
Location: Tokyo, Japan
Posts: 1,481
Reputation:
Rep Power: 8
Solved Threads: 98
Iam running out of options here. I also think that the SaveFile function is okay. If the filename is correctly returned from GetSaveFileName, the SaveFile function should behave identically in both cases. This is a long shot but try,
If that does not work, I advice you to post the full code of the SaveFile and SaveFileAs functions.
char szFile[MAX_PATH] = ""; ofn.lpstrFile = szFile; //....Initialise rest of structure... //Pass the structure into GetSaveFileName
If that does not work, I advice you to post the full code of the SaveFile and SaveFileAs functions.
![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
apple browser c c++ code coding compilers computer crack dell development explorer framework google ie 7 install internet java languages leopard linerider linux microsoft microsystems mobile news object open operating oriented os phishing photo planning platform programming security server software source sun system ubuntu unix upgrade vista windows windows update windows vista xp
- Previous Thread: reading a key without stoping the loop
- Next Thread: find strings in file.txt



Linear Mode