Platform SDK: CDO for Windows 2000 |
The following code example can be used only with event sinks. The IMessage.EnvelopeFields property returns an empty collection in all cases except for a Message object reference passed to a sink during a transport event. This example loops through the envelope fields and writes each name-value pair to a log file called Fields.log.
Implements INNTPOnPost Sub INNTPOnPost_OnPost(ByVal iMsg as CDO.Message, EventStatus as CdoEventStatus) Dim Fld as ADODB.Field Dim strOutPut as String For Each Fld In iMsg.EnvelopeFields strOutPut = strOutPut & Fld .Name & ": " & Fld .Value & vbCrLf Next Dim FS as New Scripting.FileSystemObject Dim File as Scripting.TextStream Set File = FS.CreateTextFile("c:\envelopefieldlog\fields.log",True,False) File.Write strOutPut End Sub
#include <atlbase.h> #include <atlimpl.cpp> #import <msado15.dll> no_namespace raw_interfaces_only #import <cdosys.dll> no_namespace raw_interfaces_only #import <seo.dll> no_namespace raw_interfaces_only ///////////////////////////////////////////////////////////////////////////// // CSink1 class ATL_NO_VTABLE CSink1 : public CComObjectRootEx<CComSingleThreadModel>, public CComCoClass<CSink1, &CLSID_Sink1>, public IDispatchImpl<INNTPOnPost, &__uuidof(INNTPOnPost), &LIBID_TRANSSINKEXLib> { public: CSink1() { } DECLARE_REGISTRY_RESOURCEID(IDR_SINK1) DECLARE_PROTECT_FINAL_CONSTRUCT() BEGIN_COM_MAP(CSink1) COM_INTERFACE_ENTRY(INNTPOnPost) COM_INTERFACE_ENTRY(IEventIsCacheable) COM_INTERFACE_ENTRY(IDispatch) END_COM_MAP() // ISink1 public: STDMETHOD(IsCacheable)() { return S_OK;} STDMETHOD(OnPost)(IMessage* pMsg, CdoEventStatus* pStat); }; STDMETHODIMP MyClass::OnPost([in] IMessage *Msg, [in,out] CdoEventStatus *EventStatus) { // This example for the most part ignores // error values. This is not good practice // in general! HRESULT hr = S_OK; CComPtr<Field> pField; CComPtr<Fields> pFields; Msg->get_EnvelopeFields(&pFields); CComBSTR bstrName; CComVariant varValue; CComBSTR bstrOut = ""; long count = 0; pFields->get_Count(&count); for(long i = 0;i<count;i++) { pFields->get_Item(CComVariant((long)i), &pField); if(SUCCEEDED(hr)){ pField->get_Name(&bstrName); pField->get_Value(&varValue); bstrOut += bstrName; bstrOut += ":"; bstrOut += varValue.bstrVal; bstrOut += "\r\n"; bstrName.Empty(); varValue.Clear(); pField.Release(); } } LPCWSTR szFilename = L"c:\\logs\\nntpevents.txt"; HANDLE hFile; int nBufSize = 0; ULONG nSizeWritten = 0 ; BOOL fRC = FALSE; hFile = CreateFileW(szFilename, GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); if(hFile == INVALID_HANDLE_VALUE) return E_POINTER; SetFilePointer(hFile,0,NULL,FILE_END); nBufSize = lstrlenW(bstrOutPut); fRC = WriteFile(hFile,bstrOutPut,nBufSize * 2,&nSizeWritten,NULL); if(fRC == FALSE) return S_FALSE; CloseHandle(hFile); pFields.Release(); Msg->Release(); return hr; }