Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Reading/Writing files to samba share using username and password (QFile or QProcess)?

Reading/Writing files to samba share using username and password (QFile or QProcess)?

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 3 Posters 1.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MarKS
    wrote on 27 May 2021, 18:26 last edited by MarKS
    #1

    I have successfully achieved copying files to samba share (without username and password) using QFile thanks to this link .

    Also, in the same thread, other users have suggested how to handle if samba share has a username and password:

    You can use the copy command

    copy file.txt \ip_or_hostname\sharename

    However, if the user needed for the shares, is different from the current login user in windows, it will need to use run as which will not allow you to send a password as a parameter.

    In such cases, you can use
    net use x:\ip_or_hostname\sharename /user:username password

    I understand these are windows command prompt commands but I do not know how to use them in QProcess.

    Or is there a way to achieve samba authentication on Win 10 using QFile?

    A 1 Reply Last reply 27 May 2021, 18:37
    0
    • M MarKS
      27 May 2021, 18:26

      I have successfully achieved copying files to samba share (without username and password) using QFile thanks to this link .

      Also, in the same thread, other users have suggested how to handle if samba share has a username and password:

      You can use the copy command

      copy file.txt \ip_or_hostname\sharename

      However, if the user needed for the shares, is different from the current login user in windows, it will need to use run as which will not allow you to send a password as a parameter.

      In such cases, you can use
      net use x:\ip_or_hostname\sharename /user:username password

      I understand these are windows command prompt commands but I do not know how to use them in QProcess.

      Or is there a way to achieve samba authentication on Win 10 using QFile?

      A Offline
      A Offline
      artwaw
      wrote on 27 May 2021, 18:37 last edited by
      #2

      @MarKS Hi,
      the short answer is "no".

      The long answer: mapping/accessing network drives (applies to any protocol given Windows instance can understand) is outside Qt.
      If, however, your question is rather "but can I do it in C++" the answer is "probably yes". This should be done using one of the Windows APIs. No, I don't know which one. Usually this should be done beforehand by the sysadmin responsible for setting up your system (even if that sysadmin is you).

      If you are interested though you can start reading here https://docs.microsoft.com/en-us/windows-server/security/windows-authentication/credentials-processes-in-windows-authentication and figure out which parts of the Windows API services you'd like to access, then read how.

      For more information please re-read.

      Kind Regards,
      Artur

      1 Reply Last reply
      1
      • M Offline
        M Offline
        mrjj
        Lifetime Qt Champion
        wrote on 27 May 2021, 18:38 last edited by
        #3

        Hi
        QFile can not log in to anything so the access has to be there before we can copy.

        While it's possible to call net.exe from QProcess, it's a bit clunky.

        You could use
        https://docs.microsoft.com/en-us/windows/win32/api/winnetwk/nf-winnetwk-wnetaddconnection2a?redirectedfrom=MSDN

        but you have to link to some windows DLLs so it makes your app platform bound.

        A 1 Reply Last reply 27 May 2021, 18:40
        0
        • M mrjj
          27 May 2021, 18:38

          Hi
          QFile can not log in to anything so the access has to be there before we can copy.

          While it's possible to call net.exe from QProcess, it's a bit clunky.

          You could use
          https://docs.microsoft.com/en-us/windows/win32/api/winnetwk/nf-winnetwk-wnetaddconnection2a?redirectedfrom=MSDN

          but you have to link to some windows DLLs so it makes your app platform bound.

          A Offline
          A Offline
          artwaw
          wrote on 27 May 2021, 18:40 last edited by
          #4

          @mrjj That and fiddling with various net.exe options is possible too, I was just writing an edit to my post when you replied.

          For more information please re-read.

          Kind Regards,
          Artur

          1 Reply Last reply
          1
          • M Offline
            M Offline
            MarKS
            wrote on 27 May 2021, 18:45 last edited by
            #5

            @artwaw @mrjj while googling i stumbled upon this:

            QString strCommand;
                if(QSysInfo::productType()=="windows")
                    strCommand = "cmd /C ";
                strCommand += ui->lineEdit->text();
                m_process->start(strCommand);
            

            can I make something like this and expect it to work?

            strCommand += "net use x:\ip_or_hostname\sharename /user:username password";
            m_process->start(strCommand);
            
            A 1 Reply Last reply 27 May 2021, 18:49
            0
            • M MarKS
              27 May 2021, 18:45

              @artwaw @mrjj while googling i stumbled upon this:

              QString strCommand;
                  if(QSysInfo::productType()=="windows")
                      strCommand = "cmd /C ";
                  strCommand += ui->lineEdit->text();
                  m_process->start(strCommand);
              

              can I make something like this and expect it to work?

              strCommand += "net use x:\ip_or_hostname\sharename /user:username password";
              m_process->start(strCommand);
              
              A Offline
              A Offline
              artwaw
              wrote on 27 May 2021, 18:49 last edited by
              #6

              @MarKS Of course you can however implementing error handling will be cumbersome if at all viable (you'll need to grab and parse text output). Same for unmounting the resource.
              if you absolutely have to do it I rather urge you to the effort of learning how to do it via system calls/routines.

              For more information please re-read.

              Kind Regards,
              Artur

              1 Reply Last reply
              1
              • M Offline
                M Offline
                MarKS
                wrote on 27 May 2021, 18:51 last edited by
                #7

                @mrjj @artwaw thank you for your valuable inputs. I will try to read through the links and implement them.

                M 1 Reply Last reply 27 May 2021, 19:02
                0
                • M MarKS
                  27 May 2021, 18:51

                  @mrjj @artwaw thank you for your valuable inputs. I will try to read through the links and implement them.

                  M Offline
                  M Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on 27 May 2021, 19:02 last edited by
                  #8

                  @MarKS
                  Hi
                  What compiler are you using ? mingw or visual studio ?
                  You need to add some libs to the project file using
                  LIBS +=

                  M 1 Reply Last reply 27 May 2021, 19:10
                  0
                  • M mrjj
                    27 May 2021, 19:02

                    @MarKS
                    Hi
                    What compiler are you using ? mingw or visual studio ?
                    You need to add some libs to the project file using
                    LIBS +=

                    M Offline
                    M Offline
                    MarKS
                    wrote on 27 May 2021, 19:10 last edited by
                    #9

                    @mrjj I am using visual studio. Reading through the links if I choose to use WNetAddConnection2A function I need Mpr.dll

                    M 1 Reply Last reply 27 May 2021, 19:24
                    0
                    • M MarKS
                      27 May 2021, 19:10

                      @mrjj I am using visual studio. Reading through the links if I choose to use WNetAddConnection2A function I need Mpr.dll

                      M Offline
                      M Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on 27 May 2021, 19:24 last edited by
                      #10

                      @MarKS
                      Ok. so it should just work.
                      The example says
                      // Need to link with Netapi32.lib and Mpr.lib

                      not sure why also Netapi32 but it's something to keep in mind if you get undefined linker errors.

                      1 Reply Last reply
                      1

                      1/10

                      27 May 2021, 18:26

                      • Login

                      • Login or register to search.
                      1 out of 10
                      • First post
                        1/10
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved