WNetCancelConnection2 - How to Force Disconnect?

M

Mack

Have tried varying declares for the api call, i need to close a
connection even if resources are open (damn perfcounters)
It suggests that you can use a true (dim as a bool in the api
declare), this doesn't work, it always tells me the
getlasterrormessage of
'This network connection has files open or requests pending.'

I've tried using 1,-1 (after dimming as a long) as values, but they
still fail also

TIA

Cheers

Mack

GtErrorMessage(WNet.WNetCancelConnection2("\\192.168.1.2",0, 0))

Class WNet
<StructLayout(LayoutKind.Sequential)> _
Public Class NETRESOURCE
Public dwScope As Integer
Public dwType As Integer
Public dwDisplayType As Integer
Public dwUsage As Integer
Public lpLocalName As String
Public lpRemoteName As String
Public lpComment As String
Public lpProvider As String
End Class

Public Declare Ansi Function WNetAddConnection2 Lib "mpr.dll"
Alias "WNetAddConnection2A" (ByVal lpNetResource As NETRESOURCE, ByVal
lpPassword As String, ByVal lpUserName As String, ByVal dwFlags As
Integer) As Integer
Public Declare Ansi Function WNetCancelConnection2 Lib "mpr.dll"
Alias "WNetCancelConnection2A" (ByVal lpName As String, ByVal dwFlags
As Long, ByVal fForce As Long) As Integer

End Class
 
M

Mattias Sjögren

Mack,

A correct declare should look like this

Public Declare Auto Function WNetCancelConnection2 Lib "mpr.dll"
(ByVal lpName As String, ByVal dwFlags As Integer, ByVal fForce As
Boolean) As Integer



Mattias
 
M

Mack

I've tried that also, using true/false in the code, but it doesn't
make the slightest bit off difference, what exact value is the API
expecting? is it -1 ? does .net handle it's true/false equivalents
different to the API ?
 
M

Mattias Sjögren

I've tried that also, using true/false in the code, but it doesn't
make the slightest bit off difference

In that case I don't think the problem is in your VB.NET code, but
perhaps some quirk with the API. Have you gotten it to work in some
other programming language? Have you tried it on multiple versions of
Windows?

what exact value is the API expecting? is it -1 ?

The value of Win32's TRUE is 1, but functions typically just check for
a non-zero value.

does .net handle it's true/false equivalents different to the API ?

Yes



Mattias
 
M

Mack

If i use the declaration as
Public Declare Auto Function WNetCancelConnection2 Lib "mpr.dll"
(ByVal lpName As String, ByVal dwFlags As Integer, ByVal fForce As
Boolean) As Integer

then i get an error message
This network connection does not exist.

I guess this is due to the Ansi charset being used by the api?
if i use

Public Declare Ansi Function WNetCancelConnection2 Lib "mpr.dll"
(ByVal lpName As String, ByVal dwFlags As Integer, ByVal fForce As
Boolean) As Integer

then i get the error
This network connection has files open or requests pending.

If I try changing what i am disconnecting, then it does throw the
above error, so it would suggest that using the Auto in the declare is
incorrect, since it would not appear to understand the string being
passed.

I've tried changing the boolean to an integer and passing 1, but that
fails also.


Cheers

Mack
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top