#define INTERRUPTED 1 #define INSUFFICIENT_MEMORY 2 #define MUST_BE_REAL 3 #define NUMBER_OF_ERRORS 3 // table of error messages // if your function never returns an error -- you do not need to create this table char * myErrorMessageTable[NUMBER_OF_ERRORS] = { "interrupted",. "insufficient memory", "must be real" };
// this code executes the multiplication LRESULT MultiplyRealArrayByRealScalar( COMPLEXARRAY * const Product, LPCCOMPLEXSCALAR Scalar, LPCCOMPLEXARRAY Array ) { unsigned int row, col; // check that the scalar argument is real if ( Scalar->imag != 0.0) // if it is not, display "must be real" error message // under the scalar argument ( the 1st argument ) return MAKELRESULT( MUST_BE_REAL, 1)
// check that the array argument is real if ( Array->hImag != NULL ) // if it is not, display "must be real" error message // under the array argument ( the 2nd argument ) return MAKELRESULT( MUST_BE_REAL, 2); // allocate memory for the product if( !MathcadArrayAllocate( Product, Array-rows,Array-cols, TRUE, // allocate memory for the real part FALSE )) // do not allocate memory for the imaginary part
// if allocation is not successful, return with the appropriate error code return INSUFFICIENT_MEMORY; // if all is well so far -- go ahead and perform the multiplication for ( col = 0; col < Product-> cols; col++ ) { // check that a user has not tried to interrupt the calculation if (isUserInterrupted()) { // if user has interrupted -- free the allocated memory MathcadArrayFree( Product ); // and return with an appropriate error code return INTERRUPTED; } for ( row = 0; row < Product-> rows; row++ ) Product->hReal[col][row] = Scalar-> real*Array-> hReal[col][row]; }
FUNCTIONINFO multiply = { // name by which Mathcad will recognize the function "multiply",
// description of "multiply" parameters to be used "a,M",
// description of the function "returns the product of real scalar a and real array M",
// pointer to the executable code // i.e. code that should be executed when a user types "multiply(a,M)=" (LPCFUNCTION)MultiplyRealArrayByRealScalar;
// multiply(a, M) returns a complex array COMPLEX_ARRAY, // multiply takes on two arguments 2, // the first is a complex scalar, the second a complex array { COMPLEX_SCALAR, COMPLEX_ARRAY} };
// DLL entry point code // the _CRT_INIT function is needed if you are using Microsoft's 32-bit compiler
BOOL WINAPI _CRT_INIT(HINSTANCE hinstDLL, DWORD dwReason, LPVOID lpReserved); BOOL WINAPI DllEntryPoint (HINSTANCE hDLL, DWORD dwReason, LPVOID lpReserved) { switch (dwReason) { case DLL_PROCESS_ATTACH: // DLL is attaching to the address space of the current process if (!_CRT_INIT(hDLL, dwReason, lpReserved)) return FALSE;
if ( CreateUserErrorMessageTable( hDLL, NUMBER_OF_ERRORS, myErrorMessageTable ) ) // and if the errors register properly, register the user function CreateUserFunction( hDLL, &multiply ); break; case DLL_THREAD_ATTACH: case DLL_THREAD_DETACH: case DLL_PROCESS_DETACH: if (!_CRT_INIT(hDLL, dwReason, lpReserved)) return FALSE; break; } return TRUE; } #undef INTERRUPTED #undef INSUFFICIENT_MEMORY #undef MUST_BE_REAL #undef NUMBER_OF_ERRORS