唯一一个不强制设定退出码的函数——ExitThread

#include "stdafx.h"

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

DWORD WINAPI ThreadFunc(LPVOID);
class CSomeclass
{
public:
	CSomeclass()
	{
		printf("CSomeclass()\n");
	}
	~CSomeclass()
	{
		printf("~CSomeclass()\n");
	}
};
CSomeclass g_obj;

int main()
{
	CSomeclass l_obj;

    HANDLE hThrd;
    DWORD exitCode = 0;
    DWORD threadId;
    
    hThrd = CreateThread(NULL,
        0,
        ThreadFunc,
        (LPVOID)1,
        0,
        &threadId );
    if (hThrd)
        printf("Thread launched\n");

    printf("About to exit Main thread,return 1\n");
	//return 1;
    //exit(1);
	ExitThread(1);//唯一一个不强制设定退出码的函数——ExitThread
	//ExitProcess(1);
	

    for(;;)
    {
        BOOL rc;
        rc = GetExitCodeThread(hThrd, &exitCode);
        if (rc && exitCode != STILL_ACTIVE )
            break;
    }

    CloseHandle(hThrd);

    printf("Thread returned %d\n", exitCode);

    return EXIT_SUCCESS;
}


/*
 * Call a function to do something that terminates
 * the thread with ExitThread instead of returning.
 */
DWORD WINAPI ThreadFunc(LPVOID n)
{
    printf("Thread running\n");
    printf("About to exit thread,return 5\n");
    ExitThread(5);
    printf("Under this will never execute!\n");

    return 0;
}

///

#include <stdio.h>
#include <windows.h>
int main(int argc, char *argv[])
{
	char szCommandLine[] = "E:\\Files\\DiskE\\Book\\vc\\Win32多线程程序设计(随书源码)\\Exerc\\ExitThread\\ExitProcess\\Debug\\ExitProcess.exe";
	STARTUPINFO si = { sizeof(si) };
	PROCESS_INFORMATION pi;
	si.dwFlags = STARTF_USESHOWWINDOW; // 指定wShowWindow成员有效
	si.wShowWindow = TRUE; // 此成员设为TRUE的话则显示新建进程的主窗口
	BOOL bRet = CreateProcess (
	NULL, // 不在此指定可执行文件的文件名
	szCommandLine, // 命令行参数
	NULL, // 默认进程安全性
	NULL, // 默认进程安全性
	FALSE, // 指定当前进程内句柄不可以被子进程继承
	CREATE_NEW_CONSOLE, // 为新进程创建一个新的控制台窗口
	NULL, // 使用本进程的环境变量
	NULL, // 使用本进程的驱动器和目录
	&si,
	&pi) ;
	if(bRet)
	{

		printf("回车获得进程的退出码\n");
		getchar();
		DWORD dwExitCode;
		GetExitCodeProcess(pi.hProcess,&dwExitCode);
		printf("%d\n",dwExitCode);

		// 不使用的句柄最好关掉
		CloseHandle(pi.hThread);
		CloseHandle(pi.hProcess);
		printf("新进程的ID号:%d\n",pi.dwProcessId);
		printf("新进程的主线程ID号:%d\n",pi.dwThreadId);
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值