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)?
Forum Updated to NodeBB v4.3 + New Features

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.7k Views 3 Watching
  • 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.
  • MarKSM Offline
    MarKSM Offline
    MarKS
    wrote on 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?

    artwawA 1 Reply Last reply
    0
    • MarKSM MarKS

      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?

      artwawA Offline
      artwawA Offline
      artwaw
      wrote on 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
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on 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.

        artwawA 1 Reply Last reply
        0
        • mrjjM mrjj

          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.

          artwawA Offline
          artwawA Offline
          artwaw
          wrote on 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
          • MarKSM Offline
            MarKSM Offline
            MarKS
            wrote on 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);
            
            artwawA 1 Reply Last reply
            0
            • MarKSM MarKS

              @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);
              
              artwawA Offline
              artwawA Offline
              artwaw
              wrote on 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
              • MarKSM Offline
                MarKSM Offline
                MarKS
                wrote on last edited by
                #7

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

                mrjjM 1 Reply Last reply
                0
                • MarKSM MarKS

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

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on 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 +=

                  MarKSM 1 Reply Last reply
                  0
                  • mrjjM mrjj

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

                    MarKSM Offline
                    MarKSM Offline
                    MarKS
                    wrote on 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

                    mrjjM 1 Reply Last reply
                    0
                    • MarKSM MarKS

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

                      mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on 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

                      • Login

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