Hello all,
I am newbie to IDE extensibility. Please help in the query:
I have a custom window (created throught VS package) whose fields I want to save when the save functionality of Visual Studio is invoked.
The save event(s) has to be handled from all sources: Save as, Save, Save all, Ctrl(s).
Any help or pointers for this will be of great help.
Thanks in advance.

Hook VS Save event(s)
darrellp
Sure. We use a C++ base class (below) to provide event handling for all document events. You should be able to convert this to other languages easily enough.
Cheers,
Anna
#pragma once
//lint -esym(1932, CAddInDocumentEventSink) // (Note -- Base class is not abstract -- More Effective C++ #33)
/// CAddInDocumentEventSink handles command events from the Visual Studio automation interfaces.
///
class CAddInDocumentEventSink : public IDispEventImpl<1,
CAddInDocumentEventSink,
&__uuidof(EnvDTE::_dispDocumentEvents),
&__uuidof(EnvDTE::FakeLIBID_EnvDTE), -1, -1>
{ //lint !e1932 (Note -- Base class is not abstract -- More Effective C++ #33)
// Construction/destruction
public:
CAddInDocumentEventSink(void)
{
}
virtual ~CAddInDocumentEventSink(void)
{
ATLASSERT(m_ptrDocumentEvents == NULL);
}
// Data members
protected:
EnvDTE::_DocumentEventsPtr m_ptrDocumentEvents; ///< Allows us to receive Document events from Visual Studio.
// Event sink map
public:
BEGIN_SINK_MAP(CAddInDocumentEventSink)
SINK_ENTRY_EX(1, __uuidof(EnvDTE::_dispDocumentEvents), 1, DocumentSaved)
SINK_ENTRY_EX(1, __uuidof(EnvDTE::_dispDocumentEvents), 2, DocumentClosing)
SINK_ENTRY_EX(1, __uuidof(EnvDTE::_dispDocumentEvents), 3, DocumentOpening)
SINK_ENTRY_EX(1, __uuidof(EnvDTE::_dispDocumentEvents), 4, DocumentOpened)
END_SINK_MAP()
// Operations
public:
/// This method should be called by CConnect::OnConnection() to initialise our event sinks.
///
/// \param pDTE An pointer to the DTE interface of the VS.NET IDE.
/// \return \em S_OK if if configured correctly.
///
HRESULT OnConnection(const CComPtr<EnvDTE::_DTE>& pDTE)
{
HRESULT hr = E_FAIL;
ATLASSERT(pDTE != NULL);
if (pDTE != NULL)
{
try
{
m_ptrDocumentEvents = pDTE->GetEvents()->GetDocumentEvents(NULL);
DispEventAdvise(m_ptrDocumentEvents);
hr = S_OK;
}
catch (const _com_error& e)
{
hr = e.Error();
}
}
return hr;
}
/// This method should be called by CConnect::OnDisconnection() to close down our event sinks.
void OnDisconnection(void)
{
try
{
// Unsubscribe for events
DispEventUnadvise(m_ptrDocumentEvents);
m_ptrDocumentEvents = NULL;
}
catch (const _com_error& e)
{
ATLTRACE( _T("ERROR: Unexpected exception %x in CAddInDocumentEventSink::OnDisconnection(): %s"),
e.Error(),
e.ErrorMessage() );
}
}
// Event handlers
protected:
virtual HRESULT __stdcall DocumentSaved(EnvDTE::Document* /*pDocument*/) { return S_OK; }
virtual HRESULT __stdcall DocumentClosing(EnvDTE::Document* /*pDocument*/) { return S_OK; }
virtual HRESULT __stdcall DocumentOpening(_bstr_t /*bsDocumentPath*/, VARIANT_BOOL /*bReadOnly*/ ) { return S_OK; }
virtual HRESULT __stdcall DocumentOpened(EnvDTE::Document* /*pDocument*/) { return S_OK; }
};
Shinji
Anna-Jayne is correct, but for more languages, you can download the automation samples from http://msdn.microsoft.com/vstudio/downloads/code/automation/default.aspx, specifically you want to look at the EventWatcher sample.
Craig
Keehan
To elaborate, I need to implement my own save functionality, for that I could see _dispDocumentEvents_DocumentSavedEventHandler ( http://msdn2.microsoft.com/en-us/library/envdte.aspx ). Can anyone provide some example for the custom save handler.
Thanks