Force connections to a remote drive

  • Thread starter Thread starter vbshanes
  • Start date Start date
V

vbshanes

We use Samba on a SCO Unix box as our file server / backup device. I
have a directory mapped as a Windows drive. I read / write files from
this remote drive with my VB,net application. This arrangement works
99.99% of the time.

That being said, Windows does not automatically connect to the remote
drive at system boot every once in a great while. Some of our
customers have certain Windows boxes that the problem is a bit more
frequent.

The easy work around is to go into Windows Explorer and double click
on the remote drive. Every program in Windows can now see the drive.
That's not very professional.

I want to force my application to do the same sort of connection that
Windows Explorer is doing. Any ideas?

Thanks.
 
This looks more like a SAMBA misconfiguration to me , just a thought is
netbios enabled on the Windows computers ?
as i remember simular issues in the company i previously worked for . it
turned out that the Windows XP machines had standard netbios dissabled
enabling netbios solved these issues .

regards

Michel
 
We use Samba on a SCO Unix box as our file server / backup device. I
have a directory mapped as a Windows drive. I read / write files from
this remote drive with my VB,net application. This arrangement works
99.99% of the time.

That being said, Windows does not automatically connect to the remote
drive at system boot every once in a great while. Some of our
customers have certain Windows boxes that the problem is a bit more
frequent.

The easy work around is to go into Windows Explorer and double click
on the remote drive. Every program in Windows can now see the drive.
That's not very professional.

I want to force my application to do the same sort of connection that
Windows Explorer is doing. Any ideas?

You can try accessing a file or a folder on the drive from code. That might
do whatever magic Windows does when you double-click drive letter. If that
doesn't work then you can look at the WNetAddConnection2 and
WNetCancelConnection2 APIs, which will let you mount and dismount network
files programmatically.
 
What verson of Samba are you using? What verison of SCO Unix?
I had this problem a few years ago, well it was more like Samba on FlexOS,
but the problem was only intermit.
 
Appologies to word wrapping.

There is more code than the bare minimum to get the class to work. I
put an error routine to get the english meaning of the returned error
codes. Also, all of the constants defined by the Microsoft MSDN2 pages
are defined, although I only needed a couple of them.

Enjoy

Imports System.Runtime.InteropServices
Public Class clsConnectDrive
Private Declare Function WNetAddConnection2 Lib "mpr.dll" Alias _
"WNetAddConnection2A" (ByVal netResource As NETRESOURCE, _
ByVal password As [String], ByVal Username As [String], _
ByVal Flag As Integer) As Integer

Private Declare Function WNetCancelConnection2 Lib "mpr.dll" Alias
_
"WNetCancelConnection2A" (ByVal lpName As String, _
ByVal Flag As Integer, ByVal fForce As Integer) As Integer

Private m_strDrive As String
Private m_strNetworkShare As String
Private m_intError As Integer

<StructLayout(LayoutKind.Sequential)> _
Class NETRESOURCE
Public dwScope As Integer
Public dwType As Integer
Public dwDisplayType As Integer
Public dwUsage As Integer
Public LocalName As String
Public RemoteName As String
Public Comment As String
Public Provider As String
End Class

'---------- Network Constants ----------
Private Const NO_ERROR As Integer = 0
Private Const CONNECT_UPDATE_PROFILE As Integer = &H1 '
Disconnect

Private Const RESOURCETYPE_ANY As Integer = &H0
Private Const RESOURCETYPE_DISK As Integer = &H1
Private Const RESOURCETYPE_PRINT As Integer = &H2

Private Const RESOURCE_CONNECTED As Integer = &H1
Private Const RESOURCE_GLOBALNET As Integer = &H2
Private Const RESOURCE_REMEMBERED As Integer = &H3

Private Const RESOURCEDISPLAYTYPE_GENERIC As Integer = &H0
Private Const RESOURCEDISPLAYTYPE_DOMAIN As Integer = &H1
Private Const RESOURCEDISPLAYTYPE_SERVER As Integer = &H2
Private Const RESOURCEDISPLAYTYPE_SHARE As Integer = &H3

Private Const RESOURCEUSAGE_CONNECTABLE As Integer = &H1
Private Const RESOURCEUSAGE_CONTAINER As Integer = &H2

' Error Constants:
Private Const ERROR_ACCESS_DENIED As Integer = 5&
Private Const ERROR_ALREADY_ASSIGNED As Integer = 85&
Private Const ERROR_BAD_DEV_TYPE As Integer = 66&
Private Const ERROR_BAD_DEVICE As Integer = 1200&
Private Const ERROR_BAD_NET_NAME As Integer = 67&
Private Const ERROR_BAD_PROFILE As Integer = 1206&
Private Const ERROR_BAD_PROVIDER As Integer = 1204&
Private Const ERROR_BUSY As Integer = 170&
Private Const ERROR_CANCELLED As Integer = 1223&
Private Const ERROR_CANNOT_OPEN_PROFILE As Integer = 1205&
Private Const ERROR_DEVICE_ALREADY_REMEMBERED As Integer = 1202&
Private Const ERROR_EXTENDED_ERROR As Integer = 1208&
Private Const ERROR_INVALID_PASSWORD As Integer = 86&
Private Const ERROR_NO_NET_OR_BAD_PATH As Integer = 1203&

'---------- Start Code ----------
Public Sub New(ByVal Drive As String, ByVal NetworkShare As
String)
m_strDrive = Drive
If m_strDrive.Length > 1 Then m_strDrive =
m_strDrive.Substring(0, 1)
m_strDrive += ":"
m_strNetworkShare = NetworkShare
End Sub

'---------- Connect a drive ----------
Public Function DriveConnect(Optional ByVal IgnoreAlreadyConnected
As Boolean = True) As Integer
Dim myNetResource As New NETRESOURCE
myNetResource.dwScope = RESOURCE_GLOBALNET
myNetResource.dwType = RESOURCETYPE_DISK
myNetResource.dwDisplayType = RESOURCEDISPLAYTYPE_SHARE
myNetResource.dwUsage = RESOURCEUSAGE_CONNECTABLE
myNetResource.LocalName = m_strDrive
myNetResource.RemoteName = m_strNetworkShare
myNetResource.Provider = Nothing
m_intError = WNetAddConnection2(myNetResource, Nothing,
Nothing, 0)
If m_intError = ERROR_ALREADY_ASSIGNED AndAlso
IgnoreAlreadyConnected Then m_intError = NO_ERROR
Return m_intError
End Function

'---------- Disconnect a drive ----------
Public Function DriveDisconnect() As Integer
m_intError = WNetCancelConnection2(m_strDrive,
CONNECT_UPDATE_PROFILE, 0)
Return m_intError
End Function

'---------- Error Code Description ----------
' Use the last error from either the connect or disconnect
Public Function ErrorCodeMeaning() As String
Dim m_strMessage As String = "*** Undefined ***"
Select Case m_intError
Case NO_ERROR : m_strMessage = ""
Case ERROR_ACCESS_DENIED : m_strMessage = "Access Denied"
Case ERROR_ALREADY_ASSIGNED : m_strMessage = "Already
Assigned"
Case ERROR_BAD_DEV_TYPE : m_strMessage = "Bad Device Type"
Case ERROR_BAD_DEVICE : m_strMessage = "Bad Device"
Case ERROR_BAD_NET_NAME : m_strMessage = "Bad Net Name"
Case ERROR_BAD_PROFILE : m_strMessage = "Bad Profile"
Case ERROR_BAD_PROVIDER : m_strMessage = "Bad Provider"
Case ERROR_BUSY : m_strMessage = "Busy"
Case ERROR_CANCELLED : m_strMessage = "Cancelled"
Case ERROR_CANNOT_OPEN_PROFILE : m_strMessage = "Can Not
Open Profile"
Case ERROR_DEVICE_ALREADY_REMEMBERED : m_strMessage =
"Device Already Remembered"
Case ERROR_EXTENDED_ERROR : m_strMessage = "Extended
Error"
Case ERROR_INVALID_PASSWORD : m_strMessage = "Invalid
Password"
Case ERROR_NO_NET_OR_BAD_PATH : m_strMessage = "No Network
or Bad Path"
Case 53 : m_strMessage = "Can Not Connect - Check Samba
Log"
Case Else : m_strMessage = "*** Undefined ***"
End Select
Return m_strMessage
End Function
End Class
 
Back
Top