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

  1. Start Microsoft Visual C++ .NET 2003.
  2. On the File menu, point to New, and then click Project.

    Visual C++ will display the New Project dialog box.

  3. In the Project Types list, select Visual C++ Projects.
  4. In the Templates list, select Win32 Project.
  5. In the Name box, type My_C_Functions
  6. In the Location box, type C:\
  7. If the button in the lower left-hand corner of the dialog box says More, click the button to expand the dialog box.
  8. Make sure the check box labeled Create directory for Solution is cleared.
  9. Click OK.

    Visual C++ will display the Win32 Application Wizard.

  10. Click Application Settings.
  11. Under Application type, select DLL.
  12. Under Additional options, select Empty project.
  13. Click Finish.

    Visual C++ will create the project.

  14. On the Project menu, click Add New Item.

    Visual C++ will display the Add New Item dialog box.

  15. In the Templates list, select C++ File.
  16. In the Name box, type Functions
  17. Click Open.
  18. 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;
    }
    
  19. On the File menu, click Save Functions.cpp.
  20. On the Project menu, click Add New Item.

    Visual C++ will display the Add New Item dialog box.

  21. In the Templates list, select Module-Definition File.
  22. In the Name box, type My_C_Functions.def
  23. Click Open.
  24. 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.

  25. On the File menu, click Save My_C_Functions.def.
  26. 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.