WNetAddConnection2 in a service (repost) 
Author Message
 WNetAddConnection2 in a service (repost)

I'm using the standard WNetAddConnection2 API call in my code.  If I run it
in the load event of a windows form, then everything works fine.  If,
however, I try to use it in the OnStart event of a windows service then the
first time I run it it works fine, but when I restart the service, it fails
with error 85 - ERROR_ALREADY_ASSIGNED.  If I restart the machine then it
again works fine the first time the service runs, but again fails when the
service restarts.

I've tried adding a call to WNetCancelConnection2 before the connect
attempt.  That is failing with error 2250 - ERROR_LOCAL_DRIVE

To keep this post manageable, I haven't included my own code as I don't
think it's necessary.  If someone does want to see it the then let me know
and I can post it.  The basic connection code is available here
http://www.*-*-*.com/

If anyone else has come across this problem in a service, then I'd love to
hear any ideas.

Thanks.

--
For real reply address, lose the cash
www.realuk.co.uk



Tue, 13 Sep 2005 03:13:21 GMT  
 WNetAddConnection2 in a service (repost)
If you make a persistent connection by passing CONNECT_UPDATE_PROFILE then the
connection will be available in the second attempt and thus you will get that
error. You can pass null here if you want to connect manually everytime through
your service.

Hope this helps!
Bharat Patel
Microsoft, Visual Basic .NET and C# .NET

This posting is provided "AS IS" with no warranties, and confers no rights.
Please reply to newsgroups only. Thanks.              



Tue, 13 Sep 2005 04:59:16 GMT  
 WNetAddConnection2 in a service (repost)
I've tried both CONNECT_UPDATE_PROFILE and 0, but I hadn't tried null.  I'll
give it a go tomorrow.  Thanks for the idea.

--
For real reply address, lose the cash
www.realuk.co.uk



Quote:
> If you make a persistent connection by passing CONNECT_UPDATE_PROFILE then
the
> connection will be available in the second attempt and thus you will get
that
> error. You can pass null here if you want to connect manually everytime
through
> your service.

> Hope this helps!
> Bharat Patel
> Microsoft, Visual Basic .NET and C# .NET

> This posting is provided "AS IS" with no warranties, and confers no
rights.
> Please reply to newsgroups only. Thanks.



Tue, 13 Sep 2005 09:00:22 GMT  
 WNetAddConnection2 in a service (repost)
No.  Still not working.  I was originally passing 0 as the flags value for
the connection.  I assume that's what you mean by null (as it's a
non-optional integer).

Any other ideas?

--
Rob Oldfield
www.realuk.co.uk



Quote:
> If you make a persistent connection by passing CONNECT_UPDATE_PROFILE then
the
> connection will be available in the second attempt and thus you will get
that
> error. You can pass null here if you want to connect manually everytime
through
> your service.

> Hope this helps!
> Bharat Patel
> Microsoft, Visual Basic .NET and C# .NET

> This posting is provided "AS IS" with no warranties, and confers no
rights.
> Please reply to newsgroups only. Thanks.



Tue, 13 Sep 2005 19:25:28 GMT  
 WNetAddConnection2 in a service (repost)
Rob,

Right after the call to WNetAddConnection2 API, add an If statement and check
the returncode for ERROR_ALREADY_ASSIGNED. If it is true, just continue
normally.

Hope this helps!
Bharat Patel
Microsoft, Visual Basic .NET

This posting is provided "AS IS" with no warranties, and confers no rights.
Please reply to newsgroups only. Thanks.              



Tue, 13 Sep 2005 23:49:20 GMT  
 WNetAddConnection2 in a service (repost)
No.  That doesn't work either.

I currently have just about everything in try/catch blocks so that I can
track what's going on.  Something like...

try
    makeconnection
end try

try
    write to share via mapped drive
end try

try
    write to share via UNC
end try

...with extra catch lines to enable me to write messages to an event log for
debugging.  When the connection fails with error 85 then the attempt with a
mapped drive comes back with "The filename, directory name, or volume label
syntax is incorrect", the the UNC attempt gives me "Logon failure: unknown
user name or bad password"

--
Rob Oldfield
www.realuk.co.uk



Quote:
> Rob,

> Right after the call to WNetAddConnection2 API, add an If statement and
check
> the returncode for ERROR_ALREADY_ASSIGNED. If it is true, just continue
> normally.

> Hope this helps!
> Bharat Patel
> Microsoft, Visual Basic .NET

> This posting is provided "AS IS" with no warranties, and confers no
rights.
> Please reply to newsgroups only. Thanks.



Wed, 14 Sep 2005 01:11:12 GMT  
 WNetAddConnection2 in a service (repost)
You may want to close the connection when the service is stopped using
WNetCancelConnection2 API.

Hope this helps!
Bharat Patel
Microsoft, Visual Basic .NET

This posting is provided "AS IS" with no warranties, and confers no rights.
Please reply to newsgroups only. Thanks.              



Sat, 17 Sep 2005 07:04:33 GMT  
 WNetAddConnection2 in a service (repost)
Yes.  That does the job.  I'd actually just figured it out when I saw this
post.  I'd tried disconnecting as the first thing in the start code, but
that also failed.  It does actually make sense, when I think about it, to
close down the connection when the service stops - just general good
housekeeping.

Many thanks for the ideas Bharat.

--
Rob Oldfield
www.realuk.co.uk



Quote:
> You may want to close the connection when the service is stopped using
> WNetCancelConnection2 API.

> Hope this helps!
> Bharat Patel
> Microsoft, Visual Basic .NET

> This posting is provided "AS IS" with no warranties, and confers no
rights.
> Please reply to newsgroups only. Thanks.



Sun, 18 Sep 2005 16:24:47 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. WNetAddConnection2 in a service

2. NT Service and WNetAddConnection2 problem

3. Problem w/ nt service and WNetAddConnection2

4. REPOST: Getting address of multiple services

5. Service setup question (repost)

6. Using WMI to obtain dependent services (restarting services and all dependent services)

7. Help: Logging into NT using WNetAddConnection2

8. DCOM: error 1312 on WNetAddConnection2

9. WNetAddConnection2 API Call ????????

10. WNetAddConnection2 & /user:domain\username

11. WNetAddConnection2 Question

12. WNetAddConnection2 Error Codes?

 

 
Powered by phpBB® Forum Software