Visual C++ Example
In this section, we will use Microsoft Visual C/C++ to create a 32-bit DLL that contains an @USER function to perform the square root function. This is very easy to do if we make use of the AppWizard in Visual C++ to build the base code for the DLL. Or, you can find the code for this example in the USER\VC++ subdirectory off of your main LINGO directory. To build the base code, start the Visual C++ Developer Studio and do the following:
1. | Issue the File|New command. |
2. | You should now see a New dialog box. Select the Project Workspace options and then click OK. |
3. | You will now see a New Project Workspace dialog box. Give the project the name sqroot. In the Type box, select the MFC AppWizard (dll) option. Click on the Create button. |
4. | A new MFC AppWizard dialog box should appear. Simply click on the Finish button. |
5. | You should now see a New Project Information box containing a summary of the options selected for your project that resembles: |
Click the OK button to finish creating the base code for our DLL.
Now, edit the SQROOT.CPP file and add the modifications listed below in bold:
// sqroot.cpp : Defines the initialization
// routines for the DLL.
//
#include "stdafx.h"
#include "sqroot.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////
// CSqrootApp
BEGIN_MESSAGE_MAP(CSqrootApp, CWinApp)
//{{AFX_MSG_MAP(CSqrootApp)
// NOTE - the ClassWizard will add and
// remove mapping macros here.
// DO NOT EDIT what you see in these
// blocks of generated code!
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
CSqrootApp::CSqrootApp()
{
// The constructor
// Remove next line for a "quiet" version
// of MyUser.DLL
AfxMessageBox("@USER DLL installed");
}
CSqrootApp theApp;
#include <math.h>
extern "C" __declspec( dllexport)
void MyUser( int* pnNumberOfArgs,
double* pdArgs, double* dResult)
// This is an @USER routine callable by LINGO. In
// this particular case we simply take the
// square root of the first argument.
*dResult = sqrt( *pdArgs);
}
File: SQROOT.CPP
You should now be able to build the DLL. When Visual C++ completes the build, copy the SQROOT.DLL file to LINGO's startup directory (the one where LINGO15.EXE or LINGO64_15.EXE is located), and rename SQROOT.DLL to be MYUSER.DLL. Now, start LINGO and you should see the following dialog box confirming the DLL was successfully loaded:
Input a small model to compute the square root of 9 and solve it to get the following results:
If you don’t have a copy of Visual C++, you may experiment with this @USER routine by copying the DLL supplied with LINGO into your LINGO startup directory. You can find the SQROOT.DLL file in the USER\VC++ subdirectory off the main LINGO directory.