• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 817
  • Last Modified:

Net Connection using mpr.dll

Using this function WNetAddConnection to connect to a
PC , i 'm facing difficulty,. How should be the path name like which is an argument in the above function?
can i pass userid also , if so how. ?
pls help me .
thanx josh
0
joshprakash
Asked:
joshprakash
  • 3
  • 2
1 Solution
 
BarcaCommented:
it is from rpetruni

This is sample code...
procedure Something;
var
  net_resource : TNetresource;
  pass, Drive, user, Server_name : array [0..100] of char;
begin    
        Server_Name:='\\server\somethingShared';
         Drive:='f:'//Maped drive
         net_resource.dwType:=RESOURCETYPE_DISK;
         net_resource.lpLocalName:=Drive;
         net_resource.lpRemoteName:=Server_name;
         net_resource.lpProvider:=nil;
         net_resource.lpComment:='Anything';
         if WNetAddConnection2(net_resource, pass, user, CONNECT_UPDATE_PROFILE);
end;
0
 
joshprakashAuthor Commented:
hi barca,
i feel its in vc
i ll put my code in vb, can i do the same wat u do above here.
if so wat reference should be added in vb.
thanx

Declare Function WNetAddConnection Lib "mpr.dll" Alias _
"WNetAddConnectionA" (ByVal lpszNetPath As String, _
ByVal lpszPassword As String, ByVal lpszLocalName _
As String) As Long
Declare Function WNetCancelConnection Lib "mpr.dll" _
Alias "WNetCancelConnectionA" (ByVal lpszName _
As String, ByVal bForce As Long) As Long

Const WN_SUCCESS = 0 ' The function was successful.
Const WN_NET_ERROR = 2 ' An error occurred on the network.
Const WN_BAD_PASSWORD = 6 ' The password was invalid.

Option Explicit



Function AddConnection(MyShareName As String, _
MyPWD As String, UseLetter As String) As Integer

End Function

Function CancelConnection(DriveLetter As String, _
Force As Integer) As Integer

On Local Error GoTo CancelConnection_Err
CancelConnection = WNetCancelConnection(DriveLetter, _
Force)
CancelConnection_End:

Exit Function

CancelConnection_Err:
CancelConnection = Err
MsgBox Error$

Resume CancelConnection_End

End Function


End Function
End Sub

Private Sub Command2_Click()
Dim a As Integer
a = AddConnection("\\10.1.3.51\share", "sanil5", "H")
MsgBox (a)
End Sub
here while i run this i get msg as succes.
but i cannot see the mapping in windows explorer


0
 
manchandaCommented:
sample code form my code:
This does not work on local machine

Option Compare Database
Option Explicit

'This module is used for allowing shared access of resource then stoping share
 'added private before Declare & Type
      Private Declare Function WNetAddConnection2 Lib "mpr.dll" Alias _
      "WNetAddConnection2A" (lpNetResource As NETRESOURCE, _
      ByVal lpPassword As String, ByVal lpUserName As String, _
      ByVal dwFlags As Long) As Long
      Private Declare Function WNetCancelConnection2 Lib "mpr.dll" Alias _
      "WNetCancelConnection2A" (ByVal lpName As String, _
      ByVal dwFlags As Long, ByVal fForce As Long) As Long
      Private Type NETRESOURCE
        dwScope As Long
        dwType As Long
        dwDisplayType As Long
        dwUsage As Long
        lpLocalName As String
        lpRemoteName As String
        lpComment As String
        lpProvider As String
      End Type
      'Orriginally all these private were public
      Private Const NO_ERROR = 0
      Private Const CONNECT_UPDATE_PROFILE = &H1
      ' The following includes all the constants defined for NETRESOURCE,
      ' not just the ones used in this example.
      Private Const RESOURCETYPE_DISK = &H1
      Private Const RESOURCETYPE_PRINT = &H2
      Private Const RESOURCETYPE_ANY = &H0
      Private Const RESOURCE_CONNECTED = &H1
      Private Const RESOURCE_REMEMBERED = &H3
      Private Const RESOURCE_GLOBALNET = &H2
      Private Const RESOURCEDISPLAYTYPE_DOMAIN = &H1
      Private Const RESOURCEDISPLAYTYPE_GENERIC = &H0
      Private Const RESOURCEDISPLAYTYPE_SERVER = &H2
      Private Const RESOURCEDISPLAYTYPE_SHARE = &H3
      Private Const RESOURCEUSAGE_CONNECTABLE = &H1
      Private Const RESOURCEUSAGE_CONTAINER = &H2
      ' Error Constants:
      Private Const ERROR_ACCESS_DENIED = 5&
      Private Const ERROR_ALREADY_ASSIGNED = 85&
      Private Const ERROR_BAD_DEV_TYPE = 66&
      Private Const ERROR_BAD_DEVICE = 1200&
      Private Const ERROR_BAD_NET_NAME = 67&
      Private Const ERROR_BAD_PROFILE = 1206&
      Private Const ERROR_BAD_PROVIDER = 1204&
      Private Const ERROR_BUSY = 170&
      Private Const ERROR_CANCELLED = 1223&
      Private Const ERROR_CANNOT_OPEN_PROFILE = 1205&
      Private Const ERROR_DEVICE_ALREADY_REMEMBERED = 1202&
      Private Const ERROR_EXTENDED_ERROR = 1208&
      Private Const ERROR_INVALID_PASSWORD = 86&
      Private Const ERROR_NO_NET_OR_BAD_PATH = 1203&
      Private strHost As String
      Private strResource As String
      Private strUser As String
      Private strPass As String


      Public Function StartShare() '(ByVal strUser As String, ByVal strPass As String, _
      ByVal strHost As String, ByVal strResource As String)
      Dim NetR As NETRESOURCE
      Dim ErrInfo As Long
      Dim MyPass, MyUser As String
      MyPass = strPass
      MyUser = strUser
      NetR.dwScope = RESOURCE_GLOBALNET
      NetR.dwType = RESOURCETYPE_DISK
      NetR.dwDisplayType = RESOURCEDISPLAYTYPE_SHARE
      NetR.dwUsage = RESOURCEUSAGE_CONNECTABLE
      'NetR.lpLocalName = "X:" ' If undefined, Connect with no device
      NetR.lpRemoteName = strHost & strResource   ' Your valid share
      'NetR.lpComment = "Optional Comment"
      'NetR.lpProvider =    ' Leave this undefined
      ' If the UserName and Password arguments are NULL, the user context
      ' for the process provides the default user name.
      ErrInfo = WNetAddConnection2(NetR, MyPass, MyUser, _
      CONNECT_UPDATE_PROFILE)
      If ErrInfo = NO_ERROR Then
        'MsgBox "Net Connection Successful!", vbInformation, "Share Connected"
      Else
        'MsgBox "ERROR: " & ErrInfo & " - Network Problem!", vbExclamation, "Share not Connected"
      End If

End Function

Public Function StopShare() '(ByVal strHost As String, ByVal strResource As String)
      Dim ErrInfo As Long
      Dim strLocalName As String
      ' You may specify either the lpRemoteName or lpLocalName
      strLocalName = strHost & strResource
      'strLocalName = "X:"
      ErrInfo = WNetCancelConnection2(strLocalName, _
      CONNECT_UPDATE_PROFILE, False)
      If ErrInfo = NO_ERROR Then
        'MsgBox "Net Disconnection Successful!", vbInformation, _
        "Share Disconnected"
      Else
        'MsgBox "ERROR: " & ErrInfo & " - Network Problem!", _
       'vbExclamation, "Share not Disconnected"
      End If

End Function


Private Sub Class_Initialize()
'strHost = "\\cdt0129co"
strHost = "\\Td-01\data\manchanda"
strUser = "manchanda"
'strPass = "355731" for PC
strPass = "353508" 'For TC
strResource = "\ShareBack"
End Sub

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
joshprakashAuthor Commented:
can u tell me for NETRESOURCE , wat kind of reference i
should include (in VB). It shows unkown datatype if i declare something of NETRESOURCE datatype
0
 
manchandaCommented:
this is the data type for winapi function , move this data type on top and depending upon class module or standard module define it private or public.
 

Private Type NETRESOURCE
       dwScope As Long
       dwType As Long
       dwDisplayType As Long
       dwUsage As Long
       lpLocalName As String
       lpRemoteName As String
       lpComment As String
       lpProvider As String
     End Type
0
 
joshprakashAuthor Commented:
i got it right .thanx very much to all for helping me
josh
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now