Can WinService map to network drive using WNetAddConnection2?
Hello,
I'm coding a windows service that needs to map multiple network drives so they can be accessed later. I'm using WNetAddConnection2 to connect to the network drives with the following NETRESOURCE:
nr = New NETRESOURCE
nr.lpRemoteName = UNCPath
nr.lpLocalName = Nothing
strUsername = Nothing
strPassword = Nothing
nr.dwType = RESOURCETYPE_DISK
The exact same code works if I'm using Win32 Forms (verified using NET USE command to show all connections to my machine). But when I use the code in Windows Service, the WNetAddConnection2 returns the value of 0 which means 'successful operation' and when I check available connection of my machine using NETUSE the connection is not listed. What gives? the Windows service is running under my account and has privilage to the network connection. I'm coding in VB.NET Please point to me what I did wrong. Thanks.
[987 byte] By [
btymer] at [2007-11-11 7:53:17]

# 2 Re: Can WinService map to network drive using WNetAddConnection2?
Hi Phil,
Thanks for the pointer. Correct me if I'm wrong, but the article I think is discussing not to use a mapped letter drive and use UNC instead.
"A service (or any process that is running in a different security context) that must access a remote resource should use the Universal Naming Convention (UNC) name to access the resource. "
I did a UNC path in my code and did not assign any drive letter. Any help is appreciated. Thanks.
btymer at 2007-11-11 21:49:21 >

# 3 Re: Can WinService map to network drive using WNetAddConnection2?
If you're using a UNC path, why do you need WNetAddConnection? You should be able to simply open the desired file.
# 4 Re: Can WinService map to network drive using WNetAddConnection2?
I'm sorry. I should have been more specific in my application. I'm using the FileSystemWatcher to monitor network drives, and therefore I would like to maintain connection to them.
btymer at 2007-11-11 21:51:24 >

# 5 Re: Can WinService map to network drive using WNetAddConnection2?
You might see if any of these helps:
http://www.google.com/search?q=windows+service+filesystemwatcher