1891

IVsDropdownBarClient and GetEntryText: Problems with text buffers

Question:

In my Visual Studio extension, I'm going to read the text that is in the navigation bar. Therefore I listen to window created events and from the IVsCodeWindow object I get the IVsDropdownBar to get the current selection in the dropdown bar. This works fine. Then I'm using the following code snippet to extract the text of the current selection:

string text; barClient.GetEntryText(MembersDropdown, curSelection, out text); if (hr == VSConstants.S_OK) { Debug.WriteLine("Text: " + text); } else { Debug.WriteLine("No text found!"); }

However, this does not work. My extension crashes with an unhandled exception in the second line of the code snippet. I read the documentation and could find the following note:

<blockquote>

The text buffer returned in ppszText is typically created by the IVsDropdownBarClient object and the buffer must persist for the life of the IVsDropdownBarClient object. If you are implementing this interface in managed code and you need to have the string disposed of by the caller, implement the IVsCoTaskMemFreeMyStrings interface on the IVsDropdownBarClient interface.

</blockquote>

I assume that this is part of my problem, but I can't really understand what I have to change in my code to get it working. Any hints?

Answer1:

I'm pretty sure now that the Visual Studio SDK Interop DLLs have the wrong marshalling information for IVsDropDownbarClient.GetEntryText and that there's no way to call that method using that interface.

The best workaround I've found so far is:

<ol><li>

Use the tlbimp tool to generate an alternate Interop DLL for textmgr. (You can safely ignore the dozens of warnings including the one about GetTextEntry.)

tlbimp "c:\Program Files (x86)\Microsoft Visual Studio 11.0\VSSDK\VisualStudioIntegration\Common\Inc\textmgr.tlb"

</li> <li>

(Optional) If you're using source control, you'll probably want to copy the resulting file (TextManagerInternal.dll) to a subdirectory of your extension project and check it in as an external dependency.

</li> <li>

In your Visual Studio extension project, add a reference to the file (TextManagerInternal.dll).

</li> <li>

Add the following method that should properly handle the string marshalling.

static public string HackHackGetEntryText(IVsDropdownBarClient client, int iCombo, int iIndex) { TextManagerInternal.IVsDropdownBarClient hackHackClient = (TextManagerInternal.IVsDropdownBarClient) client; string szText = null; IntPtr ppszText = IntPtr.Zero; try { ppszText = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(IntPtr))); if(ppszText == IntPtr.Zero) throw new Exception("Unable to allocate memory for IVsDropDownBarClient.GetTextEntry string marshalling."); hackHackClient.GetEntryText(iCombo, iIndex, ppszText); IntPtr pszText = Marshal.ReadIntPtr(ppszText); szText = Marshal.PtrToStringUni(pszText); } finally { if(ppszText != IntPtr.Zero) Marshal.FreeCoTaskMem(ppszText); } return szText; }

}

</li> </ol>

Recommend

  • C++ COM object as property
  • WPF user control not being disposed
  • Can weak symbol be resolved among libraries during linking?
  • MYSQL DATE function running insanely slow in LEFT JOIN
  • Making a QList of an abstract class objects in C++/QT?
  • declare a transaction in ejb 3.0
  • FullTextHibernateSessionProxy is not visible from class loader
  • Shortest path in a 3D maze
  • Is it possible to restore a GraphicsDevice if something goes wrong with it?
  • I want to trace logs using a Macro multi parameter always null. problem c++ windows
  • applying background to first, and every third row in a dynamically generated table
  • Dropping support for JRE 1.3
  • Why dsofile.dll still need Office Installation?
  • How can I generate integers that satisfy some restrictions?
  • HTMLcollection 0 list length, [n] returns undefined, converting to an array returns an empty array
  • How to get the relative position of a Dictionary element?
  • Theme in user control
  • I am looking for an algorithm that calculates the power of a number. (x^y), x and y are integers . I
  • Catch in Observable stops HTTP Calls from Observable.interval
  • Using Python objects in C++
  • How to clear out the contents of a map when clear() method call throws UnsupportedOperationException
  • Weird session behaviour in codeigniter
  • What is the first step to using a REST API in Rails?
  • Can I use jquery to blank textarea fields or ajax like input boxes?
  • JPA CascadeType.REMOVE not working
  • why 0.1+0.2-0.3= 5.5511151231258E-17 in php [duplicate]
  • Complex trait requirements on struct
  • Simple Factory with reflection C#
  • iOS - Is this a task for enums?
  • Build Matrix of Comparisons in SQl Server
  • DataGridView: Pass by Value or Reference?
  • A class implementing two different IObservables?
  • Wrapping a c#/WPF GUI around c++/cli around native c++
  • Can someone please explain to me in the most layman terms how to use EventArgs?
  • Firefox Extension - Monitor refresh and change of tab
  • App restarts from wrong activity
  • x64 applications using gdi+: what are the consequences on performance?
  • Control modification in presentation layer
  • Knitr HTML Loop - Some HTML output, some R output
  • Benchmarking RAM performance - UWP and C#