Delphi calls external program functions: WinExec () and ShellExecute detailed

Source: Internet
Author: User
Tags one mail microsoft outlook

1,winexec ():

  WinExec mainly runs EXE files and cannot run other types of files. No special units are referenced.   Prototype: UINT winexec (exepath,showcmd)   example, I want to open "C:\HDC with Notepad. TXT ", run in Normal mode: WinExec (PChar (' Notepad c:\taoyoyo.txt '), SW_SHOWNORMAL);   parameter description:--xepath: command-line arguments.  Note that you need to convert it with Pchar. --showcmd: How the external program runs. The values are as follows: ----sw_hide hidden  ----sw_maximize maximize  ----sw_minimize minimized, and the z order is activated in the window after this window (that is, the next level of the window)  - ---sw_restore activates the window and reverts to the initialization size sw_show the current size and state activates the window  ----sw_show displays a window with the current size and position, and moves it to the active state  ----SW_ Showdefault runs----sw_showmaximized activates the window in the default mode and maximizes  ----sw_showminimized activates the window and minimizes  ----sw_showminnoactive Minimizes but does not change the currently active window  ----sw_showna Displays the window in its current state without changing the currently active window  ----sw_shownoactivate to initialize the size of the window but does not change the currently active window  --- -sw_shownormal activates and displays the window, and if it is maximum (small), the window will be restored. You should use this value for the first time you run the program    if the call succeeds, the function returns a value not less than 31, otherwise the call fails with the meaning of the return value as follows:--0 system memory or insufficient resources  --error_bad_format. EXE file format is invalid (such as not 32-bit application)  --error_file_not_found specified file with found--error_path_not_found specified path not found   others: this function and system () verySimilarly, only run. EXE file, so that it is not satisfactory in windows, such as the method can not be used to open the file through the associated method, such as WinExec ("1.html", Sw_showna), you cannot open this document.    2,shellexecute ():  can run not only exe files, but also files that are already associated with the system. This function is similar to command-line command "Start" in Win9x, which can not only manipulate files, but also manipulate HTTP, mailto, and so on.  So we can design a hyperlink-style program. ShellExecute is much more powerful and can completely replace winexec (), in fact, WinExec () is also reserved for compatibility with previous versions, so we try to use ShellExecute () as much as possible.   must first refer to the Shellapi.pas unit: Uses shellapi;   function prototype: Functions ShellExecute (Hwnd:hwnd; Operation, FileName, Parameters,directory:pchar; Showcmd:integer): HINST; The stdcall;   parameter means the following:  --hwnd: Used to specify the parent window handle. When an error occurs in the function call procedure, it acts as the parent window of the Windows message window. For example, you can set it to the application main window handle, which is application.handle, or you can set it as the desktop window handle (obtained with the GetDesktopWindow function).   --operation: Used to specify the action to be made. ----Open: Operation means executing the program specified by the filename parameter, or opening the file or folder specified by the filename parameter;----Print: Operation means printing the file specified by the filename parameter;----  Explore: Operation means browsing the folder specified by the filename parameter. When----is nil, the default action "open" is executed.  --filename: Lets you specify the file name to open, the name of the program file to execute, or the folder name to browse.  --parameters: If the filename parameter is an executable program, this parameter specifies a command-line argument, otherwise this parameter should be nil or pchar (0).  --DIRECTory: Used to specify the default directory.  --showcmd: If the filename parameter is an executable program, this parameter specifies how the program window is initially displayed, otherwise this parameter should be set to 0.  ----Sw_hide Hide  ----maximize  ----sw_minimize minimize and activate the Z Order window after this window (that is, the next layer of the window)  ----sw_ The restore activates the window and reverts to the initialization size sw_show the current size and state activates the window  ----sw_show displays a window with the current size and position, while it enters the active state  ----Sw_showdefault Run the----sw_showmaximized by default to activate the window and maximize  ----sw_showminimized activate the window and minimize  ----sw_showminnoactive Minimizes but does not change the currently active window  ----sw_showna Displays the window in its current state without changing the currently active window  ----sw_shownoactivate to initialize the size of the window but does not change the currently active window  --- -sw_shownormal activates and displays the window, and if it is maximum (small), the window will be restored. You should use this value for the first time you run the program    if the ShellExecute function call succeeds, the return value is the instance handle of the executing program. If the return value is less than 32, it indicates an error occurred.    example:  (1), just for example: WinExec (PChar (' Notepad c:\taoyoyo.txt '), SW_SHOWNORMAL);  can be changed to ( Suppose the handle of the window is Handle)   ShellExecute (Handle, ' open ', ' notepad ', ' c:\taoyoyo.txt ', ', SW_SHOWNORMAL);   in addition, It doesn't have to be that complicated, because. The TXT affiliate program is Notepad, so just do it: ShellExecute (Handle, ', ' c:\taoyoyo.txt ', ', ', ' Sw_shownormal ');   (2), Example (Delphi):  in an application called c:project1.exe;  ShellExecute (handle, ' open ', ' c:project1.exe ', ' string contents ', Nil, sw_ Shownormal);   in Project1.exe can be called:  procedure tform1.formcreate (sender:tobject);  var i:integer ;  begin  for I:=1 to ParamCount do  if Paramstr (i) 〈〉 "then ShowMessage (Paramstr (i));  end;    (3), in VB, we first declare this function (can be obtained by VB's own API browser):  public Declare function ShellExecute Lib "Shell32.dll" Alias "S Hellexecutea "(ByVal hwnd as Long, ByVal lpoperation As String, ByVal lpfile as String, ByVal lpparameters as String, Byva L lpdirectory as String, ByVal nShowCmd as Long) as long   then place two labels, set their properties to:  Name lbhomepage lbemail C Aption http://bcbtop.126.com mailto:[email protected]   and change the font to blue with an underscore, and then add the following code to the click event of the two label:    ShellExecute (Me.hwnd, "", Lbhomepage.caption, "", "", SW_SHOWNORMAL);  ShellExecute (Me.hwnd, "", Lbemail.caption, "", "", SW_SHOWNORMAL);   3,shellexecutE () Special usage    if the filename parameter is set to the "http:" protocol format, the function opens the default browser and links to the specified URL address. If multiple browsers are installed in the user's machine, the function determines which browser to start based on the settings of the HTTP protocol handler (Protocols Handler) in the Windows 9x/nt registry.    format one:/HTTP website domain name.   such as: ShellExecute (handle, ' open ', http://, www.neu.edu.cn ', nil, nil, SW_SHOWNORMAL);   Format II.//website domain name/ The page file name.    such as: ShellExecute (handle, ' open ', HTTP//; Www.neu.edu.cn/default.htm ', Nil,nil, SW_SHOWNORMAL);   If the filename parameter is set to the "mailto:" protocol format, the function launches the default mail client, such as Microsoft Outlook (also including Microsoft Outlook Express) or Netscape Messanger. If more than one mail client is installed in the user's machine, the function determines which mail client to start based on the settings of the Mailto protocol handler in the Windows 9x/nt registry.    format a:mailto:  such as: ShellExecute (handle, ' open ', ' mailto: ', nil, nil, SW_SHOWNORMAL); Open the new mail window.    Format II: mailto: User account @ Mail server address   such as: ShellExecute (handle, ' open ', ' mailto:[email protected] ', nil, nil, SW_SHOWNORMAL); Opens a new mail window and automatically fills in the recipient's address. If you specify multiple recipient addresses, the recipient addresses must be separated by semicolons or commas (hereinafter).    format three: mailto: User account @ Mail server address? subject= message subject &body= message body   such as: ShellExecute (handLe, ' open ', ' mailto:[email protected]?subject=hello&body=this is a test ', nil, nil, SW_SHOWNORMAL); Open a new mail window, and automatically fills in the recipient's address, message subject, and message body. If the message body includes multiple lines of text, you must add a newline escape character%0a between each line of text.       =============== Other Articles:  introduces a powerful API function shellexecute   compiling Zhao Ning     Issue: I use MFC to write an app that asks for a logo, company name, address, and Company URL link in the About dialog box. After the user clicks on the URL link, the browser is launched to access the Web page, and the hot links in this dialog box add a lot to the application. The way I figured it out was to get the name of the default browser from the system registry by looking up the. htm or. html file's associated program, as if there was a lot of work to do. In addition, if the browser is already open, a new instance is always started to replace the one that is already present. Want to find an easy way, but I checked the relevant information, no harvest, please expert advice.    Workaround: If you have such an API function that much better: Openthishereurlformenowplease (), the problem is not solved!! I tell you, yes, but it's not the function you think it is. So far, there is no mention of a useful function in the documentation that can open any file on the desktop or open the Internet URL. The only reference I can find is in microsoft®.  In Access Knowledgebase, this function has been mentioned very vaguely. This incredible function is: ShellExecute, instead of winexec. You can give it any file name, it can be recognized and open it.  For example:  ShellExecute (NULL, "open", "iloveu.bmp", Null,null,sw_shownormal);  Open a bitmap file called Iloveu.bmp with the default bitmap editor, which may be Microsoft Paint, Adobe Photoshop, or Corel photopaint.   hasDetailed usage information about ShellExecute you can refer to the relevant information yourself, it is important that you know this function to open any file, even desktop and URL shortcuts (. ink or. url). ShellExecute parses all the contents of the system registry HKEY_CLASSES_ROOT, determines which one executes the program, launches a new instance, or uses DDE to connect the file name to an open instance. Then, ShellExecute returns an instance handle of the app that opened the file.   What's even more amazing is that ShellExecute not only can open files on the machine, but also open the web address of the Internet. For example:  ShellExecute (null, "open", "http://www.microsoft.com", NULL, NULL, SW_SHOWNORMAL);   This code allows you to access Microsoft's homepage. When ShellExecute encounters the "http:" in front of the file name, you can tell that the file you want to open is a Web file that starts with Internet Explorer or Netscape Navigator or any other browser you use to open the file. ShellExecute can also identify other protocols, such as FTP, GOPHER. Even identify "mailto", if the file name points to "mailto:[email protected", it starts the e-mail program and opens a new message to be edited. In summary, the ShellExecute function simply opens the disk file and the Internet file. If you change the second parameter "open" to "print" or "EXPLORE", ShellExecute will be able to print the file and open the folder. ShellExecute also has an extension function ShellExecuteEx, with a special structure in the parameters, more powerful, and its usage is described in the documentation.   Now that you know the secret of ShellExecute, it's easy to add a link to your about dialog box with it. Isn't it.   How do I load other applications?    Three SDK functions winexec, shellexecute,createprocess can be used. WinExec the simplest, two parameters, the previous specified path, and the latter specifies the display mode. The latter parameter is worth saying, such as mud with sw_showmaxmized way to load a non-maximized button program, is Neterm,calc and so on, will not appear normal form, butis already added to the task list. ShellExecute more flexible than Winexex, you can specify the working directory, the following example is to open the c:\temp\1.txt directly, instead of loading the TXT file associated with the application, many of the setup program will open a window to display the Readme  Or FAQs, I guess that's what it's all about. .  ShellExecute (null,null,_t ("1.txt", null,_t ("C:\\Temp", sw_showmaxmized)   CreateProcess is the most complex, a total of 10 parameters, but most of them can be replaced with NULL, it can specify the security properties of the process, inherit information, class priority, and so on. Let's see a very simple example: . Startupinfo Stinfo// The information for the Startup window ProcessInfo ProcInfo//Process information CreateProcess (null,_t ("notepad.exe", Null,null. FALSE, Normal_priority_ class,null,null, &stinfo,&procinfo)


--This article comes from [TTT blog]:http://www.taoyoyo.net/ttt/post/433.html

Delphi calls external program functions: WinExec () and ShellExecute detailed

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.