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);
                CComPtr pEO;
                if(S_OK ==  GetErrorInfo(NULL, &pEO)){
                        CComBSTR bstrDesc;
                        CString str;
                        str.Format("Error Description: %S\n", bstrDesc);

// 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)";
                hr = parser->get_Value(&Val);

                if (FAILED(hr))
                        AfxMessageBox("Could not get the value of the expression.");
                        return 1;
        char temp[256];
        CString str(::itoa( GetTickCount() - dwSeconds, temp, 10 ) );

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

        return 0;