M
monster
Unregistered / Unconfirmed
GUEST, unregistred user!
************************ DWORD RasHangUp(HRASCONN hrasconn); **********************<br>The connection is terminated even if the RasDial call has not yet been completed.<br>After this call, the hrasconn handle can no longer be used. <br>An application should not call RasHangUp and then immediately exit. The connection<br>state machine needs time to properly terminate. If the system prematurely terminates<br>the state machine, the state machine may fail to properly close a port, leaving the<br>port in an inconsistent state. A simple way to avoid this problem is to call <br>Sleep(3000) after returning from RasHangUp; after that pause, the application can <br>exit. A more responsive way to avoid the problem is, after returning from RasHangUp,<br>to call RasGetConnectStatus(hrasconn) and Sleep(0) in a loop until RasGetConnectStatus<br>returns ERROR_INVALID_HANDLE<br>************************ DWORD RasHangUp(HRASCONN hrasconn); **********************<br>照此说明,调用RasHangUp后,程序不应该立即退出,需要给出时间以正常关闭,以免端口状态不一致.<br>最好是在一个循环里反复调用RasGetConnectStatus(hrasconn),直到返回ERROR_INVALID_HANDLE为止.<br>由此,我写出如下代码:<br> //初始化RasConnStatus结构<br> FillChar(RasConnStatus,SizeOf(RasConnStatus),0);<br> RasConnStatus.dwSize:=SizeOf(TRasConnStatus);<br><br> dw := RasHangUp(AHandle);<br> //成功,也不能立即返回,要等待关闭端口<br> if dw = 0 then<br> begin<br> //一直等到出现非法句柄为止<br> while RasGetConnectStatus(AHandle, RasConnStatus) <> ERROR_INVALID_HANDLE do<br> begin<br> Sleep(0);<br> //Application.ProcessMessages;<br> end;<br> end;<br>结果如下:<br> 1.完全连接成功时执行RasHangUp,第一次调用RasGetConnectStatus就得到ERROR_INVALID_HANDLE,<br>一切正常;<br> 2.在连接过程中执行RasHangUp,就陷入了死循环,每次调用RasGetConnectStatus都得到执行时的状<br>态!!!死翘翘了.<br> 3.我若去掉while循环判断,程序随时挂断都没有任何问题出现,难道微软的文档是在自找麻烦?