Question:
I would like to rename the DllEntryPoint function to DllMain, but the
VCL produces an error message every time I try. Is this possible?
Answer:
Normally, there is no need to change the name of this function, however, if you are attempting to use a DLL which uses a different naming convention for the DLL entry point function, you can use a .DEF file to export the desired name instead of the default. For example, in C++Builder, the name of the function is "DllEntryPoint," but in older Borland compilers (and VC) , the name of this function is "DllMain."
The VCL requires this function to be named as such (& will prevent you from saving/closing your application), but you can workaround this by adding "#define DllEntryPoint" (shown below), which merely adds the name to the symbol table. So, an example of a DLL.CPP file using "DllMain" as the naming convention instead of "DllEntryPoint" might look like the following:
//---------------------------------------------------------------------------
// Just EXPORTS DllMain=DllEntryPoint
USEDEF("MyDll.def");
#define DllEntryPoint
//---------------------------------------------------------------------------
int WINAPI DllMain(HINSTANCE hinst, unsigned long
reason, void*)
{
MessageBox(0,"OK?", "It Works",0);
return 1;
}
//---------------------------------------------------------------------------
// To prove to yourself that the DllMain is being
exported correctly,
// go to a command prompt, navigate to where your
project is,
// and type the following:
// tdump -ee MyDll.dll
//---------------------------------------------------------------------------
Now, it is necessary to create the .DEF file. Here is an example of what it should look like:
//---------------------------------------------------------------------------
LIBRARY MYDLL.DLL
EXPORTS
DllMain=DllEntryPoint
//---------------------------------------------------------------------------
That's basically it. If you wish to see it in action, I have provided
source code below that uses the DLL. Just drop a button on your form
and copy the following into the Unit.cpp of your application:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "MainUnit.h"
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
void __fastcall TForm1::Button1Click(TObject *Sender)
{
HINSTANCE h = LoadLibrary("MyDll.dll");
FreeLibrary("MyDll.dll");
}
//---------------------------------------------------------------------------
Last Modified: 07-JUL-00