- Home
- Programming ZEMAX
- Extensions
- How Do I Write an Extension in C?
How Do I Write an Extension in C?
- By Mark Nicholson
- Published 4 April 2006
- Extensions
-
Rating:




Hello World

This code was copied verbatim from one of the supplied Extension sample files, and should be included in any C program you write to communicate with ZEMAX. The rest of the program is very simple:

The function MUST be called UserFunction. Communication is then performed by the function PostRequestMessage, which passes a data item to ZEMAX and receives a string variable szBuffer back with the response from ZEMAX. All the work in managing the communication is performed by ZCLIENT.c, which is supplied with ZEMAX in the {zemaxroot}/Extend folder.
ZCLIENT handles all of the communication with ZEMAX transparently. Embedded within ZCLIENT is a call to a single function given the name "UserFunction". This function is placed in a separate C program provided by the user, and is compiled along with ZCLIENT to create the Extension executable. When ZEMAX calls the Extension, execution begins within ZCLIENT. ZCLIENT establishes the DDE communication, then calls UserFunction: this is why your program must include a function called UserFunction.
In this particular program, we send a data item to ZEMAX using PostRequestMessage and then open a standard Windows Message Box to show the reply from ZEMAX.