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;
bussoftwareAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

TheNeilCommented:
Just add the following line AFTER your IF statement:

Form1.BringToFront;

The Neil
0
brunoheCommented:
by the way, you can do this

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

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

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

The Neil
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

MadshiCommented:
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.
0
TheNeilCommented:
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)
0
MadshiCommented:
:-)

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.
0
TheNeilCommented:
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
0
MadshiCommented:
Yep, we do agree there...

Perhaps the people who have beaten you are C++ programmers?   (-:
0
TheNeilCommented:
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
0
MadshiCommented:
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.
0
TheNeilCommented:
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
0
bussoftwareAuthor Commented:
big discussion but nobody wants the points??
0
TheNeilCommented:
GIVE ME THE POINTS (I did give the right answer)

The Neil
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bussoftwareAuthor Commented:
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.
0
TheNeilCommented:
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
0
MadshiCommented:
How about this?

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

Regards, Madshi.
0
bussoftwareAuthor Commented:
Madshi, thank you very much. That's the right solution. How about the points? You and Neil want to share?
0
MadshiCommented:
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...
0
TheNeilCommented:
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
0
MadshiCommented:
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...    (-:
0
TheNeilCommented:
Madshi

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

Mucho biggo thanx

The Neil
0
bussoftwareAuthor Commented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.