查看文章
 
【windows编程练习一】使用API函数创建新进程
2010年03月02日 星期二 22:00

点击ok创建一个新的进程(记事本),用的是CreateProcess这个API函数。

作为练习,放在这里备忘,转载注明出处。

//auther:tweety

//time: 2010-3-2

//createpro.cpp

#include <stdio.h>
#include <tchar.h>
#include <windows.h>

int createpro()
{
LPSTR commandline="notepad C:/boot.ini";//启动notepad程序
STARTUPINFO si={sizeof(si)};
PROCESS_INFORMATION pi;
si.dwFlags =STARTF_USESHOWWINDOW;
si.wShowWindow=TRUE;

BOOL ret = ::CreateProcess(
NULL,
commandline,
NULL,
NULL,
FALSE,
CREATE_NEW_CONSOLE,
NULL,
NULL,
&si,
&pi);   
if (ret)
{
::CloseHandle(pi.hThread);//必须在父进程中关闭返回的进程和线程id号
::CloseHandle(pi.hProcess);//否则即便子进程已经终止了,但子进程的进程内核对象和主线程的内核对仍然没有释放。
printf("new process id is : %d,\n",pi.dwProcessId);//在父进程中显示子进程id号
printf("new thread id is :%d,\n",pi.dwThreadId);
}
return 0;

}
int _tmain(int argc, _TCHAR* argv[])
{
int nslect = ::MessageBox(NULL,"hello,windows xp! do you wish to open notepad.exe?","greetings",MB_OKCANCEL);
if (nslect==IDOK)
{
printf("you selected ok,\n");
int newnotpad =createpro();
}
else
printf("you'v selected cancel\n");//end of the program!   
system("pause");
return 0;
}

类别:windows编程||添加到搜藏 |分享到i贴吧|浏览(277)|评论 (0)
 
最近读者:
 
网友评论:
发表评论:
姓 名:
网址或邮箱: (选填)
内 容:
     

   
帮助中心 | 空间客服 | 投诉中心 | 空间协议
©2012 Baidu