How to close connection properly with WNetCancelConnection2? How to close connection properly with WNetCancelConnection2? windows windows

How to close connection properly with WNetCancelConnection2?


I met similar issues. And I waited 60 seconds in the code and then the network connection is really closed. Paulik is right, the credential is cached somewhere which made my next connection with a different credential fail. However, if I close with WNetCancelConnection2(pathname, flag, false) and wait for 60 seconds, my new credential will work okay. Don't know the reason but it just works.


I faced the same issues, and unfortunately, didn't manage to find appropriate solution. I can only describe my research result.

This method uses 'net use /delete' command under the hood. It works fine and deletes network connection. You can check that by calling 'net use' in command window. The connection is closed, but, credentials are still cached somewhere.

So the main reason of this issue is cached credentials. You can try to find them in Credential Manager calling 'control keymgr.dll' in Run window, but in my case they were not displayed. Credentials might be cached by explorer.exe, so you can try to open Task Manager and restart it.

The only reliable way to get rid of them is to restart Workstation Service using 'net stop' 'net start' commands.