VC open external files

Source: Internet
Author: User

Currently, We know three methods: winexec, ShellExecute, and CreateProcess. Others have already summarized the following methods: winexec, ShellExecute, and CreateProcess for calling methods (functions) of other applications in VC. I will repeat the full text, and add my own summary later (in addition to the title ).

Three SDK functions: winexec, ShellExecute, and CreateProcess can meet the requirements for calling other programs. Among them, winexec is the simplest, ShellExecute is more flexible than winexec, and CreateProcess is the most complex.

Two winexec parameters: the first one specifies the path, and the last one specifies the display mode.

ShellExecute can specify a working directory, and you can also find file associations to directly open applications that do not need to be loaded with the file association. ShellExecute can also open webpages and start relevant email Association sending emails.

CreateProcess has a total of ten parameters, but most of them can be replaced by null. It can specify the process's security attributes, Inheritance Information, class priority, and so on. If we want to get enough information about the new process and control the detailed attributes of the new process, we need to use the CreateProcess function to achieve this purpose.
Syntax of the three SDK functions (winexec, shellexec, and crateprocess:

(1) winexec
This function is the simplest and has only two parameters. The prototype is as follows:
Uint winexec (
Maid, // command path
Uint ucmdshow // Display Mode
);
The usage is as follows:
Winexec ("notepad.exe", sw_show); // open notepad
Winexec ("D: // program files // test // test.exe", sw_showmaximized); // open test.exe in simplified format.
Note that if you use the sw_showmaxmized method to load a program without the maximum button, such as neterm and calc, the normal form will not appear, but it has been added to the task list.

This function can only open the EXE file.

Required header files: Windows. H, WINBASE. H (the former is tested and the latter is described on msdn). In addition, the sequence of the two files cannot be changed.

Description on msdn: [url] http://msdn.microsoft.com/en-us/library/ms687393#/url] (vs.85). aspx

(2) ShellExecute
The prototype is as follows:
Hinstance ShellExecute (
Hwnd, // parent window handle
Lpctstr lpoperation, // operation, opening method "edit", "lead E", "open", "find", "print", "null"
Lptstr lpfile, // file name, which can be added to the path above
Lptstr lpparameters, // Parameter
Lptstr lpdirectory, // Default Folder
Int nshowcmd // Display Mode
);
The usage is as follows:
ShellExecute (null, "open", "C: // test.txt", null, null, sw_shownormal); // open the C:/test.txt File
ShellExecute (null, "open", "[url] http://www.google.com/#/url]", null, null, sw_shownormal); // open the webpage [url] www.google.com [/url]
ShellExecute (null, "Explore", "d: // C ++", null, null, sw_shownormal); // open the Directory D:/C ++
ShellExecute (null, "print", "C: // test.txt", null, null, sw_hide); // print the file C:/test.txt
ShellExecute does not support targeted output.

This function can open any file and calls the program registered by the system to open the file with the corresponding suffix.

Required header files: Windows. H, shellapi. H (the former was tested by me, and the latter was required by msdn ). In addition, the order of the two cannot be changed.

[Url] http://msdn.microsoft.com/en-us/library/bb762153#/url] (vs.85). aspx

(3) CreateProcess
The prototype is as follows:
Bool CreateProcess (
Lptstr lpapplicationname, // name of the execution Program
Lptstr lpcommandline, // Parameter Line
// The following two parameters describe the security attributes of the created process and thread. If it is null, the default security attribute is used.
Lpsecurity_attributes lpprocessattributes, // process security attributes
Lpsecurity_attributes lpthreadattributes, // thread security attributes
Bool binherithandles, // inheritance flag
DWORD dwcreationflags, // create a flag
Lpvoid lpenvironment, // environment variable
Maid directory, // The initial directory where the process is running
Lpstartupinfo, // used to set various attributes when creating a sub-process
Lpprocess_information lpprocessinformation // used to receive relevant information after a process is created.
);
The usage is as follows:
Process_information PI;
Startupinfo Si;
Memset (& Si, 0, sizeof (SI ));
Si. cb = sizeof (SI );
Si. wshowwindow = sw_show;
Si. dwflags = startf_useshowwindow;
Bool fret = CreateProcess ("D: // putty.exe", null, null, false, null, & Si, & PI );

This function can open any file and calls the program registered by the system to open the file with the corresponding suffix.

Required header files: Windows. H, WINBASE. H (the former is tested and the latter is described on msdn). In addition, the sequence of the two files cannot be changed.

Description on msdn: [url] http://msdn.microsoft.com/en-us/library/ms682425424/url] (vs.85). aspx

It can be seen that through the above several different methods, you can achieve the purpose of opening other applications in the application, some of which may be troublesome, therefore, we need to select the method that best suits us for different purposes to achieve our own goals!

Other considerations for the three SDK functions: winexec, ShellExecute, and CreateProcess:

1. Define the header file

Delete the original author's content.

This is something you must pay attention to when referencing new functions. However, the reference sequence of Ms header files is a bit strange, such as the above three cleanup methods. In addition, if pre-compilation is used, remember that the reference before # include "stdafx. h" in any source program will be invalid and will take effect only after it. (It is undeniable that pre-compilation has its own advantages, especially when the program is very large, but any benefit will have to be paid)

2. Define the path

The path in C ++ must use "//" instead of "/", so the preceding three functions indicate that the paths are:
Disk: // directory //... // file name
Winexec ("D: // program files // test // test.exe", sw_showmaximized );
ShellExecute (null, "open", "C: // test.txt", null, null, sw_shownormal );
Bool fret = CreateProcess ("D: // putty.exe", null, null, false, null, & Si, & PI)

3. Pay attention to the file path

When program a calls program B, the default current path of program B is changed to the current path of program. Therefore, be sure to pay attention to it.

You can develop the habit of using absolute paths. In addition, remember to open files and perform other operations to verify whether there are errors.

 

 

 

VC ++ 6.0
Win95
How to call external DOS Programs in VC ++? What is its function? What is the function of an API?

Answer:

You can use Windows API functions winexec and ShellExecute. These two functions can call windows and DOS programs. Winexec mainly runs the EXE file. For example:
Winexec ("notepad.exe readme.txt", sw_show );
ShellExecute can run not only EXE files, but also associated files. For example:
ShellExecute (0, "open", "http://askpro.yeah.net", null, null, 0 );
In addition, refer to qa000583 "calling out a DOS program, but do not want to display the generated window ".

 

Vvc 5.0

Windows 95

I am writing a program and want to call out a ready-made com program, but I don't want to display the generated window. I tried to close the window and run it, but in my program, how do I get the window handle and disable it automatically? (Feifei)

In fact, the window is not displayed when the DOS program is called using ShellExecute, for example:

ShellExecute (0, "open", "C: // tools // arj.exe", "a c: // P. arj c ://*. bat C ://*. sys ", null, sw_hide );

For your problem, you can use findwindow to obtain the handle, but close the window and you will not be able to continue execution. However, you can close the window, probably because it has ended when you close the window.

 

 

 

 

# Include "stdafx. H"
# Include <windows. h>
# Include <shellapi. h> // the library to be referenced by ShellExecute

Int main (INT argc, _ tchar * argv [])
{
ShellExecute (null, null, _ T ("E: // projekt1.exe"), null, null, sw_show );
Getchar ();
Return 0;
}

 

 

 

# Include "stdafx. H"
# Include <stdio. h>
# Include <stdlib. h>
# Include <windows. h>

DWORD winapi threadwork (lpvoid num)
{
Printf ("% d/N", num );
Return 0;
}
Int _ tmain (INT argc, _ tchar * argv [])
{

DWORD dwthreadid [5];

Handle threadhandle = createthread (null,
0,
Threadwork,
(Lpvoid) 2, // passed Parameter
0,
& Dwthreadid [0]);
Getchar ();
Return 0;
}

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/jiangxinyu/archive/2010/03/18/5393037.aspx

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.