您的位置:首页 > 运维架构 > Linux

[操作系统]Linux实验三.10 Windows系统下的进程创建

2018-09-10 19:22 676 查看
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Pecony/article/details/82594538

创建并启动一个记事本进程 

[code]#include<iostream>
#include<windows.h>

using namespace std;

int main ()
{
char InChar;
cout << "输入Y将调用内核函数创建新的进程\n";
cin >> InChar;
if (InChar == 'Y'|| InChar == 'y')
{
BOOL IsSuccess;
char FilePath[255]; //存放记事本程序的路径
UINT uSize=255;
GetWindowsDirectory(FilePath, uSize);
//调用API函数GetWindowsDirectory()取得Windows 的系统目录
strcat(FilePath, "\\notepad.exe");  //取得记事本程序的完整路径
PROCESS_INFORMATION NewProcessInformation;
//建立进程信息变量NewProcessInformation
STARTUPINFO StartUpInformation;  //建立进程启动信息变量StarUpInformation
memset(&StartUpInformation, 0, sizeof(StartUpInformation));
//将缓冲区StartUpInformation设置为0
StartUpInformation.cb=sizeof(StartUpInformation);
StartUpInformation.wShowWindow=SW_SHOW;
//新进程的显示方式为正常显示
StartUpInformation.dwFlags=STARTF_USESHOWWINDOW;
IsSuccess=CreateProcess(NULL, FilePath, NULL, NULL, FALSE, CREATE_NEW_CONSOLE|NORMAL_PRIORITY_CLASS,\
NULL, NULL, &StartUpInformation, &NewProcessInformation);
//启动新进程
if (!IsSuccess)
cout<<"不能创建记事本进程\n";
else
cout<<"记事本进程创建成功\n";
}
}

创建并启动一个资源管理器进程

[code]//将"\\noteped.exe"换成"\\explorer.exe"即可

创建应用程序进程 

[code]#include<windows.h>
#include<stdio.h>
#include<cstring>
#include<iostream>

using namespace std;

int main()
{
char InChar;
cout << "输入y创建进程" << endl;
cin >> InChar;

if(InChar == 'Y' || InChar == 'y')
{
bool IsSuccess;
char FilePath[256];
strcpy(FilePath, "C:\\Program Files (x86)\\Lenovo\\PCManager\\WelcomePage.exe");
PROCESS_INFORMATION NewProcessInformation;// 建立进程信息变量NewProcessInformation
STARTUPINFO StartUpInformation;//建立进程启动信息变量StarUpInformation
/* memset()函数原型是extern void *memset(void *buffer, int c, int count)
buffer:为指针或是数组,
c:是赋给buffer的值,
count:是buffer的长度.
这个函数在socket中多用于清空数组.如:原型是memset(buffer, 0, sizeof(buffer))
memset 用来对一段内存空间全部设置为某个字符,一般用在对定义的字符串进行初始化为‘ ’或‘/0’;
*/
memset(&StartUpInformation, 0, sizeof(StartUpInformation));//将缓冲区StartUpInformation设置为0
StartUpInformation.cb=sizeof(StartUpInformation);
//包含STARTUPINFO结构中的字节数.如果Microsoft将来扩展该结构,它可用作版本控制手段,
//应用程序必须将cb初始化为sizeof(STARTUPINFO)。

StartUpInformation.wShowWindow=SW_SHOW;
//SW就是ShowWindow显示窗口的形式
//用于设定如果子应用程序初次调用的ShowWindow
//将SW_SHOWDEFAULT作为nCmdShow参数传递时,该应用程序的第一个重叠窗口应该如何出现。
//SW_SHOW 激活窗口并以其当前的大小和位置显示
//新进程的显示方式为正常显示

StartUpInformation.dwFlags=STARTF_USESHOWWINDOW;//STARTF_USESHOWWINDOW 使用wShowWindow成员

IsSuccess=CreateProcess(NULL, FilePath, NULL, NULL, FALSE, CREATE_NEW_CONSOLE|NORMAL_PRIORITY_CLASS,\
NULL, NULL, &StartUpInformation, &NewProcessInformation);
//启动新进程
if (!IsSuccess)
puts("不能创建word进程\n");
else
puts("应用程序进程创建成功\n");
}
else
cout << "没有输入正确的字符" << endl;
}

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: