Link to home
Start Free TrialLog in
Avatar of bussoftware
bussoftware

asked on

Calling a dll-function and my program isn't acitve anymore

I wrote an own dll, which uses SelectDirectory and exports it. If I use this dll (i.e. in a delphi program) the BrowseForFolder-dialog appears, but afterwards the calling program isn't active anymore. You may use this code:

library Testdll;

uses FileCtrl;

{$R *.RES}
function SelDir (const Text: PChar): PChar; stdcall;
var     s1 : string;
begin
    Result := '';
    if SelectDirectory (Text, '', s1) then begin
        if copy (s1, length (s1), 1) <> '\' then s1 := s1 + '\';
        Result := PChar(s1);
    end;
end;

exports SelDir;

begin
end.

Call the dll with:

procedure TForm1.Button1Click(Sender: TObject);
var     s1: string;
begin
        s1 := SelDir ('Please Select Directory');
        if s1 <> '' then Label1.Caption := s1;
end;
Avatar of TheNeil
TheNeil

Just add the following line AFTER your IF statement:

Form1.BringToFront;

The Neil
by the way, you can do this

if copy (s1, length (s1), 1) <> '\' then
       
easier:

if s1[Length(s1)] <> '\' then
 ///
brunhoe

That's a dangerous thing to do. What happens if s1 is of zero length?

The Neil
Hi, brunohe, and another new expert, welcome to the team...   :-)

Well, I agree with brunohe, it's really not so important, but it would be faster to do this:

  if (s1 <> '') and (s1[Length(s1)] <> '\') then

Because in the "Copy" solution Delphi has to allocate (and later deallocate) a new string. Okay, only a small difference...   :-)

Ehm... TheNeil is right, but I would better leave the "Form1." away. Then it works with multiple instances of the form, too.

Regards, Madshi.
Sorry Madshi but I disagree with you. You're making an assumption about how Delphi evaluates the IF. If the compiler checks the first condition then and only evaluates the next condition if that succeeds then fine BUT if the compiler evaluates ALL the conditions individually first then you're in trouble. I'd do it in a similar way to you Madshi but have two nested IF statements - inefficient I know, but just that little bit more robust.

I know I'm splitting hairs but I was beaten over the head repeatedly for doing things like that.

The Neil (The picky bloke)
:-)

Well, then beat back who ever has beaten you! Because it is *guaranteed* that Delphi evaluates the parameters one by one and stops as soon as a false parameter is found!
Except when you change the default options for the compiler switch {$B}. Please look in the help about {$B}.

If you look into the VCL sources you'll see that the VCL trusts in this logic the same way as I do.

I don't think you're splitting hairs. Because if you were right I had to change about 200000 lines of quite perfectly working code...   :-)

Regards, Madshi.
Ok. Just being maybe a little too over cautious. We both agree that to check the string without first checking it's length is a dangerous thing to do though

The Neil
Yep, we do agree there...

Perhaps the people who have beaten you are C++ programmers?   (-:
Do I detect some evil thoughts towards C++ programmers? If so, can I join in (displaying total xenophobic attitude towards anything but Delphi)?

The Neil
Yes, you did detect right and yes, you may join in... Hehe, "xenophobic attitude"... I had to ask my oneline translator for that word...   :-))

Regards, Madshi.
Oops, I do like the long words - makes me at least SOUND intelligent. I have to say, Microsoft's Visual C++ is THE WORST development environment I've ever used and thank God I've made sure that it's firmly in the bin - I like my visual languages to be actually visual.

By the way, to get a really good smile, try using 'D' eg =:D

The Neil
Avatar of bussoftware

ASKER

big discussion but nobody wants the points??
ASKER CERTIFIED SOLUTION
Avatar of TheNeil
TheNeil

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I tested it with delphi and it works. The program I need this dll for, doesn't have the command "bringtofront". Is it possible to evaluate the handle or anything else within the dll from the calling program? If yes I could activate (or bring to front) my program from within the dll.
What language are you trying to use the DLL with then?

Anyway, what you could do is force the calling application to pass it's handle as a parameter. Hunt around inside the VCL source code (Forms.pas) and find the code for BringToFront. This shows how to activate your form using the Windows API call SetForegroundWindow.

All you have to do is pass the supplied window handle and it SHOULD work. I haven't tested this and I have no idea what will happen if you activate a window and exit (which is what you're DLL is going to do). I HOPE that it all works hunky dory

The Neil
How about this?

var wnd : dword;
begin
  wnd := GetForegroundWindow;
  SelectDirectory(...);
  SetForegroundWindow(wnd);
end;

Regards, Madshi.
Madshi, thank you very much. That's the right solution. How about the points? You and Neil want to share?
Hehe, that's up to you...   :-)

Sharing points is quite difficult, you would have to ask the customer service to delete this question, then you would have to post 2 new questions, one for Neil and one for me...
What about if you rehect my 'proposed' answer (sorry but there were points on the go), give Madshi the points and then he gives me half of them by posting another question? (I'll trust him)

The Neil
Thanx for the trust!

But I don't like the idea that much - because your own points have much more worth than the points you get for answering questions. You can't buy anything with the points you got, you can't use them for posting own questions, either. They're just a kind of hiscore. So I better want to keep my points for all the difficult questions I have to ask...    :-)

bussoftware, if the split story is too much work for you, you have my official agreement for accepting Neil's answer and ignoring my help...    (-:
Madshi

Sure? You don't mind letting me have the lot? Honour indeed

Mucho biggo thanx

The Neil
Madshi and Neil

I think you are so excellent experts that there will be a chance for Neil to "help" Madshi.

Thanks to both of you for your help. Have a nice day.