Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Error - Protected Memory
Message
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Miscellaneous
Thread ID:
01395601
Message ID:
01395769
Views:
54
>I had this code working yesterday. Today I get "Attempted to read or write protected memory. This is often an indication that other memory is corrupt."
>
>Here's the code. It errors on the oSheet.SaveAs line:
>
>
>Microsoft.Office.Interop.Excel.Application oXL;
>Microsoft.Office.Interop.Excel._Workbook oWB;
>Microsoft.Office.Interop.Excel._Worksheet oSheet;
>
>oXL = new Microsoft.Office.Interop.Excel.Application();
>oWB = (Microsoft.Office.Interop.Excel._Workbook)(oXL.Workbooks.Add(Missing.Value));
>oSheet = (Microsoft.Office.Interop.Excel._Worksheet)oWB.ActiveSheet;
>
>Reader = new StreamReader(ExportParams.sSourceFileName);
>
>iRow = 0;
>sSourceRow = Reader.ReadLine();
>
>while (sSourceRow != null)
>{
>    iRow++;
>    string[] aWords = sSourceRow.Split(',');
>
>    oSheet.Cells[iRow, 1] = aWords[0];
>    oSheet.Cells[iRow, 2] = aWords[1];
>    oSheet.Cells[iRow, 3] = aWords[2];
>
>    sSourceRow = Reader.ReadLine();
>}
>
>Reader.Close();
>
>if (File.Exists(ExportParams.sResultFileName))
>{
>    File.Delete(ExportParams.sResultFileName);
>}
>
>oSheet.SaveAs(ExportParams.sResultFileName,
>        Type.Missing,
>        Type.Missing,
>        Type.Missing,
>        Type.Missing,
>        Type.Missing,
>        Type.Missing,
>        Type.Missing,
>        Type.Missing,
>        Type.Missing);
>
>oXL.Quit();
Probably it is related to Excel misbehaviour at oXL.Quit().
With oXL.Quit() one expects to close Excel as running process, but it still remain in Task Manager processes list.

If in your code you create one Excel object, then close it, and then create another Excel object, and so on, then you may see several Excels in Task Manager while your application is running. If it is the case then try to close Excel properly, lile (here is VB.NET code):
oXL.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComObject(oXL)
oXL = Nothing
Because of the above, I would not declare objects like oWB and oSheet. They should be closed one by one and in proper parent-child order before closing Excel. I would work with oXl.ActiveWorkbook, and oXl.ActiveSheet instead.

Good Luck
Previous
Reply
Map
View

Click here to load this message in the networking platform