[win32api] problem with file open function -- ERROR_ACCESS_DENIED
i am having a problem with the function below, which is supposed to prompt for a filename with the common Open dialog (does that all right), then open the file (which it does), and read the file in 1024-byte blocks and put it into the edit control (somewhere in those two steps it crashes). i get error 0xc0000005, which i assume means 5, which is access denied.
Code:
void openFile(HWND hwnd, LPSTR filename) {
if (filename == NULL) {
OPENFILENAME ofn; /* set up OPENFILENAME structure to be used to initialize the open box */
char szFileName[MAX_PATH] = "";
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hwnd;
ofn.lpstrFilter = "Text Files (*.txt)\0*.txt\0GeekEdit Files (*.gke)\0*.gke\0All Files (*.*)\0*.*\0";
ofn.lpstrFile = szFileName;
ofn.nMaxFile = MAX_PATH;
ofn.Flags = OFN_ENABLESIZING | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_PATHMUSTEXIST;
ofn.lpstrDefExt = "txt";
if (GetOpenFileName(&ofn)) { //try getting a filename with the open dialog...
HANDLE hFile = CreateFile(szFileName, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); //open the specified file
if (hFile != INVALID_HANDLE_VALUE) { //if CreateFile() doesn't error out...
LPSTR txt_from_file = (LPSTR) GlobalAlloc(GPTR, 1024); //text grabbed from file
DWORD bytestoread = 1024; //bytes to read from file
DWORD bytesread; //var that holds bytes actually read
//LPSTR curtxt; //current text in edit box
//LPSTR newtxt; //new text to put in edit box
MessageBox(hwnd, "open dialog successful, file open operation successful, variables created and memory allocated for txt_from_file", "dbg msg", MB_OK | MB_ICONINFORMATION);
RDF: //sorry i had to be evil and have a GOTO. maybe eventually i will replace this with a while loop. anyway, this is the beginning of the code that actually reads 1024 bytes from the file
if (ReadFile(hFile, &txt_from_file, bytestoread, &bytesread, NULL)) { //if the file is correctly read...
//curtxt = (LPSTR) GlobalAlloc(GPTR, GetWindowTextLength(GetDlgItem(hwnd, ID_EDIT_CTRL))); //allocate enough memory for curtxt to hold whatever is currently in the edit box
//GetWindowText(GetDlgItem(hwnd, ID_EDIT_CTRL), curtxt, GetWindowTextLength(GetDlgItem(hwnd, ID_EDIT_CTRL))); //grab the text from the edit box and put it in curtxt
//MessageBox(hwnd, "memory allocated for curtxt and text grabbed from edit box", "dbg msg", MB_OK | MB_ICONINFORMATION);
//newtxt = (LPSTR) GlobalAlloc(GPTR, GetWindowTextLength(GetDlgItem(hwnd, ID_EDIT_CTRL))+1025); //allocate enough memory for newtxt to hold whatever is currently in the edit box plus the 1024 bytes to be read from the file, plus the null-termination character
//newtxt = curtxt;
//MessageBox(hwnd, "memory allocated for newtxt and curtxt copied into newtxt", "dbg msg", MB_OK | MB_ICONINFORMATION);
//GlobalFree(curtxt);
//MessageBox(hwnd, "memory previously used by curtxt deleted", "dbg msg", MB_OK | MB_ICONINFORMATION);
//strcat(newtxt, txt_from_file);
//MessageBox(hwnd, "txt_from_file added to newtxt", "dbg msg", MB_OK | MB_ICONINFORMATION);
//SetWindowText(GetDlgItem(hwnd, ID_EDIT_CTRL), newtxt);
//MessageBox(hwnd, "edit control text set", "dbg msg", MB_OK | MB_ICONINFORMATION);
//GlobalFree(newtxt);
MessageBox(hwnd, txt_from_file, "dbg msg", MB_OK | MB_ICONINFORMATION);
LPSTR txt = myGetWndTxt(GetDlgItem(hwnd, ID_EDIT_CTRL));
SetWindowText(GetDlgItem(hwnd, ID_EDIT_CTRL), strcat(txt, txt_from_file));
GlobalFree(txt);
if (bytesread != 0) {
goto RDF; //repeats readfile sequence if EOF not reached. see above note at RDF:
}
}
GlobalFree(txt_from_file);
CloseHandle(hFile);
MessageBox(hwnd, "all done!", "dbg msg", MB_OK | MB_ICONINFORMATION);
}
}
}
}
LPSTR myGetWndTxt(HWND hwnd) { //i got exasperated b/c GetWindowText doesn't return the text, forcing me to declare a variable, so i wrote this.
LPSTR txt = (LPSTR) GlobalAlloc(GPTR, GetWindowTextLength(hwnd));
GetWindowText(hwnd, txt, GetWindowTextLength(hwnd));
return txt;
}
note: the commented out stuff is a different approach that didn't work either.
another note: changing the value in the last MessageBox from txt_from_file to simply "file read" makes it crash after that msgbox, insted of before.
someone please help.
|