Solved

ShellExecute

Posted on 2002-07-05
6
373 Views
Last Modified: 2010-04-04
I found this procedure to launch a program and it works fine, but when launching Word I want to open a saved dokument, so how do I write the parameter for the dokument?

procedure TForm1.Button1Click(Sender: TObject);
var startupinfo:Tstartupinfo;
    processinfo:Tprocessinformation;
    command: array [0..512] of Char;
    success:boolean  ;
    begin

strpcopy ( command,trim(edit1.text));
fillchar(startupinfo,sizeof(startupinfo),#0);
startupInfo.wShowWindow:=SW_SHOWNORMAL;
startupinfo.cb:=sizeof(startupinfo);
startupinfo.dwFlags:= STARTF_USESHOWWINDOW;
Success:= CreateProcess(nil,
      command,
      nil,
      nil,
      false,
      CREATE_NEW_CONSOLE or NORMAL_PRIORITY_CLASS,
      nil,
      nil,
      Startupinfo,
      Processinfo);
end;
0
Comment
Question by:rainbowsoftware
[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
  • 3
  • 2
6 Comments
 
LVL 27

Expert Comment

by:kretzschmar
ID: 7132563
your command could be like

'winword drive:\path\docname.suffix'

meikl ;-)
0
 
LVL 22

Expert Comment

by:Mohammed Nasman
ID: 7132581
Hello
 
  CreateProcess will not open document without the defineing the exe , so in edit1 should look like 'c:\office\winword.exe c:\mydoc.doc'

if you would like to open document without define the application name, use ShellExecute

uses
  ShellApi;
procedure TForm1.Button3Click(Sender: TObject);
begin
  ShellExecute(Handle,'open',Pchar(Edit1.Text),nil,nil,SW_ShowNormal);
end;

if u still would like to use CreateProcess, you have to retrieve  the associated application from the windows registry first

Best regards
0
 
LVL 22

Accepted Solution

by:
Mohammed Nasman earned 200 total points
ID: 7132621
Hello again

  There's better way to retrieve the associated application using registry, it's FindExecutable API, I added it to ur code and now it's working fine, also don't forget to Close the handle for ur process and thread

uses ShellApi;
procedure TForm1.Button2Click(Sender: TObject);
var
  AppName: array[0..250] of char;
  startupinfo:Tstartupinfo;
  processinfo:Tprocessinformation;
  command: array [0..250] of Char;
  success:boolean  ;
begin
  FindExecutable(Pchar(Edit1.text),nil,(AppName));
  strpcopy ( command,trim(edit1.text));
  fillchar(startupinfo,sizeof(startupinfo),#0);
  startupInfo.wShowWindow:=SW_SHOWNORMAL;
  startupinfo.cb:=sizeof(startupinfo);
  startupinfo.dwFlags:= STARTF_USESHOWWINDOW;
  Success:= CreateProcess(nil,
     Pchar(AppName +' '+ command),
     nil,
     nil,
     false,
     CREATE_NEW_CONSOLE or NORMAL_PRIORITY_CLASS,
     nil,
     nil,
     Startupinfo,
     Processinfo);
// closing the handle for the process and thread
  CloseHandle(ProcessInfo.hProcess);
  CLoseHandle(ProcessInfo.hThread);
end;

Best regards
Mohammed Nasman
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 27

Expert Comment

by:kretzschmar
ID: 7132650
points are yours, mohammed,
you will then step over the 100k border :-))

meikl ;-)
0
 

Author Comment

by:rainbowsoftware
ID: 7133001
Thanks to Mohammed
This is perfectly working the way I wanted.
Joern
0
 
LVL 22

Expert Comment

by:Mohammed Nasman
ID: 7133025
you are welcome rainbowsoftware

Meikl, too far from 400K :o), wish to see you soon on 500k

0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
delphi parse string to params 3 181
RESTRequest Parameter 4 86
Delphi: barcode reading on android platform 1 102
Twebbrowser add css to the header 3 54
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
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…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

710 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