Paul wrote:
Quote
Not sure if this is the right group to ask. But is there an example
somewhere on how to use a DLL in an application? Below is a test DLL. Just
don't know how to link it in and use it in an application (needs to be used
by BCB or VC++ apps).
#include <vcl.h>
#include <windows.h>
#pragma hdrstop
extern "C" __declspec(dllexport) void Evaluate(LPSTR);
#pragma argsused
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fwdreason, LPVOID lpvReserved)
{
return 1;
}
//---------------------------------------------------------------------------
void Evaluate(LPSTR lpName)
{
MessageBox(NULL,TEXT("Borland"), lpName, MB_OK);
}
You could statically link the DLL to your app, but I favour dynamic loading of the DLL
after the app has started. This is how I do it (please note : Timer2 closes the form after
a 300ms delay, and my special type LPFNDLLFUNC1 which defines the DLL's procedure
parameters) :-
typedef bool (CALLBACK* LPFNDLLFUNC1)();
typedef bool (CALLBACK* LPFNDLLFUNC2)(char *,char *,int);
typedef bool (CALLBACK* LPFNDLLFUNC3)(mystruct *);
HINSTANCE mydllhandl;
LPFNDLLFUNC1 mjhkproc;
LPFNDLLFUNC2 mjh2proc;
LPFNDLLFUNC3 mjh3proc;
//---------------------------------------------------------------------------
void __fastcall TForm1::FormPaint(TObject *Sender)
{
static bool clld=false;
if (clld) return;
clld=true; mydllhandl=LoadLibrary("myspecial.dll");
if (!mjmsghk)
{
ShowMessage("Could not load MYSPECIAL.DLL - "+mjgetlasterror());
Timer2->Enabled=true; return;
}
mjhkproc=(LPFNDLLFUNC1)GetProcAddress(mydllhandl,"dll_function1");
if (!mjhkproc)
{
ShowMessage("Error dll_function1 GetProcAddress - "+mjgetlasterror());
Timer2->Enabled=true; return;
}
mjh2proc=(LPFNDLLFUNC2)GetProcAddress(mydllhandl,"dll_function2");
if (!mjh2proc)
{
ShowMessage("Error dll_function2 GetProcAddress - "+mjgetlasterror());
Timer2->Enabled=true; return;
}
mjh3proc=(LPFNDLLFUNC3)GetProcAddress(mydllhandl,"dll_function3");
if (!mjh3proc)
{
ShowMessage("Error dll_function3 GetProcAddress - "+mjgetlasterror());
Timer2->Enabled=true; return;
}
}
//---------------------------------------------------------------------------
AnsiString __fastcall TForm1::mjgetlasterror()
{
LPVOID lpMsgBuf; AnsiString sret;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,GetLastError(),
MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),
(LPTSTR)&lpMsgBuf,0,NULL);
sret=AnsiString((char *)lpMsgBuf); LocalFree(lpMsgBuf);
return sret;
}
//---------------------------------------------------------------------------
// Later on in your code somewhere ...
if (!mjhkproc()) ShowMessage("Oh deary deary me!");
if (!mjh2proc("String Parameter 1","String Parameter 2",4078)) ShowMessage("Whoops!");
//...
// And finally free the library (usually in the form's OnCloseQuery routine) with :-
FreeLibrary(mydllhandl);
--
Mark Jacobs
www.dkcomputing.co.uk