2

recently in my project when I call ShowDialog method of OpenFileDialog I get this error:

"Attempted to read or write protected memory. This is often an indication that other memory is corrupt."

I have been searching all over the web before now but my problem did not solve. Also I installed microsoft patch, but because my project is in .Net 3.5, it was not useful.

Code Sample:

OpenFileDialog OFD = new OpenFileDialog(); 
OFD.ShowDialog();

Thanks for any help.

Pars.Engineer
  • 57
  • 2
  • 8
  • First, crosscheck whether the folder or directory which you are trying to open gets open/exists or not. And try again. – Sai Kalyan Kumar Akshinthala Dec 08 '11 at 09:13
  • I appreciate your reply, OpenFileDialog OFD = new OpenFileDialog(); OFD.ShowDialog(); recently I get that error and before it worked correctly. – Pars.Engineer Dec 08 '11 at 09:32
  • I think it's an OS related issue not your program. – Jahan Zinedine Dec 08 '11 at 20:23
  • 1
    This error always (or almost always?) occurs in unmanaged code. Ultimately a call to .NET's OpenFileDialog.ShowDialog makes a call to the underlying Windows SDK function GetOpenFileName in Comdlg32.dll, so that is probably where the error actually occurs. However, I don't know why the unmanaged function GetOpenFileName would be encountering corrupted values in memory. Does your project contain any unmanaged code or use any third-party unmanaged DLLs? – dgvid Dec 08 '11 at 20:41
  • Thank you for your answer, Yes, I use several components such as Telerik RadConrols, and a class for convert calendar which I already wrote it and use its DLL now. Also my OS is Windows 7 Ultimate; however, I tested my project and setup file on several computers with different OS but I get error. – Pars.Engineer Dec 08 '11 at 22:33
  • Please add some more of your code. – Jeff Wolski Dec 08 '11 at 23:55

3 Answers3

4

OpenFileDialog loads a large amount of unmanaged code into your process. All of the shell extensions that you have installed on your machine. One of them isn't very happy about your process environment, or messes with your process enough to make it crash and burn.

You'll need to find the shell extension that causes this. Start with Project + Properties, Debug tab, tick the "Enable unmanaged code debugging" option. You'll now see the list of DLLs that get loaded in the Output window. Odds are reasonable that the last one you see before you get the exception is the trouble-maker. Although you'll still have to reverse-engineer the DLL name to the shell extension name.

The other approach is slash and burn. Use SysInternals' AutoRuns utility. Click the Explorer tab and disable anything that wasn't made by Microsoft. Ask more questions about this at superuser.com

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Thank you for your help, As I was said, already my project worked fine and recently I get this error without any changes in source code. Moreover I tested your solution (Enable unmanaged code debugging) and checked my Dlls but my problem exist yet. Also I checked carefully and realize before work with OLEDB database (access) I don’t get any error and my project show openFileDialog fine but after connecting and disconnecting to database , it is not work correctly, so I think memory is corrupted by this action. Finally, I think this problem is a Visual Studio bug. – Pars.Engineer Dec 08 '11 at 22:55
  • Don't check *your* DLLs. I'm not aware of anybody reporting this as a VS bug before or having similar problems with OleDb. You can contact Microsoft Support for help. – Hans Passant Dec 08 '11 at 23:02
  • If you search this error title , you’ll see a lot of same problems that already those were solved with Microsoft path in .Net 2.0. Such as this link: http://stackoverflow.com/questions/596413/attempted-to-read-or-write-protected-memory Also Microsoft support don’t help me because I’m from Iran and my country is ban! – Pars.Engineer Dec 08 '11 at 23:14
  • 1
    That might be Stuxnet or Duqu then. I can't help you any further. – Hans Passant Dec 08 '11 at 23:21
  • anyway, thanks for your replies. but I didn't know what did this sentence mean:"That might be Stuxnet or Duqu then" – Pars.Engineer Dec 08 '11 at 23:29
3

This is solved my problem. In Connection string add the OLE DB Services=-1 then its working.

Like this:

Provider=Microsoft.ACE.OLEDB.12.0; Data Source=C:\DbTest\Test.accdb; OLE DB Services=-1

Or as shown in this link

Lennart
  • 9,657
  • 16
  • 68
  • 84
Vikas4u
  • 51
  • 2
  • 1
    Not sure why you received downvotes, but this absolutely solved the issue for me when using `Microsoft.ACE.OLEDB.16.0` – mdisibio Aug 08 '18 at 18:21
  • This is the only answer that solved the problem for me. I was trying to open an Excel file using OLEDB connection provided by the Microsoft Access Redistributable 2016. Upvoted! – InvalidBrainException Apr 28 '19 at 23:54
  • Out of the numerous solutions provided out there, this really worked for us! – Vaishali Jun 18 '19 at 11:44
0

I had this problem too.

I was using OpenFileDialog to select an Excel file, then read the data with .net Oledb and write data to Access database.

The first time: OK

The second time, after select file, appeared this message: Attempted to read or write protected memory

My solution:

A form "A" with an OpenFileDialog and a button to display and select files and: openFileDialog1.ReadOnlyChecked = true; openFileDialog1.ShowReadOnly = true;

A form "B" With a get/set to set the filename to read A method to read excel file and write to Access db.

From "A", send filename to read to "B" form Load "B" form, execute the main process, view results and close form On return "A", I could select another file and repeat the process without errors

No more "Attempted to read or write protected memory" error

I don't know if it's the best solution but the application runs well.

Greetings

Alberto
  • 13
  • 4