Coder Social home page Coder Social logo

Client code examples about smblibrary HOT 8 CLOSED

talaloni avatar talaloni commented on July 19, 2024
Client code examples

from smblibrary.

Comments (8)

TalAloni avatar TalAloni commented on July 19, 2024

Hi Hagay,
Basic client functions are supported and should work.
Other than checking for status at the end of each operation, your code seems correct,
First you connect to the server, then you connect to the share using TreeConnect, once you connect to the share, you use CreateFile to get a file handle (as you would with the Windows Native API).

I have tested your code against Windows 7 SP1 as server, and I'm successfully creating the file,
Please note that STATUS_ACCESS_DENIED is coming from your server, not the client, which simply report back the status returned from the server (you can use WireShark to verify this).
Are you sure that the user you use for login have both share level permissions and file level permissions that allow for creating the file? Which Windows version do you use for server?

from smblibrary.

hagaygo avatar hagaygo commented on July 19, 2024

Hi Tal,

Thanks for your quick reply.

The server is latest windows 10 pro.

I Don't have at the moment a windows 7 machine to test against.

Maybe i am missing out on the path convention ?

On windows explorer the file is available on :

\\192.168.120.10\shared\zzz.txxt

and of course my username is able to list the content of :

\\192.168.120.10\shared

"shared" is of course available on the returned shares variable from ListShares method.

Can you share a working code to list the content of a share path (similar to my example) ?

Thanks Again,

Hagay

from smblibrary.

TalAloni avatar TalAloni commented on July 19, 2024

Here is a Working example that I tested against Windows 7 SP1 / 8.1 / 10:

SMB2Client client = new SMB2Client();
bool success = client.Connect(System.Net.IPAddress.Parse("192.168.1.121"), SMBTransportType.DirectTCPTransport);
if (success)
{
    NTStatus status = client.Login(String.Empty, username, password);
    if (status == NTStatus.STATUS_SUCCESS)
    {
        SMB2FileStore fileStore = client.TreeConnect("Shared", out status) as SMB2FileStore;
        if (fileStore != null)
        {
            object handle;
            FileStatus fileStatus;
            // Open existing file for reading
            status = fileStore.CreateFile(out handle, out fileStatus, "Notes.txt", AccessMask.GENERIC_READ, 0, ShareAccess.Read, CreateDisposition.FILE_OPEN, CreateOptions.FILE_NON_DIRECTORY_FILE, null);
            if (status == NTStatus.STATUS_SUCCESS)
            {
                byte[] data;
                status = fileStore.ReadFile(out data, handle, 0, 4096);
                fileStore.CloseFile(handle);
                if (status != NTStatus.STATUS_SUCCESS)
                {
                    return;
                }
                // Write the data to another file (create it if it does not exist)
                status = fileStore.CreateFile(out handle, out fileStatus, "Copy of Notes.txt", AccessMask.GENERIC_READ | AccessMask.GENERIC_WRITE, 0, ShareAccess.None, CreateDisposition.FILE_OPEN_IF, CreateOptions.FILE_NON_DIRECTORY_FILE, null);
                if (status == NTStatus.STATUS_SUCCESS)
                {
                    int numberOfBytesWritten;
                    status = fileStore.WriteFile(out numberOfBytesWritten, handle, 0, data);
                    fileStore.CloseFile(handle);
                }
            }
        }
    }
}

from smblibrary.

hagaygo avatar hagaygo commented on July 19, 2024

Thank you very much , i managed to get the file creation/copy to work , also list non "share root" folder :

var smb = new SMB2Client();
            var b = smb.Connect(new System.Net.IPAddress(new byte[] { 192, 168, 120, 10 }), SMBLibrary.SMBTransportType.DirectTCPTransport);
            var status = smb.Login(string.Empty, Username, password);
            SMBLibrary.NTStatus actionStatus;
            var shares = smb.ListShares(out actionStatus);
            var tree = smb.TreeConnect("shared", out status) as SMB2FileStore;
            object handle;
            SMBLibrary.FileStatus fs;

            // list \\192.168.20.10\shared - share root - returns STATUS_INVALID_PARAMETER
            actionStatus = tree.CreateFile(out handle, out fs, "", AccessMask.GENERIC_READ, 0, ShareAccess.Read | ShareAccess.Write | ShareAccess.Delete, CreateDisposition.FILE_OPEN, CreateOptions.FILE_DIRECTORY_FILE, null);
            if (actionStatus == NTStatus.STATUS_SUCCESS)
            {
                actionStatus = tree.QueryDirectory(out List<QueryDirectoryFileInformation> files, handle, "*", FileInformationClass.FileDirectoryInformation);
            }
            
            // list \\192.168.20.10\shared\Backups contents - works
            actionStatus = tree.CreateFile(out handle, out fs, "Backups", AccessMask.GENERIC_READ, 0, ShareAccess.Read, CreateDisposition.FILE_OPEN, CreateOptions.FILE_DIRECTORY_FILE, null);
            if (actionStatus == NTStatus.STATUS_SUCCESS)
            {
                actionStatus = tree.QueryDirectory(out List<QueryDirectoryFileInformation> files, handle, "*", FileInformationClass.FileDirectoryInformation);
            }

            // file read  , write works
            actionStatus = tree.CreateFile(out handle, out fs, "zzz.txt", AccessMask.GENERIC_READ, 0, ShareAccess.Read, CreateDisposition.FILE_OPEN, CreateOptions.FILE_NON_DIRECTORY_FILE, null);
            if (actionStatus == NTStatus.STATUS_SUCCESS)
            {
                byte[] data;
                status = tree.ReadFile(out data, handle, 0, 4096);
                tree.CloseFile(handle);
                if (status != NTStatus.STATUS_SUCCESS)
                {
                    return;
                }
                // Write the data to another file (create it if it does not exist)
                status = tree.CreateFile(out handle, out fs, "yyy.txt", AccessMask.GENERIC_READ | AccessMask.GENERIC_WRITE, 0, ShareAccess.None, CreateDisposition.FILE_OPEN_IF, CreateOptions.FILE_NON_DIRECTORY_FILE, null);
                if (status == NTStatus.STATUS_SUCCESS)
                {
                    int numberOfBytesWritten;
                    status = tree.WriteFile(out numberOfBytesWritten, handle, 0, data);
                    tree.CloseFile(handle);
                }
            }

As for listing share root content, documentation for SMB is very hard to find , i guess "" is correct , becuase if i i use "." i get different status code.
So my guess a different parameters are required than a regular folder query.

from smblibrary.

TalAloni avatar TalAloni commented on July 19, 2024
  1. Yes, as you can see in the SMB2 specifications: "A zero length file name indicates a request to open the root of the share".

  2. I noticed that there is a specific Windows 8.1 / Windows 10 issue that I now took care of, please update to the latest commit.

  3. I have updated the previous code sample (forgot to check if fileStore is not null)

To open the root directory of the share and list it's content:

status = fileStore.CreateFile(out handle, out fileStatus, "", AccessMask.SYNCHRONIZE | (AccessMask)DirectoryAccessMask.FILE_LIST_DIRECTORY, 0, ShareAccess.Read | ShareAccess.Write | ShareAccess.Delete, CreateDisposition.FILE_OPEN, CreateOptions.FILE_SYNCHRONOUS_IO_NONALERT | CreateOptions.FILE_DIRECTORY_FILE, null);
if (status == NTStatus.STATUS_SUCCESS)
{
    List<QueryDirectoryFileInformation> result;
    status = fileStore.QueryDirectory(out result, handle, "*", FileInformationClass.FileNamesInformation);
}

BTW, You can use WireShark to see which parameters Windows clients use, and you can also check the Native API documentation (not to be confused with the Win32 API)

from smblibrary.

hagaygo avatar hagaygo commented on July 19, 2024

Running my code with your latest commit works fine now.

Thanks a lot for all your help and work.

from smblibrary.

rdang93 avatar rdang93 commented on July 19, 2024

Hi @TalAloni

When trying to use

`

                        status = fileStore.CreateFile(out object directoryHandle, out FileStatus fileStatus, filePath, AccessMask.GENERIC_READ, FileAttributes.Directory, ShareAccess.Read | ShareAccess.Write, CreateDisposition.FILE_OPEN, CreateOptions.FILE_DIRECTORY_FILE, null);

                    if (status == NTStatus.STATUS_SUCCESS)
                    {
                        List<QueryDirectoryFileInformation> fileList;
                        status = fileStore.QueryDirectory(out fileList, directoryHandle, "*", FileInformationClass.FileNamesInformation);
                        status = fileStore.CloseFile(directoryHandle);
                  }

`

Gives Not Implemented exception, I wanted to list file names so I can read each file in a loop

from smblibrary.

TalAloni avatar TalAloni commented on July 19, 2024

@rdang93 Please see the existing examples to retrieve list of file names in a directory.

from smblibrary.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.