Running Applications from Delphitm: Using Shellexecute (Part 1) {Repost}

xiaoxiao2021-03-06  54

Running Applications from Delphitm: Using Shellexecute (Part 1)

Level: Newbie-Hacker

AUTHOR:

The Baker

Date: 2002-04-19

Running or opening external programs or files, from DelphiTM applications, is often a necessary requirement. This article covers the ShellExecute Windows API function, revealing techniques for running external applications from your Delphi applications.

Running Applications from Delphi

There are a number of reasons why you might be interested in running external applications from your Delphi application Often programs are segmented into a number of logical divisions to represent different components of a larger system Some sub-system components might include..:

Main Application Setup / Installer Application System Application (Specifying System Wide Option) Application Server Application Reporting Application AN External Third Party Application

As Can Be Seen, There a Number of Reasons Why You might NEED to CALL AN External Application. Let's Discuss How We Can do this with the shellexecute command.

Shellexecute WinAPI

. The ShellExecute API allows for the execution (or "running") of external applications It also has a number of other uses, however these all involve performing operations on external files The syntax for the ShellExecute WinAPI function is.:

Hinstance Shellexecute (HWND HWND, LPCTSTR LPOPERATION, LPCTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTSTR LPDIRECTORY, INT NSHOWCMD);

The Following Is A Description of The Parameters for the Shellexecute Function.

Parameter Input / Output Description hwnd input Handle to the parent window:.. Needed for error reporting lpOperation input Pointer to a null-terminated string This is the action to be performed (the "object verb") lpFile input Pointer to a null-terminated string that specifies the file or object to execute the specified verb on. lpParameters input Pointer to a null-terminated string that specifies the parameters to be passed to the application (if the lpFile is an application file). Otherwise, this should be nULL. lpDirectory input Pointer to a null-terminated string that specifies the default directory. nShowCmd input Flag determining how the application window will be displayed when it is opened.The ShellExecute function will return an error code if the function fails. This can be retrieved using the GetLasterror Function. If The Error Code Is Greater Than 32, THE Function WAS SUCCESSFUL. IF ITLESS THAN OR Equal to 32 Then An Error Has Occurred. For a Complete Listing of the Error Codes Refer to the MSDN Website (Link Provided in The Further Reading Section).

The Easiest Way To Interpret The Error Code Is With The SySerrorMessage (GetLasterror) Function Call. This will map the appriate Windows Error Message Returned by the getLastError API Function.

If SHELLEXECUTE (Handle, 'Print', Pchar ('C: /Log.txt'), NIL, NIL, SW_SHOWNORMAL) <= 32 Then ShowMessage (SySerrorMessage (GetLastError);

Operation Type

The ShellExecute function allows for operations to be executed on programs and application objects. In some cases, objects may not be able to respond to an operation type. For example, not all document types support the "print" verb. A list of the commonly supported verbs include: lpOperation Value Result Description edit Opens a document for editing in an editor explore Explores the folder, using Windows Explorer, specified by the lpFile parameter find Opens the Search form initiating a search starting from the directory specified in the lpFile parameter.. Open Opens the file, application or folder specified by the lpfile parameter .print prints The document file specified by lpfile parameter. null more information below, OPFILE

If NULL IS Specified The Different Functionality Will Occur Depending on The Operating System Version. The Following IS An Extract from the MSDN Shellexecute Document:

For systems prior to Microsoft? Windows? 2000, the default verb is used if it is valid and available in the registry. If not, the "open" verb is used.For Windows 2000 and later systems, the default verb is used if available If NOT, The "Open" Verb is used. If neither verb is available, The System Uses The first verb listed in the registry.

If i use the "open" verb on a .pas file, nothing happens. If i use null instead, the .pas file will be opened in delphi. The registry defines the application That is associated with the .pas file.

Show Command

.................... ..

nShowCmd Value Result Description SW_HIDE Hides the window and activates another window. SW_MAXIMIZE Maximizes the specified window. SW_MINIMIZE Minimizes the specified window and activates the next top-level window in the z-order. SW_RESTORE Activates and displays the window. If the window is minimized or maximized, Windows restores it to its original size and position. An application should specify this flag when restoring a minimized window. SW_SHOW Activates the window and displays it in its current size and position. SW_SHOWDEFAULT Sets the show state based on the SW_ flag specified in the STARTUPINFO structure passed to the CreateProcess function by the program that started the application. An application should call ShowWindow with this flag to set the initial show state of its main window. SW_SHOWMAXIMIZED Activates the window and displays it as a maximized window. SW_SHOWMINIMIZED Activates The window and displays it as a minimized window. sw_showminnoactive Displays the window as a minimized window. The active window remains active. SW_SHOWNA Displays the window in its current state. The active window remains active. SW_SHOWNOACTIVATE Activates and displays a window. Windows restores it to its original size and position. The active window remains Active. sw_shownormal activates and displays a window. Windows Restores it to its original size and position.when you call an application for the first time You Should Call The SW_SHOWNORMAL FLAG.

Using shellexecute

.

Run An Application Called Demo.exe in "C: / Apps" DIRECTORY:

Shellexecute (Handle, Nil, Pchar ('C: /Apps/demo.exe'), NIL, NIL, SW_SHOWNORMAL); Open A File (E.G. "Log.txt"):

Shellexecute (Handle, 'Open', PCHAR ('C: /Log.txt'), NIL, NIL, SW_SHOWNORMAL);

Play a music file (E.G. "Ending.mp3"):

Shellexecute (Handle, 'Play', Pchar ('C: /Ending.mp3'), NIL, NIL, SW_SHOWNORMAL);

Print A log file (E.G. "Log.txt"):

Shellexecute (Handle, 'Print', Pchar ('C: /Log.txt'), NIL, NIL, SW_SHOWNORMAL);

Open Up Windows Explorer On C Drive (E.G. "C:"):

Shellexecute (Handle, 'Explore', PCHAR ('C: /'), NIL, NIL, SW_SHOWNORMAL);

Create a new email message with Outlook or Outlook Express:

Procedure CreateEmail (Const EmailAddr, Subject, Body: String); var ConcatEmailStr: String; Begin ConcatEmailStr: = EmailAddr '? Subject =' Subject '& body =' Body; ShellExecute (Handle, 'open', PChar (ConcatEmailStr ), NIL, NIL, SW_SHOWNORMAL); END;

Conclusion

I hope this article has provided you with a greater insight into running applications from Delphi and the ShellExecute WinAPI function. In the next article I will discuss how to wait for an application to finish before resuming application execution.

Further Reading

For Further Information on The Shellexecute WinApi Function Refer to the MSDN Online Library:

Microsoft MSDN - Shellexecute

转载请注明原文地址:https://www.9cbs.com/read-118218.html

New Post(0)