I have no Fortran compiler to try this with so here is a C++ program
which calls C language functions in a DLL.
Create the files that are shown. A command to build the program would
be
make -f project.mak
where the space after the -f and the extension .mak are optional.
----ProgCode.CPP------
#include <windows.h>
#include "dllcode.h"
int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
char buffer[128];
int a = 3;
int b = 7;
wsprintf(buffer, "the sum %d + %d = %d",
a, b, Add2(a, b));
MessageBox(HWND_DESKTOP, buffer, "", MB_OK);
wsprintf(buffer, "the product %d * %d = %d",
a, b, Multiply2(a, b));
MessageBox(HWND_DESKTOP, buffer, "", MB_OK);
return 0;
}
-----DllCode.H--------
#ifndef DLLCODE_H
#define DLLCODE_H
/* if used by C++ code, identify these functions as C items */
#ifdef __cplusplus
extern "C" {
#endif
__declspec(dllimport) int Add2(int a, int b);
__declspec(dllimport) int Multiply2(int a, int b);
#ifdef __cplusplus
}
#endif
#endif
-----DllCode.C--------
#include <windows.h>
/* pragma suppresses "calling argument not used" warnings */
#pragma argsused
BOOL WINAPI DllEntryPoint(
HINSTANCE hinst, DWORD reason, LPVOID reserved)
{
return TRUE;
}
__declspec(dllexport)
int Add2(int a, int b)
{
return a + b;
}
__declspec(dllexport)
int Multiply2(int a, int b)
{
return a * b;
}
-----Project.MAK-----------
# comments start with the pound symbol
# automatically check if a header file has changed
.autodepend
# -WR windows GUI exe dynamic linked (-W for static linked)
# -v generate debug info
progcode.exe : dllcode.lib progcode.cpp
bcc32 -WR -v progcode dllcode.lib
# create an import library
# -c case sensitive
dllcode.lib : dllcode.dll
implib -c dllcode.LIB dllcode.DLL
# C code does not need runtime type id or exception
# handling so they are suppressed with -RT- and -x-
# noeh32.lib stubs out any of those things found in
# the libraries, making the output file smaller
# -WDR windows dll dynamic linked (-WD for static linked)
# -v generate debug info
dllcode.dll : dllcode.c
bcc32 -WDR -v -RT- -x- dllcode.c noeh32.lib
--------------------------
C:\Documents and Settings\Administrator\My Documents\Projects\DllDemo
make -f project.mak
MAKE Version 5.2 Copyright (c) 1987, 2000 Borland
bcc32 -WDR -v -RT- -x- dllcode.c noeh32.lib
Borland C++ 5.6.4 for Win32 Copyright (c) 1993, 2002 Borland
DLLCODE.C:
Turbo Incremental Link 5.66 Copyright (c) 1997-2002 Borland
implib -c dllcode.LIB dllcode.DLL
Borland Implib Version 3.0.22 Copyright (c) 1991, 2000 Inprise
Corporation
bcc32 -WR -v progcode dllcode.lib
Borland C++ 5.6.4 for Win32 Copyright (c) 1993, 2002 Borland
PROGCODE.CPP:
Turbo Incremental Link 5.66 Copyright (c) 1997-2002 Borland
C:\Documents and Settings\Administrator\My Documents\Projects\DllDemo
--------------------------
The example above uses an import library instead of a module
definition file. Except for the import library probably being easier
to use they perform an equivalent function. You create a module
definition file with the impdef.exe tool and a command line similar to
this:
impdef DllName.DEF DllName.DLL
. Ed
I mostly program in Fortran, but my current application needs to call
code written in C. I would like to use C++ Builder to generate a DLL
containing the C functions that will be called from the Fortran.
Please excuse the elementary level of my question, but can anyone
provide a very simple example of how a DLL, import library, and
module definition file are generated from C source code? Thank you.