 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Sebastian Ledesma [Solidy Guest
|
Posted: Tue Mar 07, 2006 7:03 pm Post subject: How to log to a network share programatically |
|
|
Hi:
I wish to read a directory in a network server wich is protected by
user/password.
I'm using W.XP.
I can enter the \\servername\share at any windows and get into the logon
process, after that my program can read the directory without any problem.
I want to avoid manual logging and do it inside my app.
Thanks in advance.
Saludos
Sebastian |
|
| Back to top |
|
 |
Tamas Demjen Guest
|
Posted: Tue Mar 07, 2006 9:03 pm Post subject: Re: How to log to a network share programatically |
|
|
Sebastian Ledesma [Solidyne Labs] wrote:
| Quote: | I wish to read a directory in a network server wich is protected by
user/password.
I'm using W.XP.
I can enter the \\servername\share at any windows and get into the logon
process, after that my program can read the directory without any problem.
I want to avoid manual logging and do it inside my app.
|
You can do exactly that using the API function WNetAddConnection2:
bool Connect(const std::wstring& Provider, const std::wstring&
RemoteName, const std::wstring& UserName, const std::wstring& Password)
{
std::wstring provider = Provider;
std::wstring remotename = RemoteName;
NETRESOURCEW nr;
nr.dwScope = RESOURCE_GLOBALNET;
nr.dwType = RESOURCETYPE_ANY;
nr.dwDisplayType = RESOURCEDISPLAYTYPE_GENERIC;
nr.dwUsage = RESOURCEUSAGE_CONNECTABLE;
nr.lpLocalName = NULL;
nr.lpRemoteName = &remotename[0];
// such as "", "WORKGROUP" or "\\\\COMPUTER"
nr.lpComment = NULL;
nr.lpProvider = &provider[0]; // such as "Microsoft Windows Network"
return WNetAddConnection2W(&nr, Password.c_str(), UserName.c_str(),
0) != NO_ERROR;
}
bool Disconnect(const std::wstring& RemoteName)
{
WNetCancelConnection2W(RemoteName.c_str(), 0, TRUE);
return true;
}
Note that this is a Wide string version, and you could easily do an
AnsiString version by modifying the code a little bit.
However, this has a little problem: if your application crashes before
Disconnect is called, the connection remains open. This may be a
security issue.
There is another way to do it, which doesn't require you to open a
system-wide connection. Just use LogonUser and ImpersonateLoggedOnUser.
Here's some test code:
HANDLE token;
BOOL res = LogonUser(
"Tamas",
"password",
LOGON32_LOGON_NEW_CREDENTIALS,
LOGON32_PROVIDER_DEFAULT,
&token);
if(res)
{
res = ImpersonateLoggedOnUser(token);
if(res)
{
FileClose(FileCreate("\\\\Computer\\Share\\0.txt"));
//CopyFile("0.txt", "\\\\Computer\\Share\\0.txt", TRUE);
RevertToSelf();
}
CloseHandle(token);
}
else
Caption = IntToHex((int)GetLastError(), + " " +
IntToStr(GetLastError());
This code only works on Windows 2000 and XP, because it assumes that the
user only exists on the remote machine, not on the one that's running
your application. If the user also exists on the local computer, you can
use a more generic logon type with LogonUser, which also works on older
OSes, such as Win9x. Do a groups.google.com search with the words Demjen
and LogonUser, which will point to a thread discussing this issue in
greater detail.
Tom |
|
| Back to top |
|
 |
Sebastian Ledesma [Solidy Guest
|
Posted: Wed Mar 08, 2006 10:03 pm Post subject: Re: How to log to a network share programatically |
|
|
Thank's. It really helped!
Saludos
Sebastian
"Tamas Demjen" <tdemjen (AT) yahoo (DOT) com> escribió en el mensaje
news:440deb07$1 (AT) newsgroups (DOT) borland.com...
| Quote: | Sebastian Ledesma [Solidyne Labs] wrote:
I wish to read a directory in a network server wich is protected by
user/password.
I'm using W.XP.
I can enter the \\servername\share at any windows and get into the logon
process, after that my program can read the directory without any
problem.
I want to avoid manual logging and do it inside my app.
You can do exactly that using the API function WNetAddConnection2:
bool Connect(const std::wstring& Provider, const std::wstring& RemoteName,
const std::wstring& UserName, const std::wstring& Password)
{
std::wstring provider = Provider;
std::wstring remotename = RemoteName;
NETRESOURCEW nr;
nr.dwScope = RESOURCE_GLOBALNET;
nr.dwType = RESOURCETYPE_ANY;
nr.dwDisplayType = RESOURCEDISPLAYTYPE_GENERIC;
nr.dwUsage = RESOURCEUSAGE_CONNECTABLE;
nr.lpLocalName = NULL;
nr.lpRemoteName = &remotename[0];
// such as "", "WORKGROUP" or "\\\\COMPUTER"
nr.lpComment = NULL;
nr.lpProvider = &provider[0]; // such as "Microsoft Windows Network"
return WNetAddConnection2W(&nr, Password.c_str(), UserName.c_str(),
0) != NO_ERROR;
}
bool Disconnect(const std::wstring& RemoteName)
{
WNetCancelConnection2W(RemoteName.c_str(), 0, TRUE);
return true;
}
Note that this is a Wide string version, and you could easily do an
AnsiString version by modifying the code a little bit.
However, this has a little problem: if your application crashes before
Disconnect is called, the connection remains open. This may be a security
issue.
There is another way to do it, which doesn't require you to open a
system-wide connection. Just use LogonUser and ImpersonateLoggedOnUser.
Here's some test code:
HANDLE token;
BOOL res = LogonUser(
"Tamas",
"password",
LOGON32_LOGON_NEW_CREDENTIALS,
LOGON32_PROVIDER_DEFAULT,
&token);
if(res)
{
res = ImpersonateLoggedOnUser(token);
if(res)
{
FileClose(FileCreate("\\\\Computer\\Share\\0.txt"));
//CopyFile("0.txt", "\\\\Computer\\Share\\0.txt", TRUE);
RevertToSelf();
}
CloseHandle(token);
}
else
Caption = IntToHex((int)GetLastError(), + " " +
IntToStr(GetLastError());
This code only works on Windows 2000 and XP, because it assumes that the
user only exists on the remote machine, not on the one that's running your
application. If the user also exists on the local computer, you can use a
more generic logon type with LogonUser, which also works on older OSes,
such as Win9x. Do a groups.google.com search with the words Demjen and
LogonUser, which will point to a thread discussing this issue in greater
detail.
Tom |
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|