Solved

2 little questions

Posted on 1998-10-12
13
142 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

Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

Question has a verified solution.

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

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
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…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses
Course of the Month11 days, 11 hours left to enroll

623 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