进程

CreateProcess创建一个标准进程,打开pword程序,PROCESS_INFORMATION接收新进程的相关信息,如果不要新进程的句柄的话,应将该值设为NULL。当创建进程终止时,应该关闭PROCESS_INFORMATION结构体返回的进程和线程句柄,防止内存泄漏。

关闭进程:

a:如果进程是自己创建的,可以使用进程间通信的方法来通知这个进程来结束自己

b:向该进程的主窗口发送一个WM_CLOSE消息

c:使用GetExitCodeProcess和TerminateProcess函数,如果进程仍在运行的话,GetExitCodeProcess将返回STILL_ACTIVE

// MyProcess.cpp : Defines the entry point for the console application.

//

 

#include "stdafx.h"

#include <windows.h>

#include <commctrl.h>

 

 

int _tmain(int argc, _TCHAR* argv[])

{

     BOOL fSuccess;

     PROCESS_INFORMATION pi;

     DWORD dwExitCode;

 

     fSuccess = CreateProcess(TEXT("pword"),NULL,NULL,NULL,FALSE,0,NULL,NULL,NULL,&pi);

     if(fSuccess)  {

         printf("create process success/r/n");

     }    else {

         printf("create process error/r/n");

         goto cleanup;

     }

 

     Sleep(5000);

 

     fSuccess = GetExitCodeProcess(pi.hProcess,&dwExitCode);

     if(STILL_ACTIVE == dwExitCode)   {

         printf("process pword still active/r/n");

     }

    

 

     if(NULL == fSuccess)   {

         printf("GetExitCodeProcess pword error/r/n");

         goto cleanup;

     }    else {

         printf("GetExitCodeProcess pword success/r/n");

     }

    

     fSuccess = TerminateProcess(pi.hProcess,dwExitCode);

     if(NULL == fSuccess)   {

         printf("TerminateProcess pword error/r/n");

         goto cleanup;

     }    else {

         printf("TerminateProcess pword success/r/n");

     }

 

cleanup:

     CloseHandle(pi.hThread);

     CloseHandle(pi.hProcess);

 

     return 0;

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值