B4J Question jcifs-ng SMB-Client (SMB2)

ahabbass

Member
Can I have a simple code example of using the jcifs-ng lib to download a file from a Windows 10 shared folder?
The only example I can find is for B4A. Currently I try to make it work in B4J with no success up to now.

Thank you in advance!
 

DonManfred

Expert
Licensed User
Longtime User
1. Update to the latest jcifs-ng library.
2. In app start you initialize the Client with the Share you want to have write Access to,
B4X:
client.Initialize("SMBClient","domain","username","password","smb://192.168.2.252/Share/")

It then raises the Event SMBClient_Resource in which you get the link to the Share.
B4X:
Sub SMBClient_Resource(success As Boolean, smbobjres As Object,smbobj As Object, info As String)
    Dim smbResource As SMBResource = smbobjres ' THIS is the needed Object. it is the Folder/Share you have initialized
    Dim smbfile As SMBFile = smbobj

    Log($"SMBClient_Resource(${success},${smbResource}, ${smbfile},${info})"$)
    Log($"OwnerGroup: ${smbResource.OwnerGroup}"$)
    'Dim ownersid As SMBSID = smbResource.OwnerGroup
    'Log($"OwnerUser: ${smbResource.OwnerUser}"$)
    'Log($"canWrite: ${smbResource.canWrite}"$)
    'Log($"OwnerGroupSIDDomainName: ${ownersid.DomainName}"$)
    'Log($"OwnerGroupSIDAccountName: ${ownersid.AccountName}"$)
    'Log($"OwnerGroupType: ${ownersid.TypeText}"$)

    'Dim owner As SMBSID = smbResource.OwnerUser
    'Log($"UserSIDDomainName: ${owner.DomainName}"$)
    'Log($"OwnerSIDAccountName: ${owner.AccountName}"$)
    'Log($"OwnerType: ${owner.TypeText}"$)

    'Log($"CanonicalPath = ${smbfile.CanonicalPath}"$)
    'Log($"CanonicalUncPath = ${smbfile.CanonicalUncPath}"$)

    client.Copy2("E:\","cat.zip",smbResource,"cat.zip",16384) ' Copy E:\cat.zip to the Share with a Blocksize of 16384 Bytes
End Sub
while copying you´ll get multiple calls to
B4X:
Sub SMBClient_CopyProgress(totalBytes As Long, path As String, filename As String)
    Log($"SMBClient_CopyProgress(${File.Combine(path,filename)},${NumberFormat(totalBytes / 1024, 0, 0)} KB"$)
End Sub
and at the end of the Filecopy the event
B4X:
Sub SMBClient_CopyResult(success As Boolean, path As String, filename As String)
    Log($"SMBClient_CopyResult(${success}, ${path}, ${filename})"$)
End Sub
is raised.


If you need to copy a file to another Share (but with the same credentials as the initial Share) you need to get the correct Object of this Share to use this in the Copy2 call then.

Ex:
B4X:
    client.GetResourcefromUrl("smb://192.168.2.252/Another/Share/")
    wait for SMBClient_Resource(success As Boolean, smbobjres As Object,smbobj As Object, info As String)
    Dim smbres As SMBResource = smbobjres ' This is what we need to copy to
    Dim smbfileiso As SMBFile = smbobj
 
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…