Solved

Close file,  after being opened with WinExec

Posted on 1997-09-20
6
659 Views
Last Modified: 2010-04-04
I use WinExec to open external programs like arj and rar.
Is there a way to close these applications automaticly when they are finished with there job ?

I mean, i use SW_ShowMinNoActive so the whole proces runs smoothly out of site. But the DOS-box remains open after the event took place. How do i close it ?

Thanx !

TeRReF
0
Comment
Question by:TeRReF
  • 3
  • 3
6 Comments
 
LVL 3

Expert Comment

by:mirek071497
ID: 1345655
It is example from SWAG to run program which can close automatically.


  Function ExecDOS(Path,Params,WorkPath,Title:String; Wait:Boolean; Minimized:Boolean):Boolean;
  {Just for DOS programs, creates a PIF file then executes it, deleting it afterward.
   Path       Full path to the executable
   Params     Parameters
   WorkPath   Default directory, '' if same path as executable
   Title      Title to display at top of window
   Wait       TRUE if execution of current program waits until new program finishes
   Minimized  TRUE if program is to run minimized.}
  var f:file;
      pifpath:string;
      a:string;
      ierr:integer;
    begin
      if WorkPath='' then WorkPath:=extractfilepath(path);

      // this is a generic PIF image that we've hacked to pieces...settings:
      //   Idle sensitivity set lowest
      //   Default window
      //   Exit on terminate
      //   All memory resources used if needed
      //   Allow screen saver
      //   Not dynamic allocation
      a:=#0#120#84#69#83#84#68#79#126#49#32#32#32#32#32#32#32#32#32#32+
      #32#32#32#32#32#32#32#32#32#32#32#32#128#2#0#0#68#58#92#116#101+
      #115#116#100#111#115#112#114#111#103#114#97#109#116#104#105#110+
      #103#46#101#120#101#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0+
      #0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#16#0#101#58#92#116#101#109#112+
      #0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0+
      #0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#45#101#120+
      #32#100#58#92#116#101#115#116#32#100#58#92#42#46#42#0#0#0#0#0+
      #0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0+
      #0#0#0#0#0#0#0#0#0#0#0#1#0#255#25#80#0#0#7#0#0#0#0#0#0#0#0#0#0+
      #0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0+
      #0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0+
      #0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0+
      #0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#77#73+
      #67#82#79#83#79#70#84#32#80#73#70#69#88#0#135#1#0#0#113#1#87#73+
      #78#68#79#87#83#32#51#56#54#32#51#46#48#0#5#2#157#1#104#0#128+
      #2#0#0#100#0#50#0#255#255#0#0#255#255#0#0#2#0#2#0#159#0#0#0#0+
      #0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#45#101#120#32#100#58#92#116#101+
      #115#116#32#100#58#92#42#46#42#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0+
      #0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#87+
      #73#78#68#79#87#83#32#86#77#77#32#52#46#48#0#255#255#27#2#172+
      #1#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0+
      #0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0+
      #0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#80#73#70+
      #77#71#82#46#68#76#76#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0+
      #0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0+
      #0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#2#0#0#0#0#0#0#0#0#0+
      #0#0#0#0#129#0#0#0#0#0#0#0#0#0#0#0#1#0#0#0#5#0#25#0#3#0#200#0+
      #232#3#2#0#10#0#1#0#0#0#0#0#0#0#28#0#0#0#0#0#0#0#8#0#12#0#84#101+
      #114#109#105#110#97#108#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0+
      #0#0#0#0#0#76#117#99#105#100#97#32#67#111#110#115#111#108#101+
      #0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#3#0#0#0#80#0#25#0#128+
      #2#44#1#0#0#0#0#22#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0+
      #0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0+
      #0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0+
      #0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#1#0;
      title:=spaces(title,30);
      move(title[1],a[$02+1],30);

      if length(path)>63 then path:=copy(path,1,63);
      path:=path+#0;
      move(path[1],a[$24+1],length(path));

      if length(params)>63 then params:=copy(params,1,63);
      params:=params+#0;
      move(params[1],a[$a5+1],length(params));
      move(params[1],a[$1c5+1],length(params));

      if length(workpath)>63 then workpath:=copy(workpath,1,63);
      workpath:=workpath+#0;
      move(workpath[1],a[$65+1],length(workpath));

      if minimized
        then a[$1af+1]:=#$12
        else a[$1af+1]:=#$2;

      result:=false; // default unsuccessful
      pifpath:=newfilename(temppath+'00000000.pif',false);
      assignfile(f,pifpath);
      rewrite(f,1);
      ierr:=ioresult;
      blockwrite(f,a[1],length(a),ierr);
      closefile(f);
      if ierr<>length(a) then exit;
      result:=exec(pifpath,'','',wait,0);
      application.processmessages;
      if not wait then delay(1000); // we must wait one second for Windows to read file
      deletefile(pifpath);
    end;

I don't test this but i think so this is working example.
0
 
LVL 29

Author Comment

by:TeRReF
ID: 1345656
Sorry Mirek, but this doesn't seem to work. I'm a newbie so I am not able to get this piece of code operational... I will add another 25 points if you can...
Please add a example to your solution...

THANX !

TeRReF
0
 
LVL 3

Accepted Solution

by:
mirek071497 earned 50 total points
ID: 1345657
This was really not working example, but idea is working. I write this because i don't know Which Delphi You use. This can work on all Delphi 1-3, but in Delphi 2 and 3 You can use CreateProcess. This example is working and i think so this is good for you.

procedure ExecDOS( DosProgram, Params, WorkPath, Title:String; Minimized:Boolean);
var
  PifImage : string;
  f        : file;
  IErr     : integer;
  PgmPath  : string;
  ExecCmd : Array[0..200] of char;
begin
  PifImage:=#0#120#84#69#83#84#68#79#126#49#32#32#32#32#32#32#32#32#32#32+
  #32#32#32#32#32#32#32#32#32#32#32#32#128#2#0#0#68#58#92#116#101+
  #115#116#100#111#115#112#114#111#103#114#97#109#116#104#105#110+
  #103#46#101#120#101#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0+
  #0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#16#0#101#58#92#116#101#109#112+
  #0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0+
  #0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#45#101#120+
  #32#100#58#92#116#101#115#116#32#100#58#92#42#46#42#0#0#0#0#0+
  #0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0+
  #0#0#0#0#0#0#0#0#0#0#0#1#0#255#25#80#0#0#7#0#0#0#0#0#0#0#0#0#0+
  #0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0+
  #0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0+
  #0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0+
  #0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#77#73+
  #67#82#79#83#79#70#84#32#80#73#70#69#88#0#135#1#0#0#113#1#87#73+
  #78#68#79#87#83#32#51#56#54#32#51#46#48#0#5#2#157#1#104#0#128+
  #2#0#0#100#0#50#0#255#255#0#0#255#255#0#0#2#0#2#0#159#0#0#0#0+
  #0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#45#101#120#32#100#58#92#116#101+
  #115#116#32#100#58#92#42#46#42#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0+
  #0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#87+
  #73#78#68#79#87#83#32#86#77#77#32#52#46#48#0#255#255#27#2#172+
  #1#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0+
  #0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0+
  #0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#80#73#70+
  #77#71#82#46#68#76#76#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0+
  #0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0+
  #0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#2#0#0#0#0#0#0#0#0#0+
  #0#0#0#0#129#0#0#0#0#0#0#0#0#0#0#0#1#0#0#0#5#0#25#0#3#0#200#0+
  #232#3#2#0#10#0#1#0#0#0#0#0#0#0#28#0#0#0#0#0#0#0#8#0#12#0#84#101+
  #114#109#105#110#97#108#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0+
  #0#0#0#0#0#76#117#99#105#100#97#32#67#111#110#115#111#108#101+
  #0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#3#0#0#0#80#0#25#0#128+
  #2#44#1#0#0#0#0#22#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0+
  #0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0+
  #0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0+
  #0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#1#0;
  if Minimized then
    PgmPath := 'true'
  else
    PgmPath := 'false';
  PgmPath:=ExtractFilepath(DosProgram);
  if WorkPath='' then WorkPath:=ExtractFilepath(DosProgram);
  if PgmPath='' then PgmPath := WorkPath;

  While length(title)<30 do title:=title+' ';
  move(title[1],PifImage[$02+1],30);

  if length(DosProgram)>63 then DosProgram:=copy(DosProgram,1,63);
  DosProgram:=DosProgram+#0;
  move(DosProgram[1],PifImage[$24+1],length(DosProgram));

  if length(params)>63 then params:=copy(params,1,63);
  params:=params+#0;
  move(params[1],PifImage[$a5+1],length(params));
  move(params[1],PifImage[$1c5+1],length(params));

  if length(workpath)>63 then workpath:=copy(workpath,1,63);
  workpath:=workpath+#0;
  move(workpath[1],PifImage[$65+1],length(workpath));

  if minimized then
    PifImage[$1af+1]:=#$12
  else
    PifImage[$1af+1]:=#$2;

  assignfile(f,PgmPath+ChangeFileExt( ExtractFileName(DosProgram), '.PIF') );
  rewrite(f,1);  ierr:=ioresult;
  blockwrite(f,PifImage[1],length(PifImage),ierr); ierr:=ioresult;
  closefile(f); ierr:=ioresult;
  WinExec( StrPCopy( ExecCmd,DosProgram+' '+Params), SW_SHOW);
end;
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 29

Author Comment

by:TeRReF
ID: 1345658
It works GREAT !

THANX Mirek, i' ve completed my first real application...:-)

Could'nt have done it without you !

TeRReF
0
 
LVL 29

Author Comment

by:TeRReF
ID: 1345659
By the way....

Did you receive your 75 points or just 50 ?
0
 
LVL 3

Expert Comment

by:mirek071497
ID: 1345660
50*4 thanks.
0

Featured Post

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

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
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…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

707 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

16 Experts available now in Live!

Get 1:1 Help Now