Fabio Di Paola

LotusScript NetFunctions

12 Dicembre 2017 Domino Notes Sviluppo 0

Ripubblico qui un pezzo di codice LotusScript che mi era servito tempo fa ma che ora non ritrovo più in rete .
Probabilmente il post originale se ne è andato ma potrebbe servire ancora a qualcuno .

Si tratta di una libreria (chiamata Netfunctions ) che permette di eseguire connect e disconnect da share di rete per scrivere o leggere dati, il tutto dall’interno di una procedura LotusScript.

Per prima cosa la libreria. Copiate ed incollate il codice qui sotto in una Script Library del vostro database e chiamatela NetFunctions (o come preferite):

' ---- NetFunctions Library START---- 

‘ ******** Options section
Option Public
Public Const NO_ERROR = 0
Public Const CONNECT_UPDATE_PROFILE = &H1
‘ The following includes all the constants defined for NETRESOURCE,
‘ not just the ones used in this example.
Public Const RESOURCETYPE_DISK = &H1
Public Const RESOURCETYPE_PRINT = &H2
Public Const RESOURCETYPE_ANY = &H0
Public Const RESOURCE_CONNECTED = &H1
Public Const RESOURCE_REMEMBERED = &H3
Public Const RESOURCE_GLOBALNET = &H2
Public Const RESOURCEDISPLAYTYPE_DOMAIN = &H1
Public Const RESOURCEDISPLAYTYPE_GENERIC = &H0
Public Const RESOURCEDISPLAYTYPE_SERVER = &H2
Public Const RESOURCEDISPLAYTYPE_SHARE = &H3
Public Const RESOURCEUSAGE_CONNECTABLE = &H1
Public Const RESOURCEUSAGE_CONTAINER = &H2
‘ Error Constants:
Public Const ERROR_ACCESS_DENIED = 5&
Public Const ERROR_ALREADY_ASSIGNED = 85&
Public Const ERROR_BAD_DEV_TYPE = 66&
Public Const ERROR_BAD_DEVICE = 1200&
Public Const ERROR_BAD_NET_NAME = 67&
Public Const ERROR_BAD_PROFILE = 1206&
Public Const ERROR_BAD_PROVIDER = 1204&
Public Const ERROR_BUSY = 170&
Public Const ERROR_CANCELLED = 1223&
Public Const ERROR_CANNOT_OPEN_PROFILE = 1205&
Public Const ERROR_DEVICE_ALREADY_REMEMBERED = 1202&
Public Const ERROR_EXTENDED_ERROR = 1208&
Public Const ERROR_INVALID_PASSWORD = 86&
Public Const ERROR_NO_NET_OR_BAD_PATH = 1203&

‘ ******** Declarations section
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
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
Declare Function WNetCancelConnection2 Lib “mpr.dll” Alias _
“WNetCancelConnection2A” (Byval lpName As String, _
Byval dwFlags As Long, Byval fForce As Long) As Long
Dim NetPath As String , MyPass As String, MyUser As String, LocalDriveString As String
Dim MyFileName$

Public Sub Connect()
Dim NetR As NETRESOURCE
Dim ErrInfo As Long

NetR.dwScope = RESOURCE_GLOBALNET
NetR.dwType = RESOURCETYPE_DISK
NetR.dwDisplayType = RESOURCEDISPLAYTYPE_SHARE
NetR.dwUsage = RESOURCEUSAGE_CONNECTABLE
If LocalDriveString<>”” Then NetR.lpLocalName = NetPath ‘ If undefined, Connect with no device
NetR.lpRemoteName = NetPath ‘ Your valid share
‘NetR.lpComment = “Optional Comment”
‘NetR.lpProvider = ‘ Leave this undefined

‘ If the MyPass and MyUser arguments are null (use vbNullString), the
‘ user context for the process provides the default user name.

ErrInfo = WNetAddConnection2(NetR, MyPass, MyUser, _
CONNECT_UPDATE_PROFILE)
If ErrInfo = NO_ERROR Then
Print”Net Connection Successful!”+” (“+NetPath+”)”
Else
Msgbox “Connect Error:”+Str(ErrInfo )+”.Net Connection Failed!”
End If
End Sub

Public Sub Disconnect()
Dim ErrInfo As Long
Dim strLocalName As String
‘ You may specify either the lpRemoteName or lpLocalName
If LocalDriveString=”” Then strLocalName = NetPath Else strLocalName = LocalDriveString
‘strLocalName = “H:”
ErrInfo = WNetCancelConnection2(strLocalName, _
CONNECT_UPDATE_PROFILE, True)
If ErrInfo = NO_ERROR Then
Print “Net Disconnection Successful!” +” (“+strLocalName+”)”
Else
Msgbox “Disconnect Error:”+Str(ErrInfo )+”. Net Disconnection Failed!”
End If
End Sub

‘ —- NetFunctions Library END—-

Poi createvi il vostro agent LotusScript che come prima istruzione deve avere un Use "NetFunctions" (o come l’avete chiamata).
Qui sotto il codice di esempio per vedere come utilizzarla:

' ---- Agent START ----
Option Public
Use "NetFunctions"

Sub Initialize
Dim session As New notessession

NetPath= “\\10.0.0.2\folder$”
MyFileName$=NetPath+”\myfile.txt”
MyUser =”windowsuser”
MyPass =”userpassword”
LocalDriveString=”” ‘ can be left empty

Call Connect
Call writeTofile(“test string to write”)
Call Disconnect

Call Connect
tmpread=readFromfile()
Print “File last updated: “+Cstr(Filedatetime(MyFileName$))
Call Disconnect
MsgBox tmpread

End Sub

Sub writeTofile(wrstr as String)
Open MyFileName$ For Output As #1
Print #1, wrstr
Close #1
End Sub

Function readFromfile()
Open MyFileName$ For Input As #2
Do Until Eof(2)
Line Input #2, tmp
tmp1=tmp1+tmp
Loop
Close #2
readFromfile=tmp1
End Function
‘ —- Agent END —-

Buon codice !
Lotus Notes Domino