none
Document.GetVstoObject() throwing System.AccessViolationException(Attempted to read or write protected memory) RRS feed

  • Question

  • Hi,

    I have word addin for my Winform application. Addin code is executed whenever i open any word document. I am accessing one intranet site which has links to few word documents. These word documents are opened from the webserver through streaming. Path i can see in the opened word document is https://abc.xyz.com/binary/2_230481/docname.docx

    the code is like below:

    void Application_DocumentOpen(Microsoft.Office.Interop.Word.Document objDocument)

            {

    ToolsWord_.Document LobjDoc = objDocument.GetVstoObject();

    ...................

    }

    GetVstoObject is throwing exception for only few documents. rest all the documents are opened successfully. all the word documents opened from local systems are opened successfully as well.  the exception is:

    4/10/2012 9:51:59 AM System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
       at Microsoft.VisualStudio.Tools.Office.Runtime.Interop.IHostItemFactory.Create(IntPtr document)
       at Microsoft.VisualStudio.Tools.Office.Internal.HostItemFactoryAdapter.Microsoft.VisualStudio.Tools.Office.Contract.IHostItemFactoryContract.Create(IntPtr document)
       at Microsoft.VisualStudio.Tools.Office.Internal.IHostItemFactoryProxy.Microsoft.VisualStudio.Tools.Office.IHostItemFactory.Create(Object primaryControl)
       at Microsoft.Office.Tools.Word.Extensions.DocumentExtensions.GetVstoObject(_Document document)
       at Client.UI.Word.Addin.WordAddIn.Application_DocumentOpen(Document PobjDocument) in

    After the error word document becomes unstable and crashing. Any suggestion?

    Tuesday, April 10, 2012 6:00 PM

Answers

  • Hi Chirag,

    I just noticed you execute the GetVstoObject() method from web.  Looking at MSDN (http://msdn.microsoft.com/en-us/library/ee871560.aspx) This could result in problem as the method requires full trust.  There's security concern.  In other word, you cannot call this method in web context(non-full-trust).

     "Full trust for the immediate caller. This member cannot be used by partially trusted code"

    best regards,


    Forrest Guo | MSDN Community Support | Feedback to manager

    Thursday, April 26, 2012 3:43 AM
    Moderator

All replies

  • Hi ChiragP,

    Thanks for posting in the MSDN Forum.

    would you please clarify your Office version and VSTO runtime version?

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    Thursday, April 12, 2012 8:32 AM
    Moderator
  • Hi Tom,

    Thanks for the reply. Office version is 2010 (word 2010) and VSTO runtime version is 3.0. (Word 2007 Add in project targeting to .net framework 3.5)

    I also verified that same code is working fine in VSTO 4.0 with office 2010.  in my case, ToolsWord_.Document LobjDoc = objDocument.GetVstoObject() throws the exception mentioned above. while in VSTO 4.0, the same line is written as

    ToolsWord_.Document LobjDoc = Globals.Factory.GetVstoObject(objDocument); which works well.

    Though i am not facing issue for all the documents in VSTO 3.0. only few documents has the problem. all these documents are opened from Intranet site.

    Thanks,

    Chirag

    Thursday, April 12, 2012 2:45 PM
  • Hi Chirag,

    I will reproduce your issue next week due to I need to prepare the environment for it.

    I will tell you my result as faster as I can.

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us


    Friday, April 13, 2012 6:13 AM
    Moderator
  • Hi Tom,

    Thanks for the reply. few additonal info:

    - If i keep anyone word document opened from anywhere then i am not getting exception from GetVstoObject(). it works well. getting exception only when i do not have any word instance running.

    - As you mentioned that you will try to repro. fyi, the issue is with few documents opened from local Intranet site. documents are docx and Read-only after opened. few other documents(docx and Read-only) opened from same site are working fine. as mentioned, all the documents are opened from the webserver through streaming. Path i can see in the opened word document is https://abc.xyz.com/binary/2_230481/docname.docx

    Thanks,

    Chirag

    Friday, April 13, 2012 12:17 PM
  • Hi ChiragP,

    I'm failed to reproduce  your issue. I will involve some experts into your issue to see whether they can help you. There might have some time delay, thanks for your patience.

    Have a good day,

    Tom 


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    Tuesday, April 17, 2012 6:33 AM
    Moderator
  • Hi Chirag,

    Frankly I get confused by your original post: Word addin for Winform application. I'm not sure how these two concepts are associated on your side.

    As I understand, you run (Office 2007/.NET 35/ VSTOR V3) addin on Office 2010, and installed VSTOR V3. Remember that Office 2010 has built-in VSTOR V4, anyway, some document extentions works, sounds good. 

    GetVstoObject() method is first introduced in VSTOR V3 SP1, VS2008 SP1. So if you call the API against VSTOR V3, Please make sure you have SP1.

    I noticed that fact that the API call works for some document, but not for all. I suspect these error-document doesn't get VSTO document deployed correct. Like the solution dlls doesn't get deployed correct, Otherwise, how do you prove they're deployed correctly?  I assume you test different document on one computer.  So I excluded the possibility of unmatched assembly version. Like Microsoft.Office.Tools.Word.v9.0.dll

    Hopefully this gets you started.

    best regards,


    Forrest Guo | MSDN Community Support | Feedback to manager

    Thursday, April 19, 2012 6:24 AM
    Moderator
  • Hi Forrest Guo,

    Thanks for the reply.

    sorry for the confusion. actually we load & display the addins for the documents opened from the winform application. for other documents(opened from web or local system) we load the addins but do not display the ribbon. for such document we just display one link in the file menu which provides the functionality to save the document inside the winform application if it is running.

    so word addin is related with winform application but it is separate. I am able to reproduce the same issue with POC Addin which has only one line of code(objDoc=objDocument.GetVstoObject()) in Application_DocumentOpen(Document objDocument).

    We have investigated the document but did not find any real anomalies in the document. The crash could be occuring because of control or  image file data possibly in the header/footer of the document.

    At present i found the workaround: we were getting VSTO object for active word document to wire the right click event handler. Now i am wiring the right click event handler to the actual document object(objDocument) instead of vsto object. e.g.

    instead of this vstoObj.BeforeRightClick

    now i am using objDocument.Application.WindowBeforeRightClick

    this way i am able to escape the execution of the GetVsto for the documents being opened from the web. GetVsto is made limited only to the documents opened from the winform application where we dont have any issues.

    Tuesday, April 24, 2012 2:31 PM
  • Hi Chirag,

    I just noticed you execute the GetVstoObject() method from web.  Looking at MSDN (http://msdn.microsoft.com/en-us/library/ee871560.aspx) This could result in problem as the method requires full trust.  There's security concern.  In other word, you cannot call this method in web context(non-full-trust).

     "Full trust for the immediate caller. This member cannot be used by partially trusted code"

    best regards,


    Forrest Guo | MSDN Community Support | Feedback to manager

    Thursday, April 26, 2012 3:43 AM
    Moderator