VB.NET ExampleTable of ContentsC DLL Example
Visual C++ Example

 Using Formula Parser COM Component in Visual C++

Here is a small code snippet for Visual C++ Developers. This example uses early binding(vtable), and does not use the VC++ wizard generated wrapper classes that call the component methods through IDispatch. Component does support IDispatch but using IDispatch is slower than making direct method calls as sampled below.

This sample calculates a given expression many times in a loop and shows the result and the time elapsed.

// At some point, just like including header files,
// you should import the component dll:

#include "stdafx.h"
#include "myheader.h"

#import <bcParser.dll>
//
/////////////////////////////////////////////////////////////////////////////
//This function reports the last error associated with a COM interface.

void ReportErrorInfo(IUnknown * punk){
        CComBSTR bstrError;
        CComPtr pSEI;
        HRESULT hr = punk->QueryInterface(IID_ISupportErrorInfo,(void **) &pSEI);
        if(SUCCEEDED(hr)){
                CComPtr pEO;
                if(S_OK ==  GetErrorInfo(NULL, &pEO)){
                        CComBSTR bstrDesc;
                        pEO->GetDescription(&bstrDesc);
                        CString str;
                        str.Format("Error Description: %S\n", bstrDesc);
                        AfxMessageBox(str);
                }
        }
}

       
// At some point in your program you can get a pointer to the IUnknown.
// Then you query it to get a pointer to BCPARSERLib::IParser


        HRESULT hr;
        CComPtr<IUnknown> unk;
        hr = unk.CoCreateInstance(L"BcParser.Parser");
       
        if (FAILED(hr))
        {
                AfxMessageBox("Could not create Parser");
                return 1;
        }
       
        CComQIPtr<BCPARSERLib::IParser> ParserHolder(unk);
        //to make the intellisense of the IDE work:
        BCPARSERLib::IParser *parser = (BCPARSERLib::IParser*)ParserHolder.p;

        DWORD dwSeconds = GetTickCount();
        WORD LOOP_COUNT = 1000;
        double Val;
        for (int nLoop = 0; nLoop < LOOP_COUNT; nLoop++)
        {
                CComBSTR bstrText = L"x+5+sin(x)";
                parser->put_Expression(bstrText);
                hr = parser->get_Value(&Val);

                if (FAILED(hr))
                {
                        AfxMessageBox("Could not get the value of the expression.");
                        ReportErrorInfo(unk);
                        return 1;
                }
        }
        char temp[256];
        CString str(::itoa( GetTickCount() - dwSeconds, temp, 10 ) );
        AfxMessageBox(str);
       
        parser->get_Value(&Val);

        str = ::_gcvt(Val, 10, temp );
        AfxMessageBox(str); //display the result.

        return 0;
 

webmaster@bestcode.com