>> Rodney Roberts IS & Education Professional Homepage   >> Programming Tutorials And Downloads

Discover Engineering

Science makes it known,
Engineering makes it work,
Art makes it beautiful.


D .dll Calling Free Pascal .dll - D Files (module CmdLine)
Parsing Command Line Parameters Processing

Command line parameters are optional arguments passed to a program at start up.  The first two images below show two methods for passing
these arguments.

D Windows programs' command line parameters are accessible as a single LPSTR (Windows type - long pointer to string - basically char*)
type variable (3rd parameter passed to WinMain).  It will be necessary to break this string down into individual parameters.   D module
CmdLine is meant to be a general purpose command line parameter parser used by D main programs.  Multiple D, Lazarus, and
Object Pascal programs call one or more of the functions/procedures on these two pages.

These two pages demonstrates command line parameter parsing using a D .dll calling a Free Pascal .dll to achieve software reuse. The DMD32 D v2.066.0, Free Pascal IDE 2.6.4, and Lazarus 1.2.6 compilers were used.

Module CmdLine calls Object Pascal Library ALlazrs, which uses Lazarus unit NumRecGlbl.

This is the next step of command line argument parsing developed in winstat - D Windows Statistical Analysis1 and Lazarus, Free Pascal, Silverfrost FTN95, and Numerical Recipes1 (as well as an example of software reuse of Lazarus program ST95DLPH's unit NumRecGlbl).

When using module CmdLine, can process either right-click and Open With (as shown at right), or in a Command Prompt (lower right), prefix argument with a predefined switch.


    (Notepad++ was used to create/edit the .d and .di files - the tabbing and spacing will be off in MS Notepad)
  1. module CmdLine CmdLine.d - D source file; Command Line processing primary .dll, procedures called by D main programs; calls procedures in allazrs.dll;
    CmdLine.def - CmdLine module definition file (accessed during compilation)
    CmdLine.dll - CmdLine.d dynamic link library
    CmdLine.lib - CmdLine.dll import library - used when linking D main program
  2. CmdLine.di - D interface file, contains exported definitions (constants and functions) - referenced when compiling D main program in import ... statement
  3. ALlazrs.pas and associated include/unit files - Pascal backend;
    allazrs.dll - ALlazrs.pas (and associated files) dynamic link library, compiled with a modified fpcdll.bat;
    allazrs.lib - allazrs.dll import library (built with DMD's implib) - used when linking CmdLine.dll
       Windows Explorer right click file open with

D2 32-bit Command Prompt passing arguments

wmath (more advanced and functional version of Simulated Annealing Demonstration) being invoked with
/f switch (input file) and /nd switch (number of nodes or data points);  can also process /f"insrtnd.dat" (quotes required for file names with spaces).

Other available switches (not all switches are used by all programs):
/h   help (can also use /?); no parameter
/ba  branch angle; Dawkins8 biomorphs
2; numeric parameter
/bl  branch length; Dawkins8; numeric parameter
/dt  time interval; Rossler
3; numeric parameter
/g   number of generations; Iterated Map 2
3, Cagann12, Degaris12; numeric parameter
/m   mutation frequency; Cagann1, Degaris1; numeric parameter (available Pascal applications only)
/ns  number of silent calculations / time interval; Rossler; numeric parameter
/t1  start time; Rossler; numeric parameter
/t2  end time; Rossler; numeric parameter
/th  tree height; Dawkins8; numeric parameter

Can use - as well as /; can use upper case letters in switch; example:
wmath -Finsrtnd.dat -ND60

Obviously, this can be expanded - /t1 and /t2 can be used for any program using start and end time,
/ba for angles (in degrees), etc.

(primary exported function in CmdLine.d - download and examine source code for additional information;
screen shot taken from Notepad++)
CmdLine.d primary command line processing function

Selected Important CmdLine.d Statements:
    pragma (...) declares external library.
    extern (Pascal) {...} declares external function(s)/procedure(s) - note that it does not specify which library.
    extern (<calling model>)
      <returned data type> <external procedure name> (<passed parameter data types list>);

    void (or void*) indicates this is a procedure (does not return value).
    NOTE - using extern (Pascal) {...} requires the arguments be passed to DCMDLINE (...) in reverse order.
Additional CmdLine.d statements
(above partial excerpt comes from CmdLine.d)
Since the D simulated annealing program only requires two command line parameters maximum (input file name and/or number of points),
decided to take approach of boolean function simAnlCmdLine (...) above (not the most efficient but the simplest to avoid code duplication).
simAnlCmdLine (...) defines two dummy variables, f1 and s1, using them as 'placeholders' in call to function prcCmdLine (...).
prcCmdLine (...) returns true if help switch encountered.

  1. open a D2 32-bit Command Prompt
  2. compile & link ALlazrs.pas:
    fpcdll ALlazrs
    implib allazrs.lib allazrs.dll
  3. compile & link CmdLine.d:
    dmd -c CmdLine.d -g
    dmd CmdLine.obj CmdLine.def -g
    implib /noi /system CmdLine.lib CmdLine.dll

    (The above three lines can be placed in a batch file for easier compiling)

Calling module CmdLine:
Simulated Annealing Windows program calling CmdLine function
(above partial excerpt comes from wmath.d)
Suggested usage for D main program processing application that may use input filename and/or number of data points input parameters.
Recommend perform command line processing call before the ShowWindow(...) call.

Common fError returned values:
      -32768 : No file name supplied
              0 : archive file (read/write capable)
FILE_ATTRIBUTE_READONLY : file is read only
other values : error occurred

1. Press, William H., Brian P. Flannery, Saul A Teukolsky, and William T. Vetterling (1986).  Numerical Recipes: The Art of
Scientific Computing
New York:Press Syndicate of the University of Cambridge.
2. Rietman, Edward (1994). Genesis Redux: Experiments Creating Artificial Life.  Windcrest (McGraw-Hill).
Dawkins8 biomorphs - resembles both L-systems and iterated function systems; ported from MS-DOS Turbo Pascal main program to Object
Pascal Library (genecode.dll and dawkins8.dll); called from Pascal Windows program plife and D Windows program wlife.
Cagann1 - simple genetic algorithm neural network, also has some cellular automata properties; ported from MS-DOS Turbo Pascal main
program to Object Pascal Library (cagann1.dll); called from Pascal Windows program plife
Degaris1 - simple genetic algorithm neural network; ported from MS-DOS Turbo Pascal main program to Object Pascal Library (degaris1.dll);
called from Pascal Windows program plife
3. Rietman, Edward (1993).  Creating Artificial Life: Self-Organization. Windcrest (McGraw-Hill).
Rossler - 3D Strange Attractor; ported from GW-BASIC to a procedure in D module alife; called from D Windows program wlife
Iterated Map 2 - Iterated nonlinear system; ported from GW-BASIC to a procedure in D module alife; called from D Windows program wlife

Any and all © copyrights, ® ™ trademarks, or other intellectual property (IP) mentioned here are the property of their respective owners.  No infringement is intended.

Feel free to use any of the above in your project (without violating any intellectual property rights); please give credit (same idea as Copyleft).

Page best viewed with Mozilla FireFox (or higher), Google Chrome Version 40.0.2214.94, and Safari 5.1.7.

Free web hosting provided by  
GigaRocket Web Hosting Free Web Hosting. ,   & Hostinger Free Web Hosting.

>> Rodney Roberts IS & Education Professional Homepage   >> Programming Tutorials And Downloads