Solved

2 little questions

Posted on 1998-10-12
13
138 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
  • 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Windows Drag & Drop Location 2 100
Multiple image collision 13 74
Delphi inherited method 6 68
Not able to call Delphi XE10 dll function from Delphi 6.0 4 26
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
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…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

776 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