Convert to VB.NET

  • 12 years ago

    Hello,

    Please help me to convert the belowing code to VB.NET.


    Design event object

    The event object will have a custom interface, since that is what WinFIOL uses to call the object. The base class IWFEvent is defined in "wfoleaut.h" and "wfoleaut.tlb":

        class CWFEvent : public IWFEvent
        {
            int          iCount;
            IWFChannel * pIWFChannel;
            DWORD        dwThreadID;
            void         ReadPrintout(void);
        public:
            CWFEvent(IWFChannel *chnl);
            virtual ~CWFEvent() { }
            STDMETHOD(QueryInterface)(REFIID iid, void ** ppvObject);
            STDMETHOD_(ULONG, AddRef)(void);
            STDMETHOD_(ULONG, Release)(void);
            STDMETHOD(Notify)(INT event, BSTR eventstr);
        };












    Implementation

    Most of the code below is standard and can be found in many books about COM. The constructor takes a pointer to IWFChannel to be able to call methods in this interface.

        CWFEvent::CWFEvent(IWFChannel *chnl)
        {
            iCount = 0;
            pIWFChannel = chnl;
            dwThreadID = GetCurrentThreadId();
        }




        STDMETHODIMP CWFEvent::QueryInterface(REFIID riid, void ** ppv)
        {
            if ( riid == IID_IUnknown )
                *ppv = (IUnknown *) this;
            else if ( riid == IID_IWFEvent )
                *ppv = (IWFEvent *) this;
            else
            {
                *ppv = NULL;
                return E_NOINTERFACE;
            }
            ((IUnknown *) *ppv)->AddRef();
            return S_OK;
        }












        STDMETHODIMP_(ULONG) CWFEvent::AddRef(void)
        {
            return ++iCount;
        }


        STDMETHODIMP_(ULONG) CWFEvent::Release(void)
        {
            if ( iCount == 1 )
            {
                // make sure message loop is terminated:
                PostThreadMessage(dwThreadID, WM_QUIT, 0, 0);
                delete this;
                return 0;
            }
            return --iCount;
        }









        STDMETHODIMP CWFEvent::Notify(INT event, BSTR eventstr)
        {
            if ( event == WFEVENT_PRINTOUT )
                ReadPrintout();
            return S_OK;
        }





    Getting printout

    The function CWFEvent::ReadPrintout( ) is called from CWFEvent::Notify( ) when the WFEVENT_PRINTOUT is received from WinFIOL. This function calls IWFChannel::GetPrintoutArray( ). You can also call IWFChannel::GetPrintoutBuffer( ). For simplicity, this example uses the UNICODE version of printf( ) to print a BSTR. If the printout does not originate from the command that the COM client sent, the application needs to wait for the next printout. Otherwise, the WM_QUIT message is sent to the message loop in the send MML command example.

        void CWFEvent::ReadPrintout(void)
        {
            BOOL bQuit = TRUE;
            SAFEARRAY *sa = NULL;
            int flags = 0, faultcode = 0;
            BSTR command = NULL;




            HRESULT hr = pIWFChannel->GetPrintoutArray(&flags, &faultcode, &command, &sa);
            if ( SUCCEEDED(hr) )
            {
                BSTR *bstr;
                SafeArrayAccessData(sa, (void **) &bstr);
                int nLines = sa->rgsabound->cElements;    // number of lines
                for ( int i = sa->rgsabound->lLbound; i < nLines; i++ )
                    wprintf(L"%s", bstr[i]);
                SafeArrayUnaccessData(sa);
                if ( !command[0] )     // if command is empty, do not quit yet...
                    bQuit = FALSE;
            }
            SafeArrayDestroy(sa);
            SysFreeString(command);
            if ( bQuit )
                PostThreadMessage(dwThreadID, WM_QUIT, 0, 0);
        }















Post a reply

No one has replied yet! Why not be the first?

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“In theory, theory and practice are the same. In practice, they're not.”