attempt to read or write protected memory  
Author Message
d.cosentino





PostPosted: Visual C# General, attempt to read or write protected memory Top

hello everyone.
i'm trying to open an excel workbook using c#.
this is the code:

using Excel;

Excel.Application excelApp = new Excel.ApplicationClass();
string workbookPath = "c:/SomeWorkBook.xls";
Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(workbookPath,
0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "",
true, false, 0, true, false, false);

when i run the program on the same pc that has the c# environment it runs without problem, but when i try to run it on another pc i have the following error:

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


this exception is thrown when i try to open the workbook.

i've tried on several pc with different version of Excel (2003, 2000), but the results are the same.

any suggest

thanx



Visual C#16  
 
 
OmegaMan





PostPosted: Visual C# General, attempt to read or write protected memory Top

Are you copying the Office interops over when you copy the program


 
 
d.cosentino





PostPosted: Visual C# General, attempt to read or write protected memory Top

yes, they are located in the same directory of the .exe file
following the list of these files:
Interop.Excel.dll
Interop.Microsoft.Office.Core.dll
Interop.VBIDE.dll


 
 
OmegaMan





PostPosted: Visual C# General, attempt to read or write protected memory Top

Which version of the interops are you using


 
 
d.cosentino





PostPosted: Visual C# General, attempt to read or write protected memory Top

Microsoft Excel 10.0


 
 
OmegaMan





PostPosted: Visual C# General, attempt to read or write protected memory Top

It might also be how the code is handling the default paramters in the call to the interops. Instead of the "" use a missing object.



// Defined on the class
private global::System.Object missing = global::System.Type.Missing;

...

...Open(workbookPath, 0, false, 5, missing, missing, false, ...







 
 
d.cosentino





PostPosted: Visual C# General, attempt to read or write protected memory Top

unfortunatly the result doesn't change.....
anyway thanks


 
 
OmegaMan





PostPosted: Visual C# General, attempt to read or write protected memory Top

Some random thoughts:

  1. Does the file actually exist on the target machine at the location specified
  2. Is it open in Excel when the program tries to run
  3. Can your code open a different excel file with no problems....

Good luck.



 
 
d.cosentino





PostPosted: Visual C# General, attempt to read or write protected memory Top

Some random thoughts:

  1. Does the file actually exist on the target machine at the location specified

yes

  1. Is it open in Excel when the program tries to run

no, excel is closed. i open excel without problem. i have the exception when i try to open the file.

  1. Can your code open a different excel file with no problems....

no.....


Good luck.


thanx


 
 
OmegaMan





PostPosted: Visual C# General, attempt to read or write protected memory Top

One other thought...is there a reference assembly that is found on your computer but not the target pcs An assembly that may not be copied during build...Some other library or installed item

 
 
d.cosentino





PostPosted: Visual C# General, attempt to read or write protected memory Top

as far as i know no, but i will check more deeply.


 
 
jprny





PostPosted: Visual C# General, attempt to read or write protected memory Top

I found your posts because I'm having the same problem.

Found a likely solution at http://vbforums.com/showthread.php t=433311.

Looks like you have to remove all references to Excel objects when using late binding.

Hope that helps.

-jpr


 
 
keesp





PostPosted: Visual C# General, attempt to read or write protected memory Top

Hi,

In case some people are still facing this problem, I have one possible cause for such errors. As it happens, a lot of com dlls (pia's) are not downward compatible. In case you are developing against, say a Word XP version and have added these references to your .NET project, then this error message may occur when you deploy your application in an environment that uses WORD 2000 or so. It will not always happen, in my case it occurred when I wanted to pass a String to the COM object, for instance a file name to open a word document through automation.

to solve this problem, one needs to develop an automation application using the oldest automation library that is needed. So basically you have to ensure upwards compatibility. In my case, where I needed to deploy against WORD 2000, I had to create the pias myself using tlbimp.exe (using the command line):

tlbimp.exe msword9.olb /keyfile:<mykeyfilename.snk> /out: Microsoft.Office.Interop.Word2000.dll

the file msword9.olb can be found in office2000, so you would need to get that file from a WORD 2000 installation disk or otherwise. In the command, I have chosen to use the same naming convention as is done in WORD XP (Microsoft.Office.Interop.... so that the c# code can be easily adjusted. Last, the type libraries need to be signed or else Visual Studio will not accept them.
In this case, I can now add a reference Microsoft.Office.Interop.Word2000 to my project and adjust the appropriate com calls.
This solved my problems with memory.

see for info:
http://www.devcity.net/PrintArticle.aspx ArticleID=163
http://support.microsoft.com/kb/313666