Solved

ShellExecute

Posted on 2002-07-05
6
375 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

Enroll in July's Course of the Month

July's Course of the Month is now available! Enroll to learn HTML5 and prepare for certification. It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses
Course of the Month10 days, 17 hours left to enroll

632 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