Beginner VC Road: First task, write dialog box--method to invoke other applications

Source: Internet
Author: User
For the next one weeks? ldquo: Find code--Copy code--Failed to run, and then "Find code--Copy code--run failed" in the vicious circle, in addition to the first day to set up the dialog box and the button frame, every day only a little bit of progress on a daily basis.

The first day to find a variety of loading pictures, the simple 10 lines of code are tried again, failure;

The next day to specialize in dialog box picture loading method, but really do not understand the code inside the load class variables, define member variables, and call the function parameters are what meaning, how to call, had to give up;

The third day feel like a chicken, helpless to guide the person JJ for help. JJ said let me first implement the basic button call program function, and then consider how to load the picture. Search the Internet for a while, found this is also relatively easy, there are three SDK functions can be invoked, respectively:

WinExec, shellexecute,createprocess

One of the most simple winexec, ShellExecute more flexible than winexec, CreateProcess the most complex.

The winexec has two parameters, the previous specified path, and the latter specifies the display mode.

ShellExecute can specify a working directory, and you can also find the file's association directly open without loading the application associated with the file, ShellExecute can also open the Web page, start the corresponding mail association to send messages, and so on.

CreateProcess has 10 parameters, but most can be replaced with NULL, which can specify the security attributes of the process, the inheritance information, the priority of the class, and so on. If we want to get enough information about the new process and control the detail attributes of the new process, we need to use the CreateProcess function to achieve these goals.

The specific usage is as follows:

WinExec
This function is the simplest, with only two parameters, and the prototype is as follows:


UINT WinExec (


LPCSTR lpcmdline,//command path

UINT ucmdshow//display, there are 11 kinds, you can check the MSDN ShowWindow functions

);


Use the following methods:

WinExec ("Notepad.exe", sw_show); Open Notepad


WinExec ("D://program files//test//test.exe", sw_showmaximized); Open Test.exe in a maximized manner (note that the file name must be the same case)


Note that if you use the Sw_showmaxmized method to load a program without a maximized button, such as Calc, the normal form will not appear, but it has been added to the task list.



ShellExecute

The prototype is as follows:

HInstance ShellExecute (


HWND hwnd,//parent window Handle

LPCTSTR lpoperation,//operation, open mode "edit", "Explore", "open", "find", "print", "NULL"

LPCTSTR lpfile,//file name, front plus path

LPCTSTR lpparameters,//Parameters

LPCTSTR lpdirectory,//default folder

INT nshowcmd//display mode

);


Use the following methods:

ShellExecute (NULL, "open", "C://test.txt", null,null,sw_shownormal); Open C:/test.txt File


ShellExecute (NULL, "open", "http://www.google.com", NULL, NULL, SW_SHOWNORMAL); Open Web www.google.com


ShellExecute (NULL, "Explore", "d://c++", null,null,sw_shownormal); Open Directory D:/c++


ShellExecute (NULL, "print", "C://test.txt", Null,null, Sw_hide); Print File C:/test.txt


Note: ShellExecute does not support directed output.


CreateProcess

The prototype is as follows:

BOOL CreateProcess (


LPCTSTR Lpapplicationname,//Executive program name

LPTSTR lpcommandline,//Parameter row

The following two parameters describe the security properties of the processes and threads that are created and, if NULL, use the default security properties

Lpsecurity_attributes lpprocessattributes,//process security ATTRIBUTES

Lpsecurity_attributes lpthreadattributes,//thread security ATTRIBUTES

BOOL binherithandles,//Inheritance flag

DWORD dwcreationflags,//Create flag

LPVOID lpenvironment,//environment variables

LPCTSTR Lpcurrentdirectory,//running the process's initial directory

Lpstartupinfo Lpstartupinfo,//For setting various properties when creating a child process

Lpprocess_information lpprocessinformation//For receiving information after a process is created

);


Use the following methods:
Process_information Pi; Information for starting a window
Startupinfo si; Information about the process
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,NULL,NULL,NULL,&SI,&PI);



As a result, I designed three buttons, using these three functions to call three applications, set in the onclick message for each button:

WinExec ("Taskmgr", sw_normal); Calling System Task Manager

ShellExecute (NULL, "open", "C:/readme.txt", "" "," ", sw_show); Open the Readme file under C disk

Startupinfo si={sizeof (SI)};
Process_information Pi;
CreateProcess (NULL, "cleanmgr", NULL,NULL,FALSE,NORMAL_PRIORITY_CLASS,NULL,NULL,&SI,&PI); Calling the system's cleanup disk program


After compiling and running through, my dialog box finally has a bit of use, you can click the button to execute some applications.

It's been said from the Internet that there are some caveats to using these three functions:

1. Definition header File


The following two header files must be defined in the header file stdafx.h:

#include <shlobj.h>//Can be replaced by windows.h
#include <shellapi.h>
If you define a header file #include <windows.h> do not need to define #include <shlobj.h>, "windows.h" not only contains "shellapi.h", it also defines a number of data types, Without these data types, the shellapi.h itself will be faulted.

2. Define the path


The paths represented in C + + are "//" instead of the usual "/", so the above three functions represent paths:

Disk://directory//...//file Name

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.