Solved

2 little questions

Posted on 1998-10-12
13
136 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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 tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

758 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now