BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Re: How to use DLLs

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (IDE)
View previous topic :: View next topic  
Author Message
Paul
Guest





PostPosted: Mon Jan 30, 2006 9:12 am    Post subject: Re: How to use DLLs Reply with quote



Thanks, will give it a go today.


"Michel Leunen" <nospam (AT) noreply (DOT) please> wrote

Quote:
Paul wrote:

Not sure if this is the right group to ask.

http://users.deltacomm.com/edmulroy/howto10.htm

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



Back to top
Mark Jacobs
Guest





PostPosted: Mon Jan 30, 2006 12:52 pm    Post subject: Re: How to use DLLs Reply with quote



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





PostPosted: Mon Jan 30, 2006 9:10 pm    Post subject: Re: How to use DLLs Reply with quote




Mark Jacobs <markj (AT) critical (DOT) co.uk> wrote on Mon, 30 Jan 2006 12:52:29 +0000 :-
Quote:
if (!mjmsghk)

Should read
if (!mydllhandl)

Mark Jacobs

Back to top
Paul
Guest





PostPosted: Mon Jan 30, 2006 10:50 pm    Post subject: Re: How to use DLLs Reply with quote

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





PostPosted: Thu Feb 02, 2006 10:19 pm    Post subject: Re: How to use DLLs Reply with quote

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

Quote:
Paul wrote:

Not sure if this is the right group to ask.

http://users.deltacomm.com/edmulroy/howto10.htm

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



Back to top
Michel Leunen
Guest





PostPosted: Fri Feb 03, 2006 6:55 pm    Post subject: Re: How to use DLLs Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (IDE) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.