Link to home
Start Free TrialLog in
Avatar of adam8
adam8

asked on

Executing an app through delphi

Hi,
In Visual Basic you can use the Shell function
in C++ I suppose you could use the ShellExecute API.

How do I launch a file in Delphi and get it to open with it's associated file.

ie

Open Document1.Doc with MSWord
Background.bmp with Paint Shop Pro

etc.

I would prefer to use native delphi code but I just need to know how to do it at the moment
Avatar of edey
edey

you can also use the shelExecute api in delphi - just add shellApi to your uses clause.  The createProcess/createProcessEx api's are also available.  Heck if you really wanted to you could instantiate a copy of the WSH & use that to expose the shell functionality to call your app :).  At any rate shellExecute should be suffecient.

GL
Mike
here's and example:

var
   path: pchar;
   sr : TSearchRec;
   fileName : string;
begin
     getMem(path,MAX_PATH);
     getWindowsDirectory(path,MAX_PATH);
     findFirst(path+'\*.bmp',faAnyFile,sr);
     caption := path+'\'+sr.name;
     shellExecute(handle,'open',pchar(path+'\'+sr.name),'','',SW_SHOW);
     findClose(sr);
     dispose(path);
end;

it should find the first ocurrence of a .bmp in your windows dir & open it with the defaul app.

LG
Mike
ASKER CERTIFIED SOLUTION
Avatar of Mohammed Nasman
Mohammed Nasman
Flag of Palestine, State of image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Shell execute will do the trick.

If the file's not associated with the program though,

you can use the file in the param list:

ShellExecute(0,'Open','c:\program files\Company\Program\ExeFile.exe','file.ext',nil,SW_SHOWNORMAL);
Avatar of adam8

ASKER

I accepted your answer because the other didn't include

uses ShellApi;
I was wandering why the API didn't seem to register but it does if the declare it.

thanks
Ummm, adam8, I don't mean to comlpain or anything - It's really more important to me thaat we were able to give an appropriate solution, but I would have though that this would have been rather explicit -"just add shellApi to your uses clause" ?

GL
Mike
Now look what you done started!
You went and upset Mikey. Nevermind edey - there'll be
plenty of other questions. Let him keep his points.

:D

Seriously though, Adam, I reckon Mike should've got the
points seeing as he DID respond first - with a working solution at that. But what do I know ? Besides it's just
5 points, right ?
Sorry Mike, I didn't mean to stole the points from you, I just gave him simple sample, cuz he's still beginner with delphi, sorry for that :(
it is absolutely not a problem.  My chief concern here, at EE, it to promote, develop & extend a service I have found usefull.  To me, the points mean squat and are not something i go out of my way to collect.  The reason I try to make (polite) comments where I think mistakes (may) have been made is the same reason I spend so much time commenting on old unanswered questions & old forgotten locked question.  The reason being that for the site to work well we must not only follow the rules, but actively pursue their _spirit_.  This placed has been usefull, not because it's a wealth of information (though a working search engine would improve that) but because there is a _community_ of problem _solvers_.  

In short, I believe that if I want to come here looking for help, or to search the PAQ's for usefull tips then I should respect the community & do what I can to improve it.

<humerous "jest" on>Also, if I had gotten the points I'd bee 200 closer to catching you :)</jest>

GL
Mike
Avatar of adam8

ASKER

Hye,
I gave him the points because without his example I would be wondering while the ShellExecute function wouldn't be working. I have never programmed in Delphi before. you never assume someone knows how to do parts in your code unexplained.

Avatar of adam8

ASKER

"Seriously though, Adam, I reckon Mike should've got the
points seeing as he DID respond first - with a working solution at that. But what do I know ? Besides
it's just"

the solution as far as I was concerned is invalid. Without mnasman's code after it wouldn't have got me any further than I already am at using it.

PS. why do you have to add it when you don't for the other API
Most API functions are declared in the Windows.pas unit.
This unit includes the base Windows APIs, like for GDI, I/O
etc. Other more specialized APIs are in their own special units - like ShellAPI.

Delphi automatically includes the Windows unit in the uses
clause for most new units (eg. for every new form).

The ShellAPI unit isn't used that often, so Delphi doesn't
automatically include it in the uses clause.
you can find codumentation for these functions @ c:\program files\common files\borland shared\mshelp\win32.hlp . The entry there for shellExecute will tell you which c header file you'd have fond it in.  The .pas file is usuall named the same thing.  If you're still having problems finding the .pas definition for an api you know exists, try using the "find in diles" tool (in the delphi ide), search the c:\program files folder for all occurances of the api name (don't forget to search in subfolders) - you should get a long string of returned rsuls, but one of tem will be a function declaration & that file will be the one you need to include in your uses clause.

GL
Mike

<rant>ps, oh and I did tell you _exactly_ what to do.  And If mnasman had presented his solution as an answer, instead of a comment, it would have been considered "poaching".  Now points stopped being an incentive about the time that gold stars in elementry school lost their luster, but do remember that you asked for some help.  I would think, if you had a challenge, in "real life", with some advice you'd get some clarification, rather then ignore it all together</rant> I don't know, just a sugestion.  Have a nice day.