Attempted to read or write protected memory. When

2019-01-15 11:24发布

问题:

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.

回答1:

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



回答2:

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



回答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