MATLAB Compiler | ![]() ![]() |
Main Routine Written in C
If your main routine is coded in C (as opposed to being written as an M-file), you must:
This section references source code from a sample stand-alone application written for Microsoft Windows. The main routine WinMain
is written in C. The source code illustrates how to register and write a print handler.
The application is built from two files:
mrankwin.c
, which contains WinMain
, WinPrint
(the print handler), and a related function WinFlush
mrank.m
Both mrankwin.c
and mrank.m
are located in the <matlab>/extern/examples/compiler/
directory of your installation.
The WinMain
routine in mrankwin.c
is straightforward:
mlfMrank
, which determines the rank of the magic squares from 1 to n.mlfMrank
.The first and last items in this list refer to print handlers.
Registering a Print Handler
To register a print handler routine, call the MATLAB C Math Library routine mlfSetPrintHandler
as the first executable line in WinMain
(or main
). mlfSetPrintHander
takes a single argument, a pointer to a print handler function.
For example, the first line of WinMain
in mrankwin.c
registers the print handler routine named WinPrint
by calling mlfSetPrintHandler
.
mlfSetPrintHandler(WinPrint);
Writing a Print Handler
Whenever an mlf
function within a stand-alone application makes a request to write data, the application automatically intercepts the request and calls the registered print handler, passing the text to be displayed. In fact, the application calls the print handler once for every line of data to be output.
The print handler that you write must:
The print handler routine WinPrint
in the example program illustrates one possible approach to writing a print handler for a Windows program.
When the example WinMain
routine prints the array returned by mlfMrank
,
mlfPrintMatrix(R);
the registered print handler, WinPrint
, is called. If array R
contains a 12-row result from the call to mlfMrank
, the application calls WinPrint
12 times, each time passing the next line of data. The print handler WinPrint
dynamically allocates a buffer to hold the printable contents of array R
and appends each text string passed to it to the buffer.
In this design, the print handler prints to a buffer rather than the screen. A companion function WinFlush
actually displays the 12 lines of data in a Windows message box.
In the example, WinMain
calls WinFlush
immediately following the call to mlfPrintMatrix
.
mlfPrintMatrix(R);
WinFlush();
Though WinFlush
is not part of the print handler, this implementation of a print handler requires that you call WinFlush
after any mlf
function that causes a series of calls to the print handler. For this short program, this design is appropriate.
Here is the source code from mrankwin.c
for WinPrint
and WinFlush
. It includes:
static int totalcnt = 0; static int upperlim = 0; static int firsttime = 1; char *OutputBuffer;
void WinPrint( char *text) { int cnt; /* Allocate a buffer for the output */ if (firsttime) { OutputBuffer = (char *)mxCalloc(1028, 1); upperlim += 1028; firsttime = 0; } /* Make sure there's enough room in the buffer */ cnt = strlen(text); while (totalcnt + cnt >= upperlim) { char *TmpOut; upperlim += 1028; TmpOut = (char *)mxRealloc(OutputBuffer, upperlim); if (TmpOut != NULL) OutputBuffer = TmpOut; } /* Concatenate the next line of text */ strncat(OutputBuffer, text, cnt); /* Update the number of characters stored in the buffer */ totalcnt += cnt; }
WinFlush
that actually displays the text from the output buffer in a Windows message box.void WinFlush(void) { MessageBox(NULL, OutputBuffer, "MRANK", MB_OK); mxFree(OutputBuffer); }
For more details on mlfPrintMatrix
, see the MATLAB C Math Library User's Guide.
![]() | Print Handlers | Main Routine Written in M-Code | ![]() |