9 static char THIS_FILE
[] = __FILE__
;
13 // This is called when the user first loads the add-in, and on start-up
14 // of each subsequent Developer Studio session
15 STDMETHODIMP
CDSAddIn::OnConnection (IApplication
* pApp
, VARIANT_BOOL bFirstTime
,
16 long dwCookie
, VARIANT_BOOL
* OnConnection
)
18 AFX_MANAGE_STATE (AfxGetStaticModuleState ());
19 *OnConnection
= VARIANT_FALSE
;
21 // Store info passed to us
22 IApplication
*pApplication
= NULL
;
25 hr
= pApp
->QueryInterface (IID_IApplication
, (void **) &pApplication
);
31 if (pApplication
== NULL
)
33 ReportInternalError ("IApplication::QueryInterface");
37 m_dwCookie
= dwCookie
;
39 // Create command dispatch, send info back to DevStudio
40 CCommandsObj::CreateInstance (&m_pCommands
);
43 ReportInternalError ("CCommandsObj::CreateInstance");
46 m_pCommands
->AddRef ();
48 // The QueryInterface above AddRef'd the Application object. It will
49 // be Release'd in CCommand's destructor.
50 m_pCommands
->SetApplicationObject (pApplication
);
52 hr
= pApplication
->SetAddInInfo ((long) AfxGetInstanceHandle (),
53 (LPDISPATCH
) m_pCommands
, IDR_TOOLBAR_MEDIUM
, IDR_TOOLBAR_LARGE
,
61 // Inform DevStudio of the commands we implement
62 if (! AddCommand (pApplication
, "VisVimDialog", "VisVimDialogCmd",
63 IDS_CMD_DIALOG
, 0, bFirstTime
))
65 if (! AddCommand (pApplication
, "VisVimEnable", "VisVimEnableCmd",
66 IDS_CMD_ENABLE
, 1, bFirstTime
))
68 if (! AddCommand (pApplication
, "VisVimDisable", "VisVimDisableCmd",
69 IDS_CMD_DISABLE
, 2, bFirstTime
))
71 if (! AddCommand (pApplication
, "VisVimToggle", "VisVimToggleCmd",
72 IDS_CMD_TOGGLE
, 3, bFirstTime
))
74 if (! AddCommand (pApplication
, "VisVimLoad", "VisVimLoadCmd",
75 IDS_CMD_LOAD
, 4, bFirstTime
))
78 *OnConnection
= VARIANT_TRUE
;
82 // This is called on shut-down, and also when the user unloads the add-in
83 STDMETHODIMP
CDSAddIn::OnDisconnection (VARIANT_BOOL bLastTime
)
85 AFX_MANAGE_STATE (AfxGetStaticModuleState ());
87 m_pCommands
->UnadviseFromEvents ();
88 m_pCommands
->Release ();
94 // Add a command to DevStudio
95 // Creates a toolbar button for the command also.
96 // 'MethodName' is the name of the method specified in the .odl file
97 // 'StrResId' the resource id of the descriptive string
98 // 'GlyphIndex' the image index into the command buttons bitmap
99 // Return true on success
101 bool CDSAddIn::AddCommand (IApplication
* pApp
, char* MethodName
, char* CmdName
,
102 UINT StrResId
, UINT GlyphIndex
, VARIANT_BOOL bFirstTime
)
107 CmdText
.LoadString (StrResId
);
109 CmdString
+= CmdText
;
111 CComBSTR
bszCmdString (CmdString
);
112 CComBSTR
bszMethod (MethodName
);
113 CComBSTR
bszCmdName (CmdName
);
115 // (see stdafx.h for the definition of VERIFY_OK)
118 VERIFY_OK (pApp
->AddCommand (bszCmdString
, bszMethod
, GlyphIndex
,
120 if (bRet
== VARIANT_FALSE
)
122 // AddCommand failed because a command with this name already exists.
123 ReportInternalError ("IApplication::AddCommand");
127 // Add toolbar buttons only if this is the first time the add-in
128 // is being loaded. Toolbar buttons are automatically remembered
129 // by Developer Studio from session to session, so we should only
130 // add the toolbar buttons once.
131 if (bFirstTime
== VARIANT_TRUE
)
132 VERIFY_OK (pApp
->AddCommandBarButton (dsGlyph
, bszCmdName
, m_dwCookie
));
137 void ReportLastError (HRESULT Err
)
142 FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM
| FORMAT_MESSAGE_ALLOCATE_BUFFER
,
144 MAKELANGID (LANG_NEUTRAL
, SUBLANG_DEFAULT
),
146 sprintf (Msg
, "Unexpected error (Error code: %lx)\n%s", Err
, Buf
);
148 ::MessageBox (NULL
, Msg
, "VisVim", MB_OK
| MB_ICONSTOP
);
153 void ReportInternalError (char* Fct
)
157 sprintf (Msg
, "Unexpected error\n%s failed", Fct
);
158 ::MessageBox (NULL
, Msg
, "VisVim", MB_OK
| MB_ICONSTOP
);