Login to a remote machine & accessing network resources
I want to access a file on remote machine(win2k3, 10.10.20.30), but i couldn't understand how to login to that machine in my program. is there any simple win api that takes network path, credentials and returns the handle? i just want to access \10.10.20.30\c$\test.txt, WNetAddConnection2, WNetAddConnection3 are li开发者_如何转开发ttle confusing. Any suggestion will be helpful.
sorry for not being very clear. I want to access a computer on same network(LAN). I wanted to access a file that is not shared on other computer.
If you have administrator rights, the solution is fairly simple. The C$
administrative share is available. You can call WNetAddConnection2
to create a local driveletter pointing to it. NETRESOURCE.dwType = RESOURCETYPE_DISK
of course, .lpLocalName = NULL
as you don't need it, .lpRemoteName = _T("\\\\10.10.20.30\\c$")
(note the escaping of \ in C strings, it really starts with 4 of them). .lpProvider = NULL
- let Windows figure the provider out.
Leave the username/password empty, and Windows will use your current user credentials. If those are indeed (network) administrator credentials, they're sufficient.
dwFlags
should include CONNECT_TEMPORARY
, as you're only interested in one file.
However, I think that (given sufficient credentials) it's easier to just call CreateFile("\\\\10.10.20.30\\c$\\test.txt")
and let Windows deal with the details.
Normal c++ file access functions and libraries should work as is, just put the full network path to the file where you would put the file name, and you should be able to access. Good tutorials with sample code available at this link: http://www.cplusplus.com/doc/tutorial/files/
If you are getting errors, check that the user you are logged in as has file permissions set on the shared folder, as well as sharing permissions.
Execute mstsc.exe from your code using createprocess... Rest of thing it will handle...
精彩评论