Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

2 little questions

Posted on 1998-10-12
13
Medium Priority
?
146 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 200 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

963 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