Solved

ShellExecute

Posted on 2002-07-05
6
370 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
Independent Software Vendors: 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

749 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