据我所知,这是国外论坛最早的一个例子,原贴在此:http://forum.oberonplace.com/showthread.php?t=1880&highlight=Plugins
贴上主要代码:
1 #include "stdafx.h" 2 #include "resource.h" 3 4 // For Visual Studio 2003 5 #import "VGCoreAuto.tlb" 6 rename("GetCommandLine", "VGGetCommandLine") 7 rename("CopyFile", "VGCore") 8 rename("FindWindow", "VGFindWindow") 9 10 // For Visual Studio 2005 11 /* 12 #import "libid:95E23C91-BC5A-49F3-8CD1-1FC515597048" version("d.0") 13 rename("GetCommandLine", "VGGetCommandLine") 14 rename("CopyFile", "VGCore") 15 rename("FindWindow", "VGFindWindow") 16 */ 17 18 static HINSTANCE g_hResource = NULL; 19 20 BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) 21 { 22 if(fdwReason == DLL_PROCESS_ATTACH) 23 { 24 g_hResource = (HINSTANCE)hinstDLL; 25 } 26 return TRUE; 27 } 28 29 class CWelcomeScreenPlugin : public VGCore::IVGAppPlugin 30 { 31 private: 32 VGCore::IVGApplication *m_pApp; 33 volatile ULONG m_ulRefCount; 34 long m_lCookie; 35 36 static INT_PTR CALLBACK DlgProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam); 37 void OnAppStart(); 38 39 public: 40 CWelcomeScreenPlugin(); 41 42 // IUnknown 43 public: 44 STDMETHOD(QueryInterface)(REFIID riid, void **ppvObject); 45 STDMETHOD_(ULONG, AddRef)(void) { return ++m_ulRefCount; } 46 STDMETHOD_(ULONG, Release)(void) 47 { 48 ULONG ulCount = --m_ulRefCount; 49 if(ulCount == 0) 50 { 51 delete this; 52 } 53 return ulCount; 54 } 55 56 // IDispatch 57 public: 58 STDMETHOD(GetTypeInfoCount)(UINT *pctinfo) { return E_NOTIMPL; } 59 STDMETHOD(GetTypeInfo)(UINT iTInfo, LCID lcid, ITypeInfo **ppTInfo) { return E_NOTIMPL; } 60 STDMETHOD(GetIDsOfNames)(REFIID riid, LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId) { return E_NOTIMPL; } 61 STDMETHOD(Invoke)(DISPID dispIdMember, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams, VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr); 62 63 // IVGAppPlugin 64 public: 65 STDMETHOD(raw_OnLoad)(VGCore::IVGApplication *Application); 66 STDMETHOD(raw_StartSession)(); 67 STDMETHOD(raw_StopSession)(); 68 STDMETHOD(raw_OnUnload)(); 69 }; 70 71 CWelcomeScreenPlugin::CWelcomeScreenPlugin() 72 { 73 m_pApp = NULL; 74 m_lCookie = 0; 75 m_ulRefCount = 1; 76 } 77 78 STDMETHODIMP CWelcomeScreenPlugin::QueryInterface(REFIID riid, void **ppvObject) 79 { 80 HRESULT hr = S_OK; 81 m_ulRefCount++; 82 if(riid == IID_IUnknown) 83 { 84 *ppvObject = (IUnknown *)this; 85 } 86 else if(riid == IID_IDispatch) 87 { 88 *ppvObject = (IDispatch *)this; 89 } 90 else if(riid == __uuidof(VGCore::IVGAppPlugin)) 91 { 92 *ppvObject = (VGCore::IVGAppPlugin *)this; 93 } 94 else 95 { 96 m_ulRefCount--; 97 hr = E_NOINTERFACE; 98 } 99 return hr; 100 } 101 102 STDMETHODIMP CWelcomeScreenPlugin::Invoke(DISPID dispIdMember, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams, VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr) 103 { 104 switch(dispIdMember) 105 { 106 case 0x0012: //DISPID_APP_START 107 OnAppStart(); 108 break; 109 } 110 return S_OK; 111 } 112 113 STDMETHODIMP CWelcomeScreenPlugin::raw_OnLoad(VGCore::IVGApplication *Application) 114 { 115 m_pApp = Application; 116 if(m_pApp) 117 { 118 m_pApp->AddRef(); 119 } 120 return S_OK; 121 } 122 123 STDMETHODIMP CWelcomeScreenPlugin::raw_StartSession() 124 { 125 try 126 { 127 m_lCookie = m_pApp->AdviseEvents(this); 128 } 129 catch(_com_error &e) 130 { 131 MessageBox(NULL, e.Description(), "Error", MB_ICONSTOP); 132 } 133 return S_OK; 134 } 135 136 STDMETHODIMP CWelcomeScreenPlugin::raw_StopSession() 137 { 138 try 139 { 140 m_pApp->UnadviseEvents(m_lCookie); 141 } 142 catch(_com_error &e) 143 { 144 MessageBox(NULL, e.Description(), "Error", MB_ICONSTOP); 145 } 146 return S_OK; 147 } 148 149 STDMETHODIMP CWelcomeScreenPlugin::raw_OnUnload() 150 { 151 if(m_pApp) 152 { 153 m_pApp->Release(); 154 m_pApp = NULL; 155 } 156 return S_OK; 157 } 158 159 void CWelcomeScreenPlugin::OnAppStart() 160 { 161 try 162 { 163 m_pApp->StartupMode = VGCore::cdrStartupDoNothing; 164 // To avoid 64 bit portability warning, store the long handle value into an INT_PTR 165 // before casting it to HWND: 166 INT_PTR nHandle = m_pApp->AppWindow->Handle; 167 HWND hAppWnd = reinterpret_cast<HWND>(nHandle); 168 INT_PTR nRet = DialogBoxParam(g_hResource, MAKEINTRESOURCE(IDD_WELCOME), hAppWnd, DlgProc, (LPARAM)this); 169 switch(nRet) 170 { 171 case IDC_NEWDOC: 172 m_pApp->CreateDocument(); 173 break; 174 175 case IDC_LASTDOC: 176 if(m_pApp->RecentFiles->Count > 0) 177 { 178 m_pApp->OpenDocument(m_pApp->RecentFiles->Item[1]->FullName, 0); 179 } 180 else 181 { 182 MessageBox(NULL, "No documents were editied yet.", "Error", MB_ICONSTOP); 183 } 184 break; 185 } 186 } 187 catch(_com_error &e) 188 { 189 MessageBox(NULL, e.Description(), "Error", MB_ICONSTOP); 190 } 191 } 192 193 INT_PTR CALLBACK CWelcomeScreenPlugin::DlgProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam) 194 { 195 if(uMsg == WM_COMMAND) 196 { 197 switch(LOWORD(wParam)) 198 { 199 case IDC_NEWDOC: 200 EndDialog(hDlg, IDC_NEWDOC); 201 break; 202 203 case IDC_LASTDOC: 204 EndDialog(hDlg, IDC_LASTDOC); 205 break; 206 207 case IDOK: 208 case IDCANCEL: 209 EndDialog(hDlg, 0); 210 break; 211 } 212 } 213 else if(uMsg == WM_INITDIALOG) 214 { 215 return 1; 216 } 217 return 0; 218 } 219 220 extern "C" __declspec(dllexport) DWORD APIENTRY AttachPlugin(VGCore::IVGAppPlugin **ppIPlugin) 221 { 222 *ppIPlugin = new CWelcomeScreenPlugin; 223 return 0x100; 224 }
时间: 2024-10-13 02:01:05