Packaging a C Function within a DLL
Before you can call a C function from an IMLPlus program, you must first package the function within a DLL. The steps required to accomplish this task are highly dependent on the software development
tools used. The example shown here assumes the use of Microsoft Visual C++ .NET 2003.
To create a DLL that exports a C function
- Start Microsoft Visual C++ .NET 2003.
- On the File menu, point to New, and then click Project.
Visual C++ will display the New Project dialog box.
- In the Project Types list, select Visual C++ Projects.
- In the Templates list, select Win32 Project.
- In the Name box, type
My_C_Functions
- In the Location box, type
C:\
- If the button in the lower left-hand corner of the dialog box says More, click the button to expand the dialog box.
- Make sure the check box labeled Create directory for Solution is cleared.
- Click OK.
Visual C++ will display the Win32 Application Wizard.
- Click Application Settings.
- Under Application type, select DLL.
- Under Additional options, select Empty project.
- Click Finish.
Visual C++ will create the project.
- On the Project menu, click Add New Item.
Visual C++ will display the Add New Item dialog box.
- In the Templates list, select C++ File.
- In the Name box, type
Functions
- Click Open.
- In the Functions.cpp file, type the following source code:
extern "C" // prevents mangling of function names
int My_C_Function( int nNumElements,
double dScalar,
const double *pdXVec,
double *pdYVec )
{
int i;
for ( i = 0; i < nNumElements; ++i )
pdYVec[i] = dScalar * pdXVec[i];
return 0;
}
- On the File menu, click Save Functions.cpp.
- On the Project menu, click Add New Item.
Visual C++ will display the Add New Item dialog box.
- In the Templates list, select Module-Definition File.
- In the Name box, type
My_C_Functions.def
- Click Open.
- In the My_C_Functions.def file, move the cursor to the second line of the file and type the following statement:
EXPORTS My_C_Function
The first line specifies the name of the DLL and the second line exports the function.
- On the File menu, click Save My_C_Functions.def.
- On the Build menu, click Rebuild Solution.
Upon successful compilation and linking, the file My_C_Functions.dll should exist in the project's debug directory.