#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;
}