How to use FEMM as an Out-of-Process ActiveX server.


David Meeker
dmeeker@ieee.org
October 25, 2004

Introduction


One way of tapping into the functionality of FEMM from an external program is to communicate with FEMM via ActiveX. Using ActiveX, external programs can send FEMM strings to be processed by its internal Lua parser. A string returned to the calling program which contains results, error messages, etc.

This note describes an example program that uses ActiveX to talk to FEMM. FEMM runs as an out-of-process ActiveX server. The example program connects to the FEMM server as a client. The source from the example program is available here.

Connecting to the FEMM ActiveX server


The following steps allow you to connect to the FEMM ActiveX server in MSVC++ 6.

1. Create a plain app wizard application, e.g. a dialog-based one.
2. Open the App Wizard, press "add class" button, then choose "from type library" rather than "New". In the file selection dialog that pops up, specify femm.tlb, e.g. located at c:\Program Files\femm40\bin\femm.tlb. The class wizard then creates femm.cpp and femm.h files which it adds to the project. These files describe the interface to FEMM.
3. Add AfxOleInit(); to the InitInstance of the main application
4. Define a member variable somewhere (e.g. as part of the main app, or the dlg part, or whatever) of type IActiveFEMM (which is what got created automatically by the class wizard and defined in femm.h).
5. Before ActiveX functions are called, fire up the server by calling the CreateDispatch("femm.ActiveFEMM"); member function of the IActiveFEMM member variable.
6. Do calls to FEMM via the call2femm() or mlab2femm() member functions of IActiveFEMM.
7. When done with the ActiveX server, shut it down with DetachDispatch();

The above steps were used to build ActiveX support into the xclient example considered in this note.

Example Program xclient


The xclient example is a dialog-based application that acts as an ActiveX client talking to the FEMM out-of-process ActiveX server. The program takes a line of input in one edit box in the dialog. When is pressed, the application sends the contents of the input edit box to the call2femm() function. The Lua scripting language embedded in FEMM parses the input, and the results are returned in a string with each results separated by a \n character.

The results returned by FEMM are then reported in the read-only edit box, also on the dialog. In other applications, one might want to obtain numerical values from the results, rather than simply printing the results. Typically, the number of results expected from the call is known, so that sscanf could be employed to most easily parse the results. For example, if four results were expected from FEMM, the line that took the four resulting numerical values and put them into the previously defined doubles x1 through x4 would be:

sscanf(u,"%lf\n%lf\n%lf\n%lf",&x1,&x2,&x3,&x4);

It is, of course, possible that commands sent to FEMM could result in an error. If this is the case, the FEMM error message will be returned from the call2femm function. All error messages start with the word error, with the text describing the error following.

Conclusions


Communicating with FEMM as an ActiveX client is actually fairly straightforward. Hopefully, this simple example has given you the knowledge necessary to access the FEMM ActiveX server from your programs.
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki