Access violation

J

Jon

I want to fill an edit control from a text file. I need help debugging
a couple of runtime errors: "Stack around the variable txtFromFile was
corrupted" and an access violation in ReadFile(). Ideas?

/////////////////////////////////////////////////////////////////////////////////

char szFileName[MAX_PATH] = "";
LPCSTR txtFromFile;
DWORD bytestoread = 1024;
DWORD bytesread;


if (LOWORD(wParam) == ID_BROWSE)
{
OPENFILENAME ofn;
ZeroMemory(&ofn, sizeof(ofn));

ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hDlg;
ofn.lpstrFilter = "Text Files (*.txt)\0*.txt\0All Files
(*.*)\0*.*\0";
ofn.lpstrFile = szFileName;
ofn.nMaxFile = MAX_PATH;
ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST |
OFN_HIDEREADONLY;
ofn.lpstrDefExt = "txt";

if (GetOpenFileName(&ofn))
{
HANDLE hFile = CreateFile(szFileName, GENERIC_READ, 0,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

if (ReadFile(hFile, &txtFromFile, bytestoread, &bytesread,
NULL))
{
SetDlgItemText(hDlg, IDC_EDIT1, txtFromFile);
}

CloseHandle(hFile);

return true;
}
}

/////////////////////////////////////////////////////////////////////////////////
 
J

Jochen Kalmbach [MVP]

Hi Jon!
I want to fill an edit control from a text file. I need help debugging
a couple of runtime errors: "Stack around the variable txtFromFile was
corrupted" and an access violation in ReadFile(). Ideas?

LPCSTR txtFromFile;
...
if (ReadFile(hFile, &txtFromFile, bytestoread, &bytesread,
NULL))

Where do you store the read-data?
txtFromFile is just a pointer...

You need to do something like:

char *szBuffer[1024];
DWORD bytesToRead = 1024;

Now you can pass "szBuffer" to ReadFile:
ReadFile(hFile, szBuffer, bytesToRead, &bytesRead, NULL)

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top