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应该是局部变量析构的结果。这难道又是编译器不同的结果??
...全文
479 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用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函数的
自毁程序是一些电脑高手编写的可执行代码,没有现成的。我给个代码你看一下,如果你能看懂就可以用了。面的代码由Gary Nebbett写就.Gary Nebbett乃是WINDOWS NT/2000 NATIVE API REFERENCE的作者.乃NT系统一等一的高手.下面就分析一些他的这段代码. 这段代码在PROCESS没有结束前就将启动PROCESS的EXE文件删除了. int main(int argc, char *argv[]) { HMODULE module = GetModuleHandle(0); CHAR buf[MAX_PATH]; GetModuleFileName(module, buf, sizeof buf); CloseHandle(HANDLE(4)); __asm { lea eax, buf push 0 push 0 push eax push ExitProcess push module push DeleteFile push UnmapViewOfFile ret } return 0; } 现在,我们先看一下堆栈中的东西 偏移 内容 24 0 20 0 16 offset buf 12 address of ExitProcess 8 module 4 address of DeleteFile 0 address of UnmapViewOfFile 调用RET返回到了UnmapViewOfFile,也就是栈里的偏移0所指的地方.当进入UnmapViewOfFile的流程时,栈里见到的是返回地址DeleteFile和HMODUL module.也就是说调用完毕后返回到了DeleteFile的入口地址.当返回到DeleteFile时,看到了ExitProcess的地址,也就是返回地址.和参数EAX,而EAX则是buffer.buffer存的是EXE的文件名.由GetModuleFileName(module, buf, sizeof buf)返回得到.执行了DeleteFile后,就返回到了ExitProcess的函数入口.并且参数为0而返回地址也是0.0是个非法地址.如果返回到地址0则会出错.而调用ExitProcess则应该不会返回. 这段代码的精妙之处在于: 1.如果有文件的HANDLE打开,文件删除就会失败,所以,CloseHandle(HANDLE(4));是十分巧妙的一手.HANDLE4是OS的硬编码,对应于EXE的IMAGE.在缺省情况下,OS假定没有任何调用会关闭IMAGE SECTION的HANDLE,而现在,该HANDLE被关闭了.删除文件就解除了文件对应的一个句柄. 2.由于UnmapViewOfFile解除了另外一个对应IMAGE的HANDLE,而且解除了IMAGE在内存的映射.所以,后面的任何代码都不可以引用IMAGE映射地址内的任何代码.否则就OS会报错.而现在的代码在UnmapViewOfFile后则刚好没有引用到任何IMAGE内的代码. 3.在ExitProcess之前,EXE文件就被删除了.也就是说,进程尚在,而主线程所在的EXE文件已经没了.(WINNT/9X都保护这些被映射到内存的WIN32 IMAGE不被删除.)

70,022

社区成员

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

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