views:

66

answers:

3

Hi, 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 little 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.

A: 

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.

Bork Blatt
sorry for not being very clear. I want to access a computer on same network(LAN). Please ignore all corner cases like vpn, computer across networks etc. And the file i want access is not shared. i want to give credentials and then access.
calvin
Answer revised - hopefully this is more helpful
Bork Blatt
Im not asking about file funtions like fopen or fclose etc. "check that the user you are logged in", i'm asking how to login to another machine from our c++ program to access some file on that machine. And the file is not shared at all.
calvin
I don't know all the ins and outs of Windows security, but there is no way I know of to remotely access a file that isn't shared or available on a web site. Not saying it is impossible, just that I don't know any way.
Bork Blatt
A: 

Execute mstsc.exe from your code using createprocess... Rest of thing it will handle...

mihirpmehta
i donot want to do RDC to that machine. i just want to access one file .
calvin
i don't know the other way to access files especially which are not shared...
mihirpmehta
+2  A: 

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.

MSalters
Thanks alot Msalters, I just confused with usage of dwflags. Thanks for clearing my confusion.
calvin