?
Solved

Net Connection using  mpr.dll

Posted on 2003-02-22
6
Medium Priority
?
806 Views
Last Modified: 2012-06-27
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
Comment
Question by:joshprakash
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 3

Expert Comment

by:Barca
ID: 8002036
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
 

Author Comment

by:joshprakash
ID: 8002247
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
 
LVL 2

Accepted Solution

by:
manchanda earned 80 total points
ID: 8003402
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
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.

 

Author Comment

by:joshprakash
ID: 8006168
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
 
LVL 2

Expert Comment

by:manchanda
ID: 8009633
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
 

Author Comment

by:joshprakash
ID: 8014165
i got it right .thanx very much to all for helping me
josh
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Navigation is an important part of web design from a usability perspective. But it is often a pain when it comes to a developer’s perspective. By navigation, it often means menuing. This is less theory and more practical of how to get a specific gro…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question