Your first DLL in C++Builder
You have complete source code for the dll project and the testapplication in the download file, so I will show only the interesting part of it here (I'm using C++Builder 6.0).
We will make 2 projects, a DLL and a test application which will call a function in the DLL. If you make everything from scratch, do it like this (or download the source and play with it):
Select File->New->Other->DLL Wizard, and just press OK-button without any other setup. The Wizard will produce a DLL skeleton for you, and the only thing you have to do is to manually enter the code for a small testfunction.
You shall end up with something like:
#pragma argsused
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
{
return 1;
}
//------------------------------------------------------------------------
//------------------------------------------------------------------------
extern "C"
{
char *str= "Hello from DLL";
char* __declspec(dllexport) TestDLLInterface(void)
{
return str;
}
}
You will have to enter the code for "extern "C" ... etc. The functionname is, as you see, TestDLLInterface(void) and it will return a pointer to the string "Hello from DLL". Compile and link everything.
If you read Harold Howe's article, you'll see that this is the same as his "UnknownFunction".
Now, the test application, a simple form with one button and a Text Label. The code will something like:
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//-----------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
extern "C"
{
char * __declspec(dllimport) TestDLLInterface(void);
}
//-----------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//-----------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
try
{
Label1->Caption = (String)(TestDLLInterface());
}
catch (...)
{
};
}
//-----------------------------------------------------------------------
Before you compile and link this application, you should copy the output from the DLL project to this project. The output we are interested in are two files, xxx.dll and xxx.lib (or whatever you called it). The DLL and the LIB file should be copied to the catalog where your exe is generated, and the xxx.lib should be added to this project. Now, do the compile and link.
Run the application, press the button, and you will se the text in the Text Label.
Download project (nothing yet, too simple, isn't it?).
- grandpa -