Board index » cppbuilder » EXE to DLL

EXE to DLL


2005-05-24 07:58:07 AM
cppbuilder94
I've been using C++BuilderX in developing this open source project,
www.jsbsim.org. I've been asked to create a DLL of the source files, minus
the main executable source file. It's not clear to me how to modify my
project to either outright build a DLL, or to support bulding both a DLL and
an executable.
Any suggestions?
Jon Berndt
 
 

Re:EXE to DLL

Assuming that you are using Borland's compiler and linker, then for an
executable the bcc32 compiler's command line has -W or -WC on it and
for a DLL it whould be replaced with -WD (note that -tW, -tWC, -tWD do
the same as -W, -WC and -WD respectively) The linker, ilink32, has
/Tpe on the commandline and for a DLL /Tpd is used instead.
Instead of main or WinMain, a DLL starts at one of the functions
DllEntryPoint or DllMain. The Win32 help, win32.hlp, has descriptions
of those functions. Alternatively you could look them up at the MSDN
site:
msdn.microsoft.com/library/default.asp
Functions which a DLL makes available for others are said to be
exported and from the view of an EXE or DLL that uses it they are
imported. A function is marked for export in the DLL source code with
the sequence
__declspec(dllexport)
and in the header file of the code which uses the DLL the function
prototype is decorated with
__declspec(dllimport)
It is easiest to use a function exported from a DLL if it uses the
__stdcall calling convention so both the function and its prototype
are decorated with __stdcall (or with the macro WINAPI which expands
to __stdcall). If the source code is C++ then it also should be
decorated with extern "C" so that C++ name mangling is suppressed.
While not specific to CBuilderX, this page presents a trivial example
of DLL and using program source code and some discussion of the things
involved.
www.mulroy.org/howto10.htm
Note that the code for make files on that page uses standard make file
syntax. If you are using gnu make it is not likely to work correctly
as gnu make uses a different syntax and does not support standard make
files.
. Ed
Quote
Jon Berndt wrote in message
news: XXXX@XXXXX.COM ...

I've been using C++BuilderX in developing this open source
project, www.jsbsim.org. I've been asked to create a DLL of
the source files, minus the main executable source file. It's
not clear to me how to modify my project to either outright
build a DLL, or to support bulding both a DLL and an
executable.
 

Re:EXE to DLL

Quote
Functions which a DLL makes available for others are said to be
exported and from the view of an EXE or DLL that uses it they are
imported. A function is marked for export in the DLL source code with
the sequence
__declspec(dllexport)

and in the header file of the code which uses the DLL the function
prototype is decorated with
__declspec(dllimport)

It is easiest to use a function exported from a DLL if it uses the
__stdcall calling convention so both the function and its prototype
are decorated with __stdcall (or with the macro WINAPI which expands
to __stdcall). If the source code is C++ then it also should be
decorated with extern "C" so that C++ name mangling is suppressed.
Thanks. OK, another question: so, for C++ code that is currently supported
on several platforms (g++ under Cygwin, Linux, etc., IRIX, Mac, CBuilder,
etc.), I suppose there is no simple way for the same code to support a DLL?
It looks like there will need to be significant code changes that are not
compatible across platforms with a simple executable. Yet, I suspect it is
possible. Probably through judicious and clever use of #ifdef and #define
statements?
Jon
 

{smallsort}

Re:EXE to DLL

Jon Berndt < XXXX@XXXXX.COM >wrote:
Quote
[...]
Thanks. OK, another question: so, for C++ code that is currently supported
on several platforms (g++ under Cygwin, Linux, etc., IRIX, Mac, CBuilder,
etc.), I suppose there is no simple way for the same code to support a DLL?
It looks like there will need to be significant code changes that are not
compatible across platforms with a simple executable. Yet, I suspect it is
possible. Probably through judicious and clever use of #ifdef and #define
statements?
What you usually do is to decorate function
names using a macro. For DLL generation, this
would expand to '__declspec(dllexport)', for
including DLL headers, it expands to import,
otherwise it's an empty macro.
Here's what we use:
#if defined(_WIN32)
# if defined( MYLIB_IMPORT)
# define MYLIB_FUNC_PROTO __declspec(dllimport)
# elif defined( MYLIB_EXPORT)
# define MYLIB_FUNC_PROTO __declspec(dllexport)
# else
# define MYLIB_FUNC_PROTO
# endif
#else
# define MYLIB_FUNC_PROTO
#endif
We define 'MYLIB_EXPORT' in the library's
project and 'MYLIB_IMPORT' for projects
where the lib is used and its headers are
included.
If your code isn't prepared for this, it
might be a bit of work to identify all
the functions that need to get exported
and maybe re-organize the headers so that
they are together. It's not really hard,
though. What I've done instead is to
create a library API wrapping the actual
code. This comes with its own set of
headers that are implemented mostly by
forwarding into the actual code. One
advantage of this is that the API might
not need to change even if the underlying
code changes. However, if this is worth
the trouble depends on your needs.
Quote
Jon
HTH,
Schobi
--
XXXX@XXXXX.COM is never read
I'm Schobi at suespammers dot org
"Coming back to where you started is not the same as never leaving"
Terry Pratchett
 

Re:EXE to DLL

For many of those platforms I was not aware that they supported a DLL.
For example, I do not know how to do a DLL for Linux and don't even
remember anything which said it was possible.
Therefore you will want to have alternate build arrangements,
conditional compilation.
For example:
When building for Windows the macro _WIN32 is defined so a DLL would
be created and a #ifdef can be used to configure a macro which
decorates exported functions in the manner used under Windows. The
existence of the macro definition can also trigger the expansion of
the DllEntryPoint function code.
Under Linux _WIN32 would not be defined so neither the DLL style
function decaration nor the DllEntryPoint function would be created.
. Ed
Quote
Jon Berndt wrote in message
news:4293089f$ XXXX@XXXXX.COM ...

Thanks. OK, another question: so, for C++ code that is currently
supported
on several platforms (g++ under Cygwin, Linux, etc., IRIX, Mac,
CBuilder,
etc.), I suppose there is no simple way for the same code to support
a DLL?
It looks like there will need to be significant code changes that
are not
compatible across platforms with a simple executable. Yet, I suspect
it is
possible. Probably through judicious and clever use of #ifdef and
#define
statements?