In message <Xns9AD4EF8673996ikkehierbe@[EMAIL PROTECTED]
>, Ikke
<ikke@[EMAIL PROTECTED]
> writes
>Hi everybody,
>
>Is it possible to call a .dll from Delphi, and have it call your program
>back, and if so, how would I go about doing something like this?
>
>What I'd like to do is the following:
>- I have a Delphi application which dynamically calls a .dll (consider it
a
>plugin written in another language)
>- the application calls a few functions from the .dll
>So far so good - I know how to accomplish this, although I'm not sure how
>I'd go about setting up an interface for the .dll(s).
>
Try something like this. It is the method I have been using for some
time now. (Delphi 6):
// define the type of function you will call in the DLL
Type
TfuncPStrToBool = function (AStr:Pshortstring):boolean; StdCall;
// now define some variables ...
var
// A variable to store the Windows ID for the loaded DLL
LibHandle : THandle;
// a variable to hold the address of the function you will call.
funcEnterUserName : TfuncPStrToBool;
// a place to enter a user name ...
UName:shortstring;
..... now the code ...
// try to load the DLL (you do this ONLY ONCE in your application)
LibHandle := LoadLibrary('yourlibname.dll');
// check that the DLL is loaded ...
if LibHandle=0 then
Failed; // do whatever you need to do: e.g exit/raise exception.
// load the address of the function you are interested in.
// In this case the function is a TfuncPStrToBool (defined above)
// and it is called "EnterUserName".
@[EMAIL PROTECTED]
:= GetProcAddress(LibHandle, 'EnterUserName');
...Later in your code you can call the EnterUserName function...
if @[EMAIL PROTECTED]
>nil then
if funcEnterUserName(@[EMAIL PROTECTED]
) =FALSE then
....something_went_wrong
else
... all OK, name is in the string UName
Finally you should always unload your DLL...
if LibHandle <> 0 then
FreeLibrary(LibHandle);
>Next it should do the following:
>- at random times (as the .dll sees fit), the .dll should call specific
>functions from my application
>It is this particular function that I'm not sure about.
>
>Does anybody know of a way to accomplish this? Sorry for being so vague
>about it, but as you might have noticed I myself am not very clear as to
>how to do this, thus I'm asking for help :)
>
I don't think you should attempt this, even if it is possible. If your
app were to crash, the DLL would not know, and continue to call the
function which is not now there.
Instead, try calling the DLL periodically, 'pu****ng' any data to the DLL
rather than the DLL 'pulling' its data.
Gray
--
E-mail: Remove X's and underscores from X_grahams_x@[EMAIL PROTECTED]


|