Reading/Writing files to samba share using username and password (QFile or QProcess)?
-
wrote on 27 May 2021, 18:26 last edited by 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 passwordI 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
? -
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 passwordI 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
?wrote on 27 May 2021, 18:37 last edited by@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.
-
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=MSDNbut you have to link to some windows DLLs so it makes your app platform bound.
-
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=MSDNbut you have to link to some windows DLLs so it makes your app platform bound.
-
wrote on 27 May 2021, 18:45 last edited by
@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);
-
@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);
wrote on 27 May 2021, 18:49 last edited by@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. -
@MarKS
Hi
What compiler are you using ? mingw or visual studio ?
You need to add some libs to the project file using
LIBS += -
@MarKS
Hi
What compiler are you using ? mingw or visual studio ?
You need to add some libs to the project file using
LIBS += -
@mrjj I am using visual studio. Reading through the links if I choose to use WNetAddConnection2A function I need Mpr.dll
@MarKS
Ok. so it should just work.
The example says
// Need to link with Netapi32.lib and Mpr.libnot sure why also Netapi32 but it's something to keep in mind if you get undefined linker errors.
1/10