Solved

2 little questions

Posted on 1998-10-12
13
141 Views
Last Modified: 2010-04-06
How can I retrieve the current path (application.exename) but in the MS-DOS format?

How can I make my program wait until the program executed using winexec () terminates?
0
Comment
Question by:jpcs
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
  • 3
  • +1
13 Comments
 
LVL 20

Expert Comment

by:Madshi
ID: 1342615
jpcs,

(1) Convert "Application.ExeName" with "GetShortPathName".


(2) Call the following function instead of WinExec:

function ExecutePrgOrFile(prgOrFile, params, workingDir: string; var processHandle: cardinal) : boolean;
var ei : TShellExecuteInfo;
begin
  processHandle:=INVALID_HANDLE_VALUE;
  with ei do begin
    cbSize:=SizeOf(ei);
    fMask:=SEE_MASK_FLAG_NO_UI or SEE_MASK_NOCLOSEPROCESS;
    WND:=Application.handle;
    lpVerb:='Open';
    lpFile:=PChar(prgOrFile);
    lpParameters:=PChar(params);
    if workingDir='' then workingDir:=ExtractFilePath(prgOrFile);
    lpDirectory:=PChar(workingDir);
    nShow:=SW_SHOWNORMAL;
  end;
  result:=ShellExecuteEx(@ei);
  if result then processHandle:=ei.hProcess;
end;

Now you can call WaitForSingleObject with the process handle as parameter. Note, that this WaitForSingleObject call does not return, until the process is really finished. That means your program does not handle messages anymore. So perhaps you should think about a little thread...

Regards, Madshi.
0
 
LVL 1

Expert Comment

by:BlackDeath
ID: 1342616
hoi, madshi!
0
 
LVL 20

Expert Comment

by:Madshi
ID: 1342617
Hoi, BlackDeath (wat' soll den "hoi" bloß heißen?)...
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Expert Comment

by:BlackDeath
ID: 1342618
zickezacke...
0
 
LVL 20

Expert Comment

by:Madshi
ID: 1342619
Aha :-)
0
 
LVL 1

Expert Comment

by:BlackDeath
ID: 1342620
oder aber auch t.n.t. >:>=>
0
 
LVL 1

Author Comment

by:jpcs
ID: 1342621
I've tried getshortpathname, but it changes my default path to the old dos format, and, when i call again :

 path := extractfilepath (application.ExeName);

it gives me "invalid pointer operation".

And the second question : I have no idea what a thread is. Please, give me a full code example.


Give me a full example for the first question too (with variables...) and, if it works, I'll give you the points.

0
 
LVL 20

Accepted Solution

by:
Madshi earned 50 total points
ID: 1342623
Here comes my answer...

(1)

function GetShortName(long: string; var short: string) : boolean;
var c1 : cardinal;
begin
  result:=false;
  short:=long;
  if (long='') or (integer(GetFileAttributes(pchar(long)))=-1) then exit;
  c1:=GetShortPathName(PChar(long),nil,0);
  if c1=0 then exit;
  SetLength(short,c1);
  GetShortPathName(PChar(long),PChar(short),c1);
  SetLength(short,c1-1);
  result:=true;
end;

Now you can call "GetShortName(Application.ExeName,strVar)".

(2) A little example:

procedure ExecuteAndWait;
var c1 : cardinal;
begin
  if ExecutePrgOrFile('notepad.exe','','',ph) then begin
    WaitForSingleObject(ph);
    CloseHandle(ph);
  end;
end;

This will work, but it will "block" your application until the other application has finished. If you don't like that you'll have to look at the Delphi "TThread" object yourself. Unfortunately I can't give you a complete source example for the non-blocking-solution, because it would be a little bit work. However, the blocking-solution should fulfil your needs, doesn't it?
You can use "CreateProcess" instead of "ShellExecuteEx" (see EKIM's answer), but "CreateProcess" is not able to run shortcuts or files or ..., while "ShellExecuteEx" is able to do that.

Regards, Madshi.
0
 
LVL 1

Author Comment

by:jpcs
ID: 1342624
I'll accept your answer, but I have to accept madshi comment, so i'll give him 50 points also.

By the way, how can I make the file I'm going to execute invisible?

Thanks.
0
 
LVL 20

Expert Comment

by:Madshi
ID: 1342625
What do you mean with invisible? You could add the "hidden" file attribute to the file (GetFileAttributes, then SetFileAttributes). But that's no perfect solution. Perhaps you could move the file to a temporary path before loading it. But that would be a little bit un-nice, don't you think so?

Regards, Madshi.
0
 
LVL 1

Author Comment

by:jpcs
ID: 1342626
I mean that its a small dos application that terminates in about 1 or 2 seconds, and I don't want to let it be seen. I want it to be invisible. (the winexec has a parameter hide)
0
 
LVL 20

Expert Comment

by:Madshi
ID: 1342627
If you use the "ExecuteProgramOrFile" function, I've given you some comments before, just change the "nShow" value to "SW_HIDE".

Regards, Madshi.
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 6975435
Finalized today by Moondancer - EE Moderator
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

738 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