In designing a Visual Basic (VB) application it is sometimes necessary to call a function written in C or Fortran. This is typically done by packaging the function in a DLL (Dynamic Linked Library) and then accessing the function through the DLL interface. We discuss in this article the more complicated issue that arises when the C or Fortran must access the VB code.
We encountered this issue while developing a test program for GRG2. Our test program is written in VB. The test problems are part of the VB program and the objective and constraint functions for these constrained nonlinear optimization problems are coded in the VB language. The GRG2 optimization code is written in C. Our VB program calls GRG2 via its DLL interface and this is straightforward to do. In order to get values for the objective function and the constraint functions, GRG2 must call the VB program and this takes a little more work.
Our test system has four main parts: GRG2, auxiliary C code that communicates between the VB program and GRG2, the VB program, and VB code to evaluate functions for the test problems. The diagram on page 2 shows the four application components and how they communicate with one another.
GRG2 gets objective and constraint function values by calling a C procedure (a pointer) that is passed to it. In order for this to work, this C procedure must be able to make a callback to the VB program. We use the MsgHook control to setup a callback from GRG2 to the VB program. MsgHook provides a way to intercept and respond to messages sent from any Windows program to a Visual Basic application.
Next, we give an overview of the callback process. The interface to our VB test program allows the user to select a test problem from a long list of problems. Once the problem has been selected and the Optimize button clicked, the VB program assigns its Window handle (fmGrg2ez.hwnd) and the function evaluation message (GRG2_EVAL) to the appropriate MsgHook properties.
Msghook1.HwndHook = fmGrg2ez.hwnd
Msghook1.message(GRG2_EVAL) = True
The same information along with the problem size is passed to GRG2VB.
Call GRGSetup(fmGrg2ez.hwnd,
GRG2_EVAL, nvars+1, nfuns+1)
GRGSetup
passes a pointer (GRGgcomp, a C procedure that is part of GRG2VB) to GRG2 for function evaluation. Finally, our VB program calls GRG2 to start the optimization process.During the optimization process, when GRG2 needs function values, it calls GRGgcomp. GRGgcomp uses SendMessage to send the GRG2_EVAL message to the VB program (fmGrg2ez.hwnd). The GRG2_EVAL message is intercepted by the VB procedure Msghook1_Message. Msghook1_Message checks to see if it has received the GRG2_EVAL message. If so, it calls ComputeGforGRG. ComputeGforGRG calls GRGGetX(x), a GRG2VB procedure, to retrieve the variable values, x, from GRG2, which it uses to evaluate the objective function and the constraint functions. Then, ComputeGforGRG calls GRGPutG(g) to send the function values, g, back to GRG2. When this is finished, ComputeGforGRG returns to Msghook1_Message. Then, Msghook1_Message returns and GRGgcomp resumes execution returning the function values to GRG2. This completes the GRG2 function evaluation process.

GRG2 is accessed through Grg232.dll, the 32-bit version for Widows 95. Auxiliary C code, referred to as GRG2VB, needed to communicate with VB, is accessed through Grg2VB32.dll. The VB program and function evaluation code are written in Visual Basic 4.0 (32-bit Version).
If you are interested in learning more about the VB callback process or in modifying it for your own need, the VB project is available from our free download area:
Download GRG2VB Project (291KB self-extracting zip file, Grg2ezvb.EXE).
This zip file, which is a simplified version of our testing system, includes: all the VB code used in this example, Grg2VB32.dll, and a special evaluation version of Grg232.dll that solves problems with one variable and one constraint.
MsgHook is freeware distributed by the Waite Group Press. You can download the Msghook files at ftp://ftp.waite.com/pub/waite/msghook. ![]()

WTI or its products can be found at the following Internet addresses:
|
Visual Solutions |
|
|
Mathworks |
|
|
SIS Scientific Software |
|
|
SIS Scientific Software |
|
|
Addlink Software Cientifico |
|
|
SciTech International |
WTI has signed an agreement with MGA Software to provide optimization technology for use in MGA products. MGA Software offers simulation expertise and software to commercial, defense, and educational organizations throughout the world. MGA's main product is Advanced Continuous Simulation Language (ACSL, pronounced "axle"). MGA offers ACSL Optimize™ that gives the user the ability to do robust parameter estimation, optimization, and sensitivity analysis. Contact MGA for more information about ACSL and ACSL Optimize: call (800) 647-2275, or send e-mail to software@mga.com, or visit MGA's Web site http://www.mga.com. ![]()

Due to the increased activity and interest in the stock market, we have been approached by several groups to study the feasibility of using mathematical tools for the purpose of developing investing and speculation strategies.
One of the areas of interest is that of intraday trading. With the increase in SOES (small order entry system) trading, small investors can begin to compete with the larger institutions on a more equal basis. However the lack of analysis tools for the small trader puts him at a disadvantage relative to the institutional traders.
We are exploring the creation of a financial modeling application for intraday trading. Intraday stock prices are being collected and analyzed for trends. The detection of trends leads to buy and sell signals. For active stocks the price can change over 5000 times a day (that is, every 3-5 seconds). A typical trader has a difficult time keeping up with all this activity. The need for analysis tools is compelling.
Take a look at the graph below that indicates the long and short positions taken on a given stock over the course of an hour. ![]()

ON SITE CONSULTING We find that many of our clients have an explicit need to upgrade or add optimization to their products, but they do not have the time to invest in coming up to speed on the new technologies. At WTI we recognize this need and offer a site visit and consultation on your specific software needs. The cost for this service is $500 a day, plus expenses.
GUARANTEE We are so convinced in the quality of this service and our products in general that if you are not completely satisfied, we offer a 90 day money-back guarantee. ![]()
This is a quarterly newsletter of Windward Technologies, Inc. Please let us know what your interests are and what sort of articles you would like to see. Please contact us if you would like to have your name added to our newsletter mailing list or if you would like to contribute an article to the newsletter! ![]()
|
Phone: |
281-564-6523 |
|
Fax: |
281-564-6921 |
|
|
|
|
Mail: |
Windward Technologies, Inc. |
|
|
12039 Mulholland |
|
|
Meadows, TX 77477 |
|
|
|
|
E-mail: |
Regards,
Tom Aird
TomAird@aol.com and
Phil Smith
PWSmith@aol.com