?
Solved

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

Posted on 2000-01-06
22
Medium Priority
?
155 Views
Last Modified: 2010-04-06
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;
0
Comment
Question by:bussoftware
  • 10
  • 7
  • 4
  • +1
22 Comments
 
LVL 5

Expert Comment

by:TheNeil
ID: 2329178
Just add the following line AFTER your IF statement:

Form1.BringToFront;

The Neil
0
 

Expert Comment

by:brunohe
ID: 2329223
by the way, you can do this

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

if s1[Length(s1)] <> '\' then
 ///
0
 
LVL 5

Expert Comment

by:TheNeil
ID: 2329292
brunhoe

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

The Neil
0
[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

 
LVL 20

Expert Comment

by:Madshi
ID: 2329380
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
 
LVL 5

Expert Comment

by:TheNeil
ID: 2329434
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
 
LVL 20

Expert Comment

by:Madshi
ID: 2329471
:-)

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

Expert Comment

by:TheNeil
ID: 2329495
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
 
LVL 20

Expert Comment

by:Madshi
ID: 2329513
Yep, we do agree there...

Perhaps the people who have beaten you are C++ programmers?   (-:
0
 
LVL 5

Expert Comment

by:TheNeil
ID: 2329539
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
 
LVL 20

Expert Comment

by:Madshi
ID: 2329653
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
 
LVL 5

Expert Comment

by:TheNeil
ID: 2329689
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
 

Author Comment

by:bussoftware
ID: 2331385
big discussion but nobody wants the points??
0
 
LVL 5

Accepted Solution

by:
TheNeil earned 600 total points
ID: 2331417
GIVE ME THE POINTS (I did give the right answer)

The Neil
0
 

Author Comment

by:bussoftware
ID: 2331450
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
 
LVL 5

Expert Comment

by:TheNeil
ID: 2331498
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
 
LVL 20

Expert Comment

by:Madshi
ID: 2331644
How about this?

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

Regards, Madshi.
0
 

Author Comment

by:bussoftware
ID: 2332266
Madshi, thank you very much. That's the right solution. How about the points? You and Neil want to share?
0
 
LVL 20

Expert Comment

by:Madshi
ID: 2332365
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
 
LVL 5

Expert Comment

by:TheNeil
ID: 2332418
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
 
LVL 20

Expert Comment

by:Madshi
ID: 2332462
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
 
LVL 5

Expert Comment

by:TheNeil
ID: 2333173
Madshi

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

Mucho biggo thanx

The Neil
0
 

Author Comment

by:bussoftware
ID: 2337430
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

Featured Post

Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Planning to migrate your EDB file(s) to a new or an existing Outlook PST file? This video will guide you how to convert EDB file(s) to PST. Besides this, it also describes, how one can easily search any item(s) from multiple folders or mailboxes…
Hi, this video explains a free download that you can incorporate into your Access databases, or use stand-alone for contact management. Contacts -- Names, Addresses, Phone Numbers, eMail Addresses, Websites, Lists, Projects, Notes, Attachments…
Suggested Courses

589 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question