Subj : Re: Help with DLL, some additional questions. To : borland.public.cpp.borlandcpp From : Ed Mulroy [TeamB] Date : Sun Nov 21 2004 11:41 am The 56K size can easily be reduced. The language has defined runtime type id (RTTI) and exception handling (EH). The code to do that is large. If you change the command line bcc32 -WD mult.c to bcc32 -WD -RT- -x- mult.c noeh32.lib -RT- do not generate RTTI -x- do not generate EH noeh32.lib lib to stub out any EH or RTTI calls from the compiler's runtime library then the DLL size will shrink. If you are running on a machine where BC++ is installed or are willing to copy a DLL to the machine on which the program will run then this will decrease the size by dynamic linking to the compiler's runtime library. bcc32 -WDR -RT- -x- mult.c noeh32.lib A DLL exports functions by public name, not by the name seen in the source code. The public name is formed from the source code name, the language used and the calling conventions used. C++ allows multiple functions with the same name but different sets of calling arguments. The way which of the several functions are to be called is determined by the public name which has the calling argument list encoded with characters added to the end of the public name. The way to keep this from happening to your function is to identify it as extern "C". Here is a rewrite of your code which should build cleanly as either of C or C++ while maintaining the same exported name so that your program can find it. ----------------- #include #ifdef __cplusplus /* if compiled as C++ */ extern "C" { /* use C naming for what is in the block */ #endif #ifdef __BORLANDC__ #pragma argsused #endif BOOL WINAPI DllEntryPoint( HINSTANCE dllInst, DWORD reason, LPVOID i) { return TRUE; } __declspec(dllexport) int __stdcall mult(int a, int b) { return a*b; } #ifdef __cplusplus /* if compiled as C++ */ } /* end of the extern "C" block */ #endif ----------------- >1. Returning to you previous answer, I don't >understand where I would use second bit of >code that you supplied: > >Code: >---------------------------------------------- >#ifndef SOMEDLL_H >#define SOMEDLL_H > >/* handle if this C function is used from C++ */ >#ifdef __cplusplus >extern "C" >#endif > >__declspec(dllimport) int __stdcall mult(int, int); > >#endif That is the source to a header file, a file which would be included into a C or C++ source file that uses the DLL. I included it for completeness. You are not the only person reading messages on here and others who have interest in the answer may be using a DLL from their C or C++ program. Getting back to the subject of file size. The machine I am on today (Sunday) has C++ Builder 4 on it so your file sizes will differ but the relative difference between build methods will remain. Here is a screen capture of the building in the 3 different ways that I mentioned above showing file sizes. Note that the differences in file sizes are significant, 44,544, 22,528 and 5,632 bytes. ----------------- F:\Projects\LookAt\q402 >bcc32 -WD mult.c Borland C++ 5.4 for Win32 Copyright (c) 1993, 1999 Inprise Corporation mult.c: Turbo Incremental Link 4.00 Copyright (c) 1997, 1999 Inprise Corporation F:\Projects\LookAt\q402 >dir *.dll Volume in drive F is GATEWAY2 Volume Serial Number is 34A2-93ED Directory of F:\Projects\LookAt\q402 MULT DLL 44,544 11-21-04 2:31p mult.dll 1 file(s) 44,544 bytes 0 dir(s) 331,726,848 bytes free F:\Projects\LookAt\q402 >bcc32 -WD -RT- -x- mult.c noeh32.lib Borland C++ 5.4 for Win32 Copyright (c) 1993, 1999 Inprise Corporation mult.c: Turbo Incremental Link 4.00 Copyright (c) 1997, 1999 Inprise Corporation F:\Projects\LookAt\q402 >dir *.dll Volume in drive F is GATEWAY2 Volume Serial Number is 34A2-93ED Directory of F:\Projects\LookAt\q402 MULT DLL 22,528 11-21-04 2:31p mult.dll 1 file(s) 22,528 bytes 0 dir(s) 331,743,232 bytes free F:\Projects\LookAt\q402 >bcc32 -WDR -RT- -x- mult.c noeh32.lib Borland C++ 5.4 for Win32 Copyright (c) 1993, 1999 Inprise Corporation mult.c: Turbo Incremental Link 4.00 Copyright (c) 1997, 1999 Inprise Corporation F:\Projects\LookAt\q402 >dir *.dll Volume in drive F is GATEWAY2 Volume Serial Number is 34A2-93ED Directory of F:\Projects\LookAt\q402 MULT DLL 5,632 11-21-04 2:32p mult.dll 1 file(s) 5,632 bytes 0 dir(s) 332,087,296 bytes free F:\Projects\LookAt\q402 > ----------------- To identify the name of the Runtime Library DLL used in the third form of the build, run this command: tdump -em. mult.dll (do not overlook the period at the end of '-em.') The DLL beginning with "CC" is the one it needs. It would need to be placed into a directory in the path on any machine running mult.dll. Normally placing it in one of c:\windows (on some machines c:\winnt) or in the same directory as math.dll will work. .. Ed >tazzi wrote: > >I have had some success and some additional questions. >This dialog continues from an earlier question, >which was incorrectly posted at >newsgroups.borland.com/borland.public.cppbuilder >for those interested in following the entire thread. > >Using the first bit of code that you supplied, >I performed 3 experiments. The code is listed below: > >Code: >------------------------------------------------- >#include > >#ifdef __BORLANDC__ >#pragma argsused >#endif > >BOOL WINAPI DllEntryPoint( > HINSTANCE dllInst, DWORD reason, LPVOID i) > { > return TRUE; > } > >__declspec(dllexport) > >int __stdcall mult(int a, int b) >{ > return a*b; >} >-------------------------------------------------- > >Experiment #1: >1. From IDE, compile mult.c to mult.dll. > -> This produces the file mult.dll which > is 6.5kb long. >2. Start Maple and attempt to load with > the following command: > > > mult := > > define_external('mult', > > a::integer[4], > > b::integer[4], > > LIB="mult.dll", > > RETURN::integer[4]): > >-> Produces a failed load, with the following >error message: > >"Error, external lookup of mult: The specified >procedure could not be found." > >Experiment #2: >1. From command line, compile mult.c to mult.dll >using the following command (Note: Must kill Maple >session, as the program has opened "mult.dll" and >this file can not be deleted): > >bcc32 -WD mult.c >-> This produces the file mult.dll which > is 56kb long. >2. Start Maple and attempt to load with > previous command. > >-> Produces a successful load! >3. Now test the DLL: > > > mult(2,3); -> 6 (Correct answer) > >Experiment #3: >1. Copy mult.c to mult.cpp (the original extension used >in my first correspondence). From command line, compile >mult.cpp to mult.dll using the following command: > >bcc32 -WD mult.cpp >-> This produces the file mult.dll which > is 56kb long. >2. Start Maple and attempt to load with > previous command. > >-> Produces a failed load, with the following >error message: > >"Error, external lookup of mult: The specified >procedure could not be found." >--------------------------------------------- >Ed, can you answer the following questions? > >1. Returning to you previous answer, I don't >understand where I would use second bit of >code that you supplied: > >Code: >---------------------------------------------- >#ifndef SOMEDLL_H >#define SOMEDLL_H > >/* handle if this C function is used from C++ */ >#ifdef __cplusplus >extern "C" >#endif > >__declspec(dllimport) int __stdcall mult(int, int); > >#endif >---------------------------------------------- > >2. Can you explain what settings I need to >make in the IDE to get it to compile correctly, >as with the command line experiment #2? > >3. When the code has a cpp extension, does >the compiler change the function names? >This may be why experiment #3 failed. .