ExitProcess(0)

yht8708 2012-03-02 04:19:41
Windows核心编程中,有个例子
class test
{
public:
test(){printf("Constructor\n");}
~test(){printf("Destructor\n");}
};
test t1;
int _tmain(int argc, _TCHAR* argv[])
{
test t2;
ExitProcess(0);
return 0;
}
说上说只会有两个Constructor输出,而不会有Destructor,但我用VS2008测试会有两个Constructor和一个Destructor,而且测试过Destructor应该是局部变量析构的结果。这难道又是编译器不同的结果??
...全文
463 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
Lactoferrin 2012-03-02
  • 打赏
  • 举报
回复
看错了,vc6的动态库也可以
Lactoferrin 2012-03-02
  • 打赏
  • 举报
回复
因为你用的是动态c++运行库,所以test1会析构
换成静态的就没有

而且只有比较新的运行库才有这功能,vc6的就不行
RabbitLBJ 2012-03-02
  • 打赏
  • 举报
回复
class test
{
public:
test(int i):mi(i){printf("Constructor\n");}
~test(){printf("Destructor %d\n",mi);}
private:
int mi;
};
test t1(100);
int _tmain(int argc, _TCHAR* argv[])
{
test t2(200);
ExitProcess(0);
return 0;
}

析构的结果打印100
RabbitLBJ 2012-03-02
  • 打赏
  • 举报
回复
LZ,是全局变量析构的结果,你在类里面加个参数,在析构时候打印就知道了

这是MSDN原文,里面说的很清楚

Remarks
Use the GetExitCodeProcess function to retrieve the process's exit value. Use the GetExitCodeThread function to retrieve a thread's exit value.
Exiting a process causes the following:
All of the threads in the process, except the calling thread, terminate their execution without receiving a DLL_THREAD_DETACH notification.
The states of all of the threads terminated in step 1 become signaled.
The entry-point functions of all loaded dynamic-link libraries (DLLs) are called with DLL_PROCESS_DETACH.
After all attached DLLs have executed any process termination code, the ExitProcess function terminates the current process, including the calling thread.
The state of the calling thread becomes signaled.
All of the object handles opened by the process are closed.
The termination status of the process changes from STILL_ACTIVE to the exit value of the process.
The state of the process object becomes signaled, satisfying any threads that had been waiting for the process to terminate.
If one of the terminated threads in the process holds a lock and the DLL detach code in one of the loaded DLLs attempts to acquire the same lock, then calling ExitProcess results in a deadlock. In contrast, if a process terminates by calling TerminateProcess, the DLLs that the process is attached to are not notified of the process termination. Therefore, if you do not know the state of all threads in your process, it is better to call TerminateProcess than ExitProcess. Note that returning from the main function of an application results in a call to ExitProcess.
Calling ExitProcess in a DLL can lead to unexpected application or system errors. Be sure to call ExitProcess from a DLL only if you know which applications or system components will load the DLL and that it is safe to call ExitProcess in this context.
Exiting a process does not cause child processes to be terminated.
Exiting a process does not necessarily remove the process object from the operating system. A process object is deleted when the last handle to the process is closed.
zhanshen2891 2012-03-02
  • 打赏
  • 举报
回复
是局部变量析构的结果??

那几乎是不可能的。

因为从编译器来讲,它只认识C++语法,不可能识别API函数的

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧