Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / July 2005

Tip: Looking for answers? Try searching our database.

Access violation

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jon - 02 Jul 2005 18:40 GMT
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;
       }
   }

/////////////////////////////////////////////////////////////////////////////////
Jochen Kalmbach [MVP] - 02 Jul 2005 19:09 GMT
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
[quoted text clipped - 4 lines]
>             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)

Signature

Greetings
  Jochen

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

Jon - 03 Jul 2005 00:00 GMT
Thanks, got it.

Jon

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.