none
VsShell: Opening a new Document Editor to display unsaved text.

    Question

  • Hello,

    I have a VS2010 Custom Editor and I am creating text of any content type during runtime (txt, cs, vb, xml, sql...) and I would like to open up a new standard editor (for the content type that is created and I know the extension during creation). I found the following thread and this search. I keep getting an exception:

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

       at Microsoft.VisualStudio.Shell.Interop.IVsUIShellOpenDocument.OpenStandardEditor(UInt32 grfOpenStandard, String pszMkDocument, Guid& rguidLogicalView, String pszOwnerCaption, IVsUIHierarchy pHier, UInt32 itemid, IntPtr punkDocDataExisting, IServiceProvider psp, IVsWindowFrame& ppWindowFrame)

    For the following code:

    public static void OpenNewDocumentInNewWindow(string filename, string text)

            {

                var provider = new ServiceProvider(ServiceFactory.Application as IServiceProvider); 

                var rdt = provider.GetService(typeof(SVsRunningDocumentTable)) as IVsRunningDocumentTable2;

                var doc = provider.GetService(typeof(SVsUIShellOpenDocument)) as IVsUIShellOpenDocument;

                if (rdt == null || doc == null) return;

     

                IntPtr fileObjectIUnknown = Marshal.GetIUnknownForObject(text);

                IntPtr docDataActualIUnknown = IntPtr.Zero;

                IVsWindowFrame winFrame;

     

                try

                {

                    IVsHierarchy docHierarchyActual;

                    uint docItemidActual;

                    uint docCookieActual;

                    rdt.FindOrRegisterAndLockDocument((uint) (_VSRDTFLAGS.RDT_EditLock), filename, null, 0, fileObjectIUnknown, out docHierarchyActual, out docItemidActual, out docDataActualIUnknown, out docCookieActual);

     

                    Guid logicalView = VSConstants.LOGVIEWID_TextView;

                    doc.OpenStandardEditor((uint)__VSOSEFLAGS.OSE_OpenAsNewFile, filename, ref logicalView, string.Empty, docHierarchyActual as IVsUIHierarchy, docItemidActual, fileObjectIUnknown, ServiceFactory.Application as IServiceProvider, out winFrame);

     

                    if (winFrame != null)

                        winFrame.Show();

                }

                finally

                {

                    if (fileObjectIUnknown != IntPtr.Zero)

                        Marshal.Release(fileObjectIUnknown);

                    if (docDataActualIUnknown != IntPtr.Zero)

                        Marshal.Release(docDataActualIUnknown);

                }

            }

    What is the best way to open a new document type and set its contents to a string value. I only need to display this tab etc..

    Thanks

    -Blake Niemyjski


    -Blake Niemyjski (Software Development Engineer)
    Monday, May 23, 2011 9:16 PM

Answers

All replies

  • I would have to debug into it but this line looks highly suspect

    >  IntPtr fileObjectIUnknown = Marshal.GetIUnknownForObject(text);

    You are getting an IUnknown around a string?  That is very odd. It shouldn't cause any crashing, but it is not normal nor would it result in what you want.  You basically need to spin up a new editor and get the IVsTextBuffer for it.  I create a new editor and assign it some arbitrary (non-file backed) contents in my example here:

    https://github.com/ryanmolden/ModalDialogHostedEditor

     

    Specifically look at the code in MyModalDialog.cs in the CreateHostedEditor method.

    Ryan

    Tuesday, May 24, 2011 3:08 PM
    Moderator
  • Hello,
    Thanks for the sample. However, the example shows off similar functionality. I need a new editor pane (for c#, vb, txt, php, etc...) to pop up in a new editor pane (not modal) with a known filename/caption (not saved) and string content which I know of. The above sample code does create a new modal window, but I want this to show up as a new document window.  I agree the fileObjectIUnkown is messed up and I messed around with this more last night but couldn't get it to work properly still. Do you have any ideas on how to get an editor window to open up in a new editor pane that uses the generic editor where I don't have to specify any specific editor guids etc (like my code above)..
    Thanks
    -Blake Niemyjski

    -Blake Niemyjski (Software Development Engineer)
    Wednesday, May 25, 2011 2:31 PM
  • I looked into this, I think trying to open one of the built in editors, in a normal editor tab populated with data that does not exist on disk is going to be a pointless battle, the system is set up to deal with files, generally from disk. 

    I don't see any good (read easy or even logical) way to open an 'empty' editor that is fed data from memory. There is likely a way to do this, the folks on the editor forum may have suggestions, but I don't see any clear path to it and if you find it I foresee you having to fight a lot of battles as you are trying to do something far from the 'mainstream'.

    I looked into the File -> New File scenario since it is similar but even that uses a temporary file on disk.  Saving the content you want to display to disk in the users temp directory and opening that file would probably be 500% easier than trying to do what you want to do above.

    Ryan

    Thursday, May 26, 2011 6:59 AM
    Moderator
  • Hello,

    Thanks for this information. I ended up going with this solution.

    Thanks

    -Blake Niemyjski


    -Blake Niemyjski (Software Development Engineer)
    Thursday, June 02, 2011 10:36 PM
  • Thursday, May 24, 2012 3:29 PM