IVsRunningDocumentTable.RenameDocument – how to call in managed code

Here’s a little code to help managed packages call the Running Document Table to rename a document:

Calling IVsRunningDocumenTable.RenameDocument is not entirely straighforward in managed code.  Here’s some code the shows how to get the SVsRunningDocumentTable service, find a document therein and then rename it.  I’ve had one partner confused by the calling pattern so I figured I’d share the solution with you in case you ever need to employ it.

IVsRunningDocumentTable pRDT = GetService(typeof(SVsRunningDocumentTable)) as IVsRunningDocumentTable;
IVsUIShellOpenDocument doc = GetService(typeof(SVsUIShellOpenDocument)) as IVsUIShellOpenDocument;
IVsUIShell uiShell = GetService(typeof(SVsUIShell)) as IVsUIShell;

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

IVsHierarchy pIVsHierarchy;
uint itemId;
IntPtr docData;
uint uiVsDocCookie;
NativeMethods.ThrowOnFailure(pRDT.FindAndLockDocument((uint)_VSRDTFLAGS.RDT_NoLock, oldName, out pIVsHierarchy, out itemId, out docData, out uiVsDocCookie));

if (docData != IntPtr.Zero) {
    IntPtr pUnk = Marshal.GetIUnknownForObject(pIVsHierarchy);
    Guid iid = typeof(IVsHierarchy).GUID;
    IntPtr pHier;
    Marshal.QueryInterface(pUnk, ref iid, out pHier);

    try {
        NativeMethods.ThrowOnFailure(pRDT.RenameDocument(oldName, newName, pHier, itemId));
    } finally {


I hope this help!


Comments (0)