Posted by: Mike
I use my own method which is very similar.
Code: Select all
!!CB!! 81
Library name: mpr.dll
ThreadSafe: No
Function: WNetCancelConnection2A
Description: Drop a network connection
Export Ordinal: 0
Returns
Number: DWORD
Parameters
String: LPSTR
Number: DWORD
Boolean: BOOL
Function: WNetAddConnection2A
Description: Make a network connection
Export Ordinal: 0
Returns
Number: DWORD
Parameters
structPointer
Receive Number: DWORD
Receive Number: DWORD
Receive Number: DWORD
Receive Number: DWORD
Receive String: LPSTR
Receive String: LPSTR
Receive String: LPSTR
Receive String: LPSTR
String: LPSTR
String: LPSTR
Number: DWORD
Function: WNetGetLastErrorA
Description:
Export Ordinal: 0
Returns
Number: DWORD
Parameters
Number: LPVOID
String: LPSTR
Number: DWORD
String: LPSTR
Number: DWORD
Function: WNetGetConnectionA
Description: Get connection of a drive letter
Export Ordinal: 0
Returns
Number: DWORD
Parameters
String: LPCSTR
Receive String: LPSTR
Receive Number: LPDWORD
Function: WNetGetUserA
Description:
Export Ordinal: 0
Returns
Number: DWORD
Parameters
String: LPSTR
Receive String: LPSTR
Receive Number: LPDWORD
! Function: WNetGetUserDeviceA
Description: Get the user from a device or resource
Export Ordinal: 61
Returns
Number: DWORD
Parameters
String: LPSTR
Receive String: LPSTR
Receive Number: LPDWORD
Function: WNetUseConnectionA
Description: WNetUSeConnection BE AWARE CHECK MSDN LIBRARY is different for win9x/ME and win NT/2K/XP
Export Ordinal: 0
Returns
Number: DWORD
Parameters
Window Handle: HWND
structPointer
Number: DWORD
Number: DWORD
Number: DWORD
Number: DWORD
String: LPSTR
String: LPSTR
String: LPSTR
String: LPSTR
String: LPSTR
String: LPSTR
Number: DWORD
Receive String: LPSTR
Receive Number: LPDWORD
Receive Number: LPDWORD
Function: WNetAddConnectionA
Description:
Export Ordinal: 0
Returns
Number: DWORD
Parameters
String: LPCSTR
String: LPCSTR
String: LPCSTR
!!CB!! 134
Function: WNetUseConnection
Description: OS independant (Uses WinNT alike) WNetUseConnection
Returns
Number:
Parameters
String: lpLocalName
String: lpRemoteName
String: lpUserID
String: lpPassword
Receive String: sDriveLetter
Static Variables
Local variables
Number: dwScope
Number: dwDisplayType
Number: dwUseage
String: lpComment
Number: OSVersionInfoSize
Number: dwMajorVersion
Number: dwMinorVersion
Number: dwBuildNumber
Number: dwPlatformID
String: szCSDVersion
Number: wServicePackMajor
Number: wServicePackMinor
Number: wSuiteMask
Number: wProductType
Number: wReserved
String: lpAccessName
Number: lpBufferSize
Number: lpResult
Number: nTemp
Actions
Set sDriveLetter=''
If not GetVersionExA(156,dwMajorVersion,dwMinorVersion,dwBuildNumber,dwPlatformID,szCSDVersion,wServicePackMajor,wServicePackMinor,wSuiteMask,wProductType,wReserved)
Return GetLastError()
If dwPlatformID=0
Call SalMessageBox('This program can not run under win32S','Melding',0)
Call SalQuit()
Call SalStrSetBufferLength(lpAccessName,128)
Set lpBufferSize=127
If dwPlatformID=1
Set nTemp=WNetUseConnectionA(hWndNULL,2,1,3,1,lpLocalName,lpRemoteName,STRING_Null,STRING_Null,lpUserID,lpPassword,0x80,lpAccessName,lpBufferSize,lpResult)
If nTemp!=0
Return nTemp
Set sDriveLetter=lpAccessName
Return nTemp
If dwPlatformID=2
Set nTemp=WNetUseConnectionA(hWndNULL,2,1,3,1,lpLocalName,lpRemoteName,STRING_Null,STRING_Null,lpPassword,lpUserID,0x80,lpAccessName,lpBufferSize,lpResult)
If nTemp!=0
Return nTemp
Set sDriveLetter=lpAccessName
Return nTemp
Return -1
!!CB!! 134
Function: DriveMap
Description: Check if any drive is mapped under network,unc,user if so return drive letter else map any drive
Returns
Number:
Parameters
String: sMapname
String: sUser
String: sPassword
Receive String: sDrive
Receive Boolean: bNewMap
Static Variables
Local variables
String: sDrives[*]
String: sMap
Number: n
Number: nBound
String: sMappedUser
Number: nTemp
Actions
Call VisDosEnumDrives(sDrives)
Set n=1
! First check all network drives if it already is mapped if so return driveletter
Call SalArrayGetUpperBound(sDrives,1,nBound)
While n<=nBound
If VisDosGetDriveType(sDrives[n])=DRIVE_Network
Set nTemp=1023
Call SalStrSetBufferLength(sMap,1024)
If WNetGetConnectionA(sDrives[n],sMap,nTemp)=0
If SalStrUpperX(sMap)=SalStrUpperX(sMapname)
Set nTemp=80
Call SalStrSetBufferLength(sMappedUser,81)
If WNetGetUserA(sDrives[n],sMappedUser,nTemp)=0
! It is also possible that mappeduser comes with a domain or workgroup.
! So find the last \ and take it from there
Set nTemp=VisStrScanReverse(sMappedUser,SalStrLength(sMappedUser)-1,'\\')
If nTemp!=-1
Set sMappedUser=SalStrMidX(sMappedUser,nTemp+1,80)
If sUser=sMappedUser
Set sDrive=sDrives[n]
Set bNewMap=FALSE
Return 0
Set n=n+1
! apparently no drive is mapped here so try to map it
Set nTemp=WNetUseConnection(sDrive,sMapname,sUser,sPassword,sDrive)
Set bNewMap=TRUE
Return nTemp
And in case you need it:
Code: Select all
!!CB!! 134
Function: fWindowsError
Description: Give the windows error back
Returns
String:
Parameters
Number: nErr
Static Variables
Local variables
String: sErr
Actions
Call SalStrSetBufferLength(sErr,1024)
If nErr<0
Set nErr=-nErr
Call FormatMessageA(0x12FF,0,nErr,0,sErr,1024,0)
Return sErr
See also: http://msdn.microsoft.com/en-us/library/aa385482(VS.85).aspx
It works flawless with connections over here.