NetServerEnum 创建不会关闭的工作线程 [英] NetServerEnum create Worker Threads who won't close

查看:56
本文介绍了NetServerEnum 创建不会关闭的工作线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试解决我之前提出的SO 问题时,我发现即使没有我的线程,问题也会发生.

While trying to solve a previously asked SO question of mine, I've find that even without my threads, the problem occurs.

我现在拥有的是一个非常简单的单线程代码,它调用 - NetServerEnum().返回时,它调用 NetApiBufferFree() 并从 main 返回,它应该结束进程.在这一点上,我的线程真正结束,但进程不会退出,因为有 4 个线程打开(不是我):

what I have now , is a really simple single-threaded code , that calls - NetServerEnum() . when returned, it calls NetApiBufferFree() and return from main, which supposed to end the process. at that point, my thread truly ends, but the process won't exit , as there are 4 threads opened (not by me):

1 * ntdll.dll!TplsTimerSet+0x7c0 (堆栈在 ntdll.dll!WaitForMultipleObjects)

1 * ntdll.dll!TplsTimerSet+0x7c0 (stack is at ntdll.dll!WaitForMultipleObjects)

(这个在调用 NetServerEnum() 时打开)

(This one opened upon the call to NetServerEnum())

3 * ndll.dll!RtValidateHeap+0x170(堆栈位于 ntdll.dll!ZwWaitWorkViaWorkerFactory+0xa)

3 * ndll.dll!RtValidateHeap+0x170 (stack is at ntdll.dll!ZwWaitWorkViaWorkerFactory+0xa)

(当我的代码返回时这些是打开的)

(These are open when my code returns)

更新:如果我在外部终止运行 ntdll.dll!TplsTimerSet+0x7c0 的线程(使用进程资源管理器),则在 main() 返回之前,程序会正常退出.我认为了解它可能会有所帮助.

UPDATE: If I kill the thread running ntdll.dll!TplsTimerSet+0x7c0 externally (using process explorer) , before return of main(), the program exit gracefully. I thought it might be useful to know.

UPDATE2:(更多技术信息)我正在使用:MS Visual Studio 2010 Ultimate x64 (SP1Rel) 在 Win7 Enterprise SP1代码是 C(但编译为 C++ 开关打开)子系统:WINDOWS编译器:cl.exe(使用IDE)所有其他参数都是默认值.

UPDATE2: (some more tech info) I'm using: MS Visual Studio 2010 Ultimate x64 (SP1Rel) on Win7 Enterprise SP1 Code is C (but compile as c++ switch is on) Subsystem: WINDOWS Compiler: cl.exe (using IDE) all other parameters are default.

我正在使用自我修改的入口点 (/ENTRY:"entry") ,它是我程序中唯一的函数):

I'm Using a self modified entry point (/ENTRY:"entry") , and it is the only function In my program):

int entry(void)
{
SERVER_INFO_101* si;
DWORD a,b;
NET_API_STATUS c;
c = NetServerEnum ( NULL , 101 , (LPBYTE*) &si , MAX_PREFERRED_LENGTH , &b ,  &a  , SV_TYPE_WORKSTATION, NULL , 0 );

c = NetApiBufferFree (si);

Sleep(1000);

return 0;

}

之前提到的所有测试都是在大约 100 个单元的 Windows 域网络中进行的.

all the tested mentioned before where preformed inside a windows domain network of about 100 units.

更新 3:在(非虚拟)WinXP 32 位 上测试时不会出现此问题.(相同的二进制文件,但对于 Win7 x64 测试了两个二进制文件 - WOW 上的 32 位和原生 x64)

UPDATE 3: This problem does not occur when tested on a (non-virtual) WinXP 32bit. (same binary, though for the Win7 x64 two binary were tested - 32bit over WOW , and native x64)

推荐答案

当您使用自定义入口点时,您绕过了运行时库,这意味着您有责任退出进程.如果没有更多线程在运行,该进程将隐式退出,但正如您所发现的,操作系统可能会代表您创建您无法控制的线程.

When you use a custom entry point, you're bypassing the runtime library, which means you're responsible for exiting the process. The process will exit implicitly if there are no more threads running, but as you've discovered, the operating system may create threads on your behalf that you don't have control over.

在您的情况下,您需要做的就是在 entry() 函数的末尾显式调用 ExitProcess.

In your case, all you need to do is to call ExitProcess explicitly at the end of the entry() function.

int entry(void)
{
   SERVER_INFO_101* si;
   DWORD a,b;
   NET_API_STATUS c;

   c = NetServerEnum ( NULL , 101 , (LPBYTE*) &si , MAX_PREFERRED_LENGTH , &b ,  &a  , SV_TYPE_WORKSTATION, NULL , 0 );

   c = NetApiBufferFree (si);

   Sleep(1000);

   ExitProcess(0);
}

在没有调用 ExitProcess 并使用自定义入口点的情况下,您看到的行为与预期一致.

In the absence of a call to ExitProcess and with a custom entry point, the behaviour you're seeing is as expected.

这篇关于NetServerEnum 创建不会关闭的工作线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆