 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Paul Guest
|
Posted: Mon Jan 30, 2006 9:12 am Post subject: Re: How to use DLLs |
|
|
Thanks, will give it a go today.
"Michel Leunen" <nospam (AT) noreply (DOT) please> wrote
|
|
| Back to top |
|
 |
Mark Jacobs Guest
|
Posted: Mon Jan 30, 2006 12:52 pm Post subject: Re: How to use DLLs |
|
|
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
#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
http://www.dkcomputing.co.uk
|
|
| Back to top |
|
 |
Mark Jacobs Guest
|
Posted: Mon Jan 30, 2006 9:10 pm Post subject: Re: How to use DLLs |
|
|
Mark Jacobs <markj (AT) critical (DOT) co.uk> wrote on Mon, 30 Jan 2006 12:52:29 +0000 :-
Should read
if (!mydllhandl)
Mark Jacobs
|
|
| Back to top |
|
 |
Paul Guest
|
Posted: Mon Jan 30, 2006 10:50 pm Post subject: Re: How to use DLLs |
|
|
Thanks. Will read it more in the morning.
"Mark Jacobs" <markj (AT) critical (DOT) co.uk> wrote
| Quote: | 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) :-
|
|
|
| Back to top |
|
 |
Paul Guest
|
Posted: Thu Feb 02, 2006 10:19 pm Post subject: Re: How to use DLLs |
|
|
I managed to get a basic DL working in BCB, and test it in a BCB application
(BCB6).
But my fellow engineers haven't been able to get it to work in VC++ (Visual
Studio 6).
I'm going to put the project zip in the borland.public.attachments under the
same subject. Can someone tell me if there is anything wrong with the
project, code, or options, as to why it doesn't work. Last error that
popped up was apparently a problem with ESP being incorrect, and possilbe
calling conventiones were wrong???
The purpose of the DLL is to wrap up some useful VCL components from BCB and
use them in VC++.
Paul.
"Michel Leunen" <nospam (AT) noreply (DOT) please> wrote
|
|
| Back to top |
|
 |
Michel Leunen Guest
|
Posted: Fri Feb 03, 2006 6:55 pm Post subject: Re: How to use DLLs |
|
|
Paul wrote:
[snip]
I replied in the attachment group.
--
----------------------------------------
Michel Leunen
mailto: see my homepage.
C++Builder, BCC5.5.1 Web site:
http://www.leunen.com/
----------------------------------------
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|