Open a new file in memory from extension using custom editor

Sajith 66 Reputation points
2025-11-12T08:33:08.7133333+00:00

I am trying to open a new file (with .sql extension) in memory not in disc using a custom editor factory that I have implemented.

IVsUIShellOpenDocument shellOpenDocument = Microsoft.VisualStudio.Shell.Package.GetGlobalService(typeof(SVsUIShellOpenDocument)) as IVsUIShellOpenDocument;
Guid editorFactoryGuid = new Guid(<editor factory guid>);
Guid logicalViewGuid = VSConstants.LOGVIEWID_Primary;
IVsUIHierarchy parentHierarchy = null;
uint itemId = (uint)VSConstants.VSITEMID.Nil;
IVsWindowFrame windowFrame = null;
__VSOSEFLAGS openFlags = __VSOSEFLAGS.OSE_OpenAsNewFile;
string ownerCaption = null;
string mkDocument = “Untitiled1.sql”;
IServiceProvider serviceProvider = (IServiceProvider)package;
IntPtr punkDocDataExisting = IntPtr.Zero;
int hr = shellOpenDocument.OpenStandardEditor(
    (uint)openFlags,          
    mkDocument,               
    ref logicalViewGuid,      
    ownerCaption,             
    parentHierarchy,          
    itemId,                   
    punkDocDataExisting,
    serviceProvider,    
    out windowFrame     
);

This throws an exception -

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)

Could you kindly provide sample code or guide me to any related sample for this to open a in-memory 'Untitled1.sql' file using my custom editor factory ( so that it does not get opened with MSSql editor) ?

Developer technologies | Visual Studio | Extensions
{count} votes

3 answers

Sort by: Most helpful
  1. Q&A Assist
    2025-11-12T08:33:39.47+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    To open a new in-memory file using your custom editor factory in Visual Studio, you need to ensure that you are correctly implementing the OpenStandardEditor method and handling the parameters appropriately. The error you are encountering, "Attempted to read or write protected memory," often indicates an issue with how the parameters are passed or how the editor is set up.

    Here's a sample code snippet that demonstrates how to open a new in-memory file with a custom editor:

    IVsUIShellOpenDocument shellOpenDocument = Microsoft.VisualStudio.Shell.Package.GetGlobalService(typeof(SVsUIShellOpenDocument)) as IVsUIShellOpenDocument;
    Guid editorFactoryGuid = new Guid(<editor factory guid>);
    Guid logicalViewGuid = VSConstants.LOGVIEWID_Primary;
    IVsUIHierarchy parentHierarchy = null;
    uint itemId = (uint)VSConstants.VSITEMID.Nil;
    IVsWindowFrame windowFrame = null;
    __VSOSEFLAGS openFlags = __VSOSEFLAGS.OSE_OpenAsNewFile;
    string ownerCaption = null;
    string mkDocument = "Untitled1.sql";
    IServiceProvider serviceProvider = (IServiceProvider)package;
    IntPtr punkDocDataExisting = IntPtr.Zero;
    
    // Ensure to create a new document data object if necessary
    IntPtr punkDocDataNew = IntPtr.Zero;
    int hr = shellOpenDocument.OpenStandardEditor(
        (uint)openFlags,          
        mkDocument,               
        ref logicalViewGuid,      
        ownerCaption,             
        parentHierarchy,          
        itemId,                   
        punkDocDataNew,          // Use new document data pointer
        serviceProvider,          
        out windowFrame           
    );
    
    if (ErrorHandler.Succeeded(hr)) {
        // Successfully opened the document
    } else {
        // Handle the error
    }
    

    Key Points:

    1. Document Data Pointer: Ensure that you are using a valid document data pointer. If you are trying to open a new document, you might need to create a new document data object instead of using punkDocDataExisting.
    2. Error Handling: Always check the HRESULT returned by the OpenStandardEditor method to handle any errors appropriately.
    3. Custom Editor Registration: Make sure your custom editor is properly registered in the Visual Studio environment and associated with the .sql file extension.

    If the issue persists, consider reviewing the implementation of your custom editor factory to ensure it is set up correctly to handle in-memory documents.


    References:

    0 comments No comments

  2. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  3. Susmitha T (INFOSYS LIMITED) 1,640 Reputation points Microsoft External Staff
    2025-12-05T10:50:18.9233333+00:00

    This issue is not resolved. Call to OpenStandardEditor with the suggested parameters throws an exception and IVsEditorFactory.CreateEditorInstance is not getting called.

     
    Regarding the command mentioned above: To open an in-memory "Untitled1.sql" file with your custom editor factory, do not call OpenStandardEditor directly (if often causes Access Violation)

     

    Reference: Running Document Table - Visual Studio (Windows) | Microsoft Learn

     

    Let me know if you need any further help with this. I will be happy to assist. If you find this helpful, Kindly mark the provided solution as "Accept Answer", so that others in the community facing similar issues can easily find the solution. Your contribution is highly appreciated.


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.