Board index » cppbuilder » How to use DLLs

How to use DLLs


2006-01-29 06:43:26 PM
cppbuilder49
Hi,
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).
Paul
//---------------------------------------------------------------------------
#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);
}
 
 

Re:How to use DLLs

Forgot to mention, I'm using BCB6.
"Paul" < XXXX@XXXXX.COM >wrote in message
Quote
Hi,

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).

Paul

 

Re:How to use DLLs

Paul wrote:
Quote
Not sure if this is the right group to ask.
users.deltacomm.com/edmulroy/howto10.htm
Michel
--
----------------------------------------
Michel Leunen
mailto: see my homepage.
C++Builder, BCC5.5.1 Web site:
www.leunen.com/
----------------------------------------
 

{smallsort}

Re:How to use DLLs

Thanks, will give it a go today.
"Michel Leunen" < XXXX@XXXXX.COM >wrote in message
Quote
Paul wrote:

>Not sure if this is the right group to ask.

users.deltacomm.com/edmulroy/howto10.htm

Michel
--
----------------------------------------
Michel Leunen
mailto: see my homepage.
C++Builder, BCC5.5.1 Web site:
www.leunen.com/
----------------------------------------
 

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 <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
 

Re:How to use DLLs

Mark Jacobs < XXXX@XXXXX.COM >wrote on Mon, 30 Jan 2006 12:52:29 +0000 :-
Quote
if (!mjmsghk)
Should read
if (!mydllhandl)
Mark Jacobs
 

Re:How to use DLLs

Thanks. Will read it more in the morning.
"Mark Jacobs" < XXXX@XXXXX.COM >wrote in message
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) :-

 

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" < XXXX@XXXXX.COM >wrote in message
Quote
Paul wrote:

>Not sure if this is the right group to ask.

users.deltacomm.com/edmulroy/howto10.htm

Michel
--
----------------------------------------
Michel Leunen
mailto: see my homepage.
C++Builder, BCC5.5.1 Web site:
www.leunen.com/
----------------------------------------
 

Re:How to use DLLs

Paul wrote:
Quote
I managed to get a basic DLL 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).
Look here. Three articles coming from late bcbdev site that may interest
you:
web.archive.org/web/20031207213941/bcbdev.com/articles/bcbdll.htm
web.archive.org/web/20041009193204/www.bcbdev.com/articles/vcdll.htm
web.archive.org/web/20041013035035/www.bcbdev.com/articles/vcdll2.htm
Michel
--
----------------------------------------
Michel Leunen
mailto: see my homepage.
C++Builder, BCC5.5.1 Web site:
www.leunen.com/
----------------------------------------
 

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:
www.leunen.com/
----------------------------------------
 

Re:How to use DLLs

Thanks.
I have tried the articles and have had some success today with the explicit
DLL access (example 2). I tried the implicit (example 1), but I couldn't
get it to work with VC++ 2005 Express Edition. Someone at work suggested
that the DEF mechanism wasn't really supported by MS anymore, not sure if
this is true or not.
I'm going to persist with the example 2 mechanism to get the initial job
done, but may go back to example 1 at a later stage.
Paul.
"Michel Leunen" < XXXX@XXXXX.COM >wrote in message
Quote
Paul wrote:

>I managed to get a basic DLL 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).

Look here. Three articles coming from late bcbdev site that may interest
you:

web.archive.org/web/20031207213941/bcbdev.com/articles/bcbdll.htm
web.archive.org/web/20041009193204/www.bcbdev.com/articles/vcdll.htm
web.archive.org/web/20041013035035/www.bcbdev.com/articles/vcdll2.htm

Michel
--
----------------------------------------
Michel Leunen
mailto: see my homepage.
C++Builder, BCC5.5.1 Web site:
www.leunen.com/
----------------------------------------
 

Re:How to use DLLs

As per attachments group reply:
I have tried the articles and have had some success today with the explicit
DLL access (example 2). I tried the implicit (example 1), but I couldn't
get it to work with VC++ 2005 Express Edition. Someone at work suggested
that the DEF mechanism wasn't really supported by MS anymore, not sure if
this is true or not.
I'm going to persist with the example 2 mechanism to get the initial job
done, but may go back to example 1 at a later stage.
Thanks
Paul.
<Mark Jacobs>wrote in message news:43de8ec1$ XXXX@XXXXX.COM ...