Click here to Skip to main content
15,662,459 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I found this question somewhere on internet now facing same problem. With little change i already have modfied question below.


I want to access a file on remote machine(, but i couldnt 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 of file? i just want to access \\\share folder\test.txt.
Any suggestion will be helpful.

sorry for not being very clear in coding sense.
I wanted to access a file that is shared in a shared folder but permission is not given to me. I am using <u><b>::CreateFile</b></u> to get the handel of the file . But in this case i am reciveing error code 5 (ACCESS_DENIED). i need to use user (my client) credential (username password) to access file which is shared. I just need handle of file.

Tell me if i am wrong ::CreateFile cannot be used in this case by using or modifying security attributes as MSDN says.
CreateFile ignores the lpSecurityDescriptor member when opening an existing file.I also thought of using Window shell but could not reched a solution.

problem Detail
Well the requirement is like this at remote machine i do not have EveryOne permission. Let me explain through an example. Suppose there is network with certain domain having atleast 3 computer lets say 5. Now there name are C1(client),C2(destination),C3,C4,C5. Now in this case i will create a file(any type) at C2 machine and will put that in a folder say MyFolder. Now make it share and after that and remove <big>EveryOne</big> from it. After that add C5 as user for accessing it. Now you can see write the some code with just ::CreateFile implementation and run it on both machine C1(client) and C5(it has permission). You will see client(C1) will get error 5 which means ACCESS_DENIED but C5 will execute it and will get the file handle......Now again i am repeating my question i need to get the file handle by any means through my user name and password, And my program will get the handle from that C1 machine..

Please advice me
Thanks in advance
Updated 23-Sep-13 9:01am

1 solution

I don't know the answer specifically but in general terms, you need to meet the security criteria for the file open method. A few things to consider:
(1) Look at the characteristics of the file share, the file in question and the directory/directories to get to it. If any of these deny access to you, you will get an access failure. You probably want to check each for read, write, delete access and match with the username you specify to connect to the machine (explicitly specified or by default).
(2) Look at the file open specifications you provide on your CreateFile() call. Check if you are trying to open the file for read, write, read+write, shared access etc.
(3) Even if your credentials match, you can get an access failure for example if another process has the file open for exclusive access.

Also, CreateFile is a misleading name. It is an API for opening a file in any of several modes. There is only one specific special case where it creates a file.
Share this answer
kaushik4study 21-Sep-13 15:45pm    
@H.Brydon First of all thank you for answering.
as per your points.
1. i am not restricted from accessing the file. So this point is correct from my side.
2. ::createfile() i am trying to open the existing file, this is actual scenario and it will never change in my case. I will never create a new file. always existing file will be opened.
3. No other process is accessing the file but your concern is valid but not required in current scenario i am sure for this too.

Yes its quite misleading but..i read doc about it on MSDN. as i mention above in my question in bold letters. I think it is not possible with ::createFile what i want to achieve.
H.Brydon 22-Sep-13 2:38am    
What are you specifying for parameters 2, 3 and 6?
What operating system for your local machine and the remote machine?
Are there any firewall issues between your machine and the remote machine?
kaushik4study 23-Sep-13 1:44am    
What are you specifying for parameters 2, 3 and 6?
What operating system for your local machine and the remote machine?
::both are XP sp2
Are there any firewall issues between your machine and the remote machine?
kaushik4study 23-Sep-13 1:45am    
@h.Brydon. One more thing if you are thinking only ::CleateFile is solution i want to mention that

1. lpSecurityAttributes [in, optional] "CreateFile ignores the lpSecurityDescriptor member when opening an existing file or device, but continues to use the bInheritHandle member"

2. Is there any solution throuh window shell coding. or something before CreateFile.
H.Brydon 23-Sep-13 10:49am    
I am out of ideas but it still goes back to file system protection on the share, on the host machine directory/directories and the file. Perhaps place a file on the remote machine with "everyone" write access, same again for its parent directory and work backwards from there.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900