Delphi calls external program functions: winexec () and ShellExecute

Source: Internet
Author: User
Tags microsoft outlook

1, winexec ():

Winexec mainly runs EXE files and cannot run other types of files. You do not need to reference special units. Prototype: uint winexec (exepath, showcmd) example. I want to use NotePad to open "C: \ HDC. TXT ", run in Normal Mode: winexec (pchar ('notepad c: \ taoyoyo.txt '), sw_shownormal); parameter description: -- xepath: command line parameter. Note: Use pchar to convert it. -- Showcmd: The running mode of the external program. The value is as follows: ---- sw_hide hides ---- sw_maximize maximized ---- sw_minimize minimizes, And the Z order after this window (that is, the next layer of the window) window activation ---- sw_restore activation window and restore to initialization size sw_show to the current size and status activation window ---- sw_show displays a window with the current size and position, at the same time, it enters the activity status ---- sw_showdefault runs ---- sw_showmaximized activation window by default and maximizes ---- sw_showminimized activation window and minimizes ---- sw_showminnoactive minimizes but does not change the current activation window ---- sw_showna displays the window in the current status but do not change the current activated window ---- sw_shownoactivate to initialize the size of the displayed window but do not change the current activated window ---- sw_shownorm Al is activated and the window is displayed. If the window is the largest (small), the window will be restored. This value should be used when the program is run for the first time. If the call is successful, this function returns a value not less than 31. Otherwise, the call fails and the returned value is as follows: -- 0 system memory or insufficient resources -- error_bad_format. the EXE file format is invalid (for example, it is not a 32-bit application) -- error_file_not_found specifies that the file has a path specified by -- error_path_not_found. The other path is not found: this function is very similar to system () and can only be run. EXE file, which is unsatisfactory in windows. For example, you cannot use this method to open the file by associating it, for example, winexec ("1.html", sw_showna ); you cannot open this document. 2. ShellExecute (): not only can run EXE files, but also the files already associated with the system. This function is similar to the command line "start" in Win9x. It can operate not only files, but also HTTP and mailto. In this way, we can design programs with hyperlink styles. ShellExecute is much more powerful and can completely replace winexec (). In fact, winexec () is also reserved for compatibility with previous versions, so we should try to use ShellExecute (). Shellapi must be referenced first. PAS unit: Uses shellapi; function prototype: function ShellExecute (hwnd: hwnd; operation, filename, parameters, directory: pchar; showcmd: integer): hinst; stdcall; parameter meaning: -- hwnd: Specifies the parent window handle. When an error occurs during a function call, it is used as the parent window of the Windows message window. For example, you can set it to the application Main Window handle, that is, application. Handle, or desktop window handle (obtained using the getdesktopwindow function ). -- Operation: Specifies the operation to be performed. ---- Open: the operation indicates that the program specified by the filename parameter is executed, or the file or folder specified by the filename parameter is opened. ---- print: The operation indicates that the file specified by the filename parameter is printed. ---- explore: the operation indicates browsing the folder specified by the filename parameter. ---- When it is nil, it indicates that the default operation "open" is executed ". -- Filename: Specifies the name of the file to be opened, the name of the program file to be executed, or the name of the folder to be browsed. -- Parameters: If the filename parameter is an executable program, this parameter specifies the command line parameter; 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 the initial display mode of the program window. Otherwise, this parameter should be set to 0. ---- Sw_hide hide ---- sw_maximize maximize ---- sw_minimize is minimized, and the Z order is placed after this window (that is, the next layer of the window) window activation ---- sw_restore activation window and restore to initialization size sw_show to the current size and status activation window ---- sw_show displays a window with the current size and position, at the same time, it enters the activity status ---- sw_showdefault runs ---- sw_showmaximized activation window by default and maximizes ---- sw_showminimized activation window and minimizes ---- sw_showminnoactive minimizes but does not change the current activation window ---- sw_showna displays the window in the current status but do not change the current activated window ---- sw_shownoactivate to initialize the size of the displayed window but do not change the current activated window ---- sw_shownormal activated and The display window. If it is the largest (small), the window will be restored. This value should be used when the program is run for the first time. If the ShellExecute Function is called successfully, the returned value is the instance handle of the program to be executed. If the returned value is smaller than 32, an error occurs. Example: (1). the preceding example is winexec (pchar ('notepad c: \ taoyoyo.txt '), sw_shownormal). You can change it to (assume that the handle of the window is handle) shellExecute (handle, 'open', 'notepad ', 'c: \ taoyoyo.txt', '', sw_shownormal); in addition, it does not need to be so complex, because. the related program of txt is notepad, so you only need to do this: ShellExecute (handle, '', 'c: \ taoyoyo.txt ','', '', sw_shownormal ); (2), example (Delphi): Call C: project1.exe; ShellExecute (handle, 'open', 'c: project1.exe ', 'string content', nil, sw_shownormal); In project1.exe, you can Call: 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 declare this function first (which can be obtained by the API Explorer that comes with VB ): public declare function ShellExecute lib "shell32.dll" alias "shellexecutea" (byval hwnd as long, byval lpoperation as string, byval lpfile as string, byval lpparameters as string, byval lpdirectory as string, B Yval nshowcmd as long) As long then place two labels, set its properties to: Name lbhomepage lbemail caption http://bcbtop.126.com mailto: hugsnow@126.com and change the font to blue with underscores, then add the following code to the click events of the two labels: ShellExecute (Me. hwnd, "", lbhomepage. caption, "", "", sw_shownormal); ShellExecute (Me. hwnd, "", lbemail. caption, "", "", sw_shownormal); 3. Special Use of ShellExecute () If you set the filename parameter to the "http:" protocol format, this function opens the default browser and links to the specified URL address. If multiple browsers are installed on your machine, the function determines which browser to start based on the settings of the HTTP handler (Protocols handler) in Windows 9x/NT Registry. Format 1: http: // website domain name. Example: ShellExecute (handle, 'open', http: //; www.neu.edu.cn ', nil, nil, sw_shownormal); Format 2: http: // website domain name/Webpage file name. Example: ShellExecute (handle, 'open', http: //; www.neu.edu.cn/default.htm', nil,nil, sw_shownormal); if you set the filename parameter to the "mailto:" protocol format, this function starts the default mail client program, such as Microsoft Outlook (including Microsoft Outlook Express) or Netscape Messanger. If multiple email client programs are installed on your machine, this function determines which email client program to start based on the settings of the mailto protocol handler in the Windows 9x/NT Registry. Format 1: mailto: for example: ShellExecute (handle, 'open', 'mailto: ', nil, nil, sw_shownormal); open the new mail window. Format 2: mailto: User Account @ mail server address such as: ShellExecute (handle, 'open', 'mailto: who@mail.neu.edu.cn ', nil, nil, sw_shownormal); open the new mail window, and automatically enter the recipient address. If multiple recipient addresses are specified, the recipient addresses must be separated by semicolons or commas (, the same below ). Format 3: mailto: User Account @ email server address? Subject = mail subject & Body = Mail body such as: ShellExecute (handle, 'open', 'mailto: who@mail.neu.edu.cn? Subject = Hello & Body = This is a test ', nil, nil, sw_shownormal); opens the new mail window and automatically fills in the recipient address, mail subject, and mail body. If the mail body contains multiple lines of text, you must add the line feed escape character % 0a between each line of text. ================== Other articles: This article introduces the powerful API function ShellExecute to compile Zhao Xiangning, the "about" dialog box is required to display the logo, company name, address, and company URL link. After the user clicks the URL link, start the browser to access the Web page. The hot link in this dialog box adds a lot of colors to the application. I tried to obtain the name of the default browser from the associated program of the. htm or. html file in the system registration table. It seems that there is a lot of work to do. In addition, if the browser is enabled, a new instance is always started to replace the existing instance. I want to find an easy way, but I have checked the relevant information and have not obtained any results. Please advise. Solution: if such an API function is good: openthishereurlformenowplease (), the problem will not be solved !! Let me tell you, yes, but it is not the function you think. So far, this useful function has not been mentioned in the relevant documentation. It can open any file on the desktop or Internet URL. The only reference I can find is that I mentioned this function in Microsoft & reg; Access KnowledgeBase. This incredible function is: ShellExecute, replacing winexec. You can give it the name of any file, which can be identified and opened. For example: ShellExecute (null, "open", "iloveu.bmp", null, null, sw_shownormal); open a bitmap file named iloveu.bmp with the map editor of Hangzhou. The default bitmap editor may be Microsoft Paint, Adobe Photoshop, or Corel photopaint. For details about how to use ShellExecute, refer to relevant information. It is important that you know that this function can open any file, or even desktop and URL shortcuts (. ink or. URL ). ShellExecute parses all content in the hkey_classes_root system registry, determines the execution program to be started, and starts a new instance or uses DDE to connect the file name to an open instance. Then, ShellExecute returns the instance handle of the application that opened the file. What's more, ShellExecute can not only open files on the machine, but also open the Internet site address. Example: ShellExecute (null, "open", "http://www.microsoft.com", null, null, sw_shownormal); this Code enables you to access Microsoft's home page. When ShellExecute Encounters "http:" in front of the file name, you can determine that the file to be opened is a Web file, and then start Internet Explorer or Netscape Navigator or any other browser you use to open the file. ShellExecute can also identify other protocols, such as FTP and gopher. Even recognize "mailto", if the file name points to "mailto: zxn@hq.cninfo.net", it starts the e-mail program and opens a new mail to be edited. In short, the ShellExecute Function is so simple to open disk files and Internet files. If you change the second parameter "open" to "print" or "Release E", ShellExecute can print files and open folders. ShellExecute also has an extension function shellexecuteex, which has a special structure and is more powerful. For more information about its usage, see the relevant documentation. Now that you know the secret of ShellExecute, you can easily add a dialog box that links to your about. No. How to load other applications? Three SDK functions are available: winexec, ShellExecute, and CreateProcess. Winexec is the simplest. It has two parameters: Specify the path before and specify the display mode after. the next parameter is worth mentioning. For example, if the mud uses sw_showmaxmized to load a program without the maximum button, that is, neterm, Calc, and so on, no normal form will appear, but it has been added to the task list. ShellExecute is more flexible than winexex. You can specify a working directory. The following example directly opens c: \ temp \ 1.txt, instead of loading the application associated with the TXT file, after many installation programs are completed, a window will be opened to display readme or FAQ. I guess this is the case. shellExecute (null, null, _ T ("1.txt", null, _ T (" C: \ Temp ", sw_showmaxmized) CreateProcess is the most complex, with a total of ten parameters, however, most of them can be replaced by null, which can specify the process's security attributes, Inheritance Information, class priority, and so on. let's take a look at a very simple example: startupinfo stinfo // information of the startup window processinfo procinfo // information of the Process 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

 

(Note: You must retain the copyright information or indicate the source when reprinting .)

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.