Example of an event DLL with a function

The example below contains a "C" function, EHI_Function, that is called from an event in a Kofax ReadSoft Invoices module. The function ignores any arguments and it Application.GetVersion, which returns the program version string. The result is displayed in a message box.

extern "C" __declspec(dllexport) DWORD CALLBACK

EHI_Function( LPDISPATCH disp, DISPPARAMS *pDP )

{

// Name of method to call

//

char Method[] = "GetVersion"; 

 

// Convert method name into UNICODE

//

HRESULT hr; 

DISPID dispid; 

int slen = strlen( Method ), s; 

int OLEStrSize = (slen+1) * sizeof( OLECHAR ); 

OLECHAR *OLEEventName = (OLECHAR*)alloca( OLEStrSize ); 

s = ::MultiByteToWideChar(CP_ACP, 0, Method, slen, OLEEventName, OLEStrSize ); 

 

assert( s != 0 ); // Character conversion failure

 

OLEEventName[ slen ] = 0; // MultiByteToWideChar() doesn't zero-terminate so we'll have to do that..

// Get the dispatch id so that we can call the function

//

hr = disp->GetIDsOfNames( IID_NULL, &OLEEventName, 1, 0, &dispid ); 

 

// Invoke the method

//

DISPPARAMS *pDispparams=0; 

VARIANT OLEResult; 

VariantInit( &OLEResult ); 

DISPPARAMS dpNoArg = { NULL, 0, 0 }; 

// No arguments, use the empty DISPPARAMS

pDispparams = &dpNoArg; 

 

// initialize EXCEPINFO struct

EXCEPINFO excepInfo; 

memset(&excepInfo, 0, sizeof excepInfo); 

 

UINT nArgErr = (UINT)-1; // initialize to invalid arg

 

hr = disp->Invoke(dispid, IID_NULL, 0, DISPATCH_METHOD, pDispparams, &OLEResult, &excepInfo, &nArgErr ); 

 

// This method returns a string containing the program version

assert( V_VT( &OLEResult ) == VT_BSTR ); 

 

 

// Use a Microsoft provided class _bstr_t since it is convenient..

_bstr_t Str( V_BSTR( &OLEResult ) ); 

 

char *tmpstr = Str; 

char Text[256]; 

sprintf( Text, "DLL: EHI_Function() Called, Program Version is '%s'", tmpstr ); 

 

MessageBox( NULL, Text, "DLL:", MB_OK ); 

 

// Clean up the returned string

//

SysFreeString( V_BSTR( &OLEResult ) ); 

 

return 0;

Related topics

DLL event handlers

Where to place event handlers