WNetAddConnection2 create too many socket in windows server 2008 HPC Edition
I call WNetAddConnection2 many times without WNetCancelConnection2, I just check its return value. This works in windows server 2003, only create开发者_开发问答 one connection, but in windows server 2008, it created too many connection. What's the problem?
EDIT - code as per comment:
TCHAR szLocalName[32] = _T("t:"), szRemoteName[MAX_PATH] = _T("\\\\ws2008_1\\sample_share");
// Assign our values to the NETRESOURCE structure.
nr.dwType = RESOURCETYPE_ANY;
nr.lpLocalName = szLocalName;
nr.lpRemoteName = szRemoteName;
nr.lpProvider = NULL;
// Call the WNetAddConnection2 function to assign
// a drive letter to the share.
dwRetVal = WNetAddConnection2(&nr, 0, 0, FALSE);
Output is:
mount <x:> to <\\ws2008_1\sample_share_2> with :0 PID:8956
mount <x:> to <\\ws2008_1\sample_share_2> with :0 PID:7284
remote name is <\\ws2008_1\sample_share_2> and errCode is: 85 PID:8592
remote name is <\\ws2008_1\sample_share_2> and errCode is: 85 PID:4196
remote name is <\\ws2008_1\sample_share_2> and errCode is: 85 PID:7708
remote name is <\\ws2008_1\sample_share_2> and errCode is: 85 PID:7028
remote name is <\\ws2008_1\sample_share_2> and errCode is: 85 PID:3988
remote name is <\\ws2008_1\sample_share_2> and errCode is: 85 PID:3680
remote name is <\\ws2008_1\sample_share_2> and errCode is: 85 PID:6364
remote name is <\\ws2008_1\sample_share_2> and errCode is: 85 PID:7764
mount <x:> to <\\ws2008_1\sample_share_2> with :0 PID:8764
mount <x:> to <\\ws2008_1\sample_share_2> with :0 PID:4692
mount <x:> to <\\ws2008_1\sample_share_2> with :0 PID:4996
mount <x:> to <\\ws2008_1\sample_share_2> with :0 PID:5300
mount <x:> to <\\ws2008_1\sample_share_2> with :0 PID:6028
Note: the process was created by CreateProcessAsUser, the user name is the same. From the log message, it works sometimes, is it logon session related?
Thanks
Dma
The error messages is NOT session related... it says ERROR_ALREADY_ASSIGNED
(see http://msdn.microsoft.com/en-us/library/ms681382%28v=vs.85%29.aspx).
This means the method you use to mount has been called more than one time in that session (different parts of your application, multiple instances of the application in same session, restarted application in same session...).
Your method to do so is flawed - you need to:
- make sure that within the same session only one process tries a mount for example with a named
Mutex
see http://msdn.microsoft.com/en-us/library/ms682411%28v=vs.85%29.aspx - check before trying to mount if the respective drive letter is already in use see
WNetGetConnection
at http://msdn.microsoft.com/en-us/library/aa385453%28v=VS.85%29.aspx and if it perhaps is mapped to the correct share (return valuelpRemoteName
) - when the session ends/the application ends you need to dismount see
WNetCancelConnection2
at http://msdn.microsoft.com/en-us/library/aa385427%28v=VS.85%29.aspx
There are other points to make sure... but they depend on the answers to the following questions:
- Does the application run within the same session multiple times ?
- Does the application try the mount in different parts of the application ?
- Is this a TerminalServer environment ?
精彩评论