Click here to Skip to main content
15,890,399 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello..

I have created a file using Core WIN32 & code is like this :

C++
HANDLE hFile = CreateFile("E:\\FileName123.txt", GENERIC_READ,0,0,OPEN_ALWAYS, FILE_ATTRIBUTE_READONLY, 0);


This will create a file with name FileName123 in E:Drive.

My question is: When it will create the file in E:Drive, it must become Copy Protect. That is., File should not copy to other location.

Please can u suggest any solution for this ..

Thanks..
Posted
Updated 9-Mar-12 2:01am
v2
Comments
Nelek 9-Mar-12 7:38am    
Do you want to avoid copy always? or only while you have the file open?
Guru_C++ 10-Mar-12 1:49am    
i dont want user to copy that file to some other location ..

a simple solution is to open file with exclusive mode, ie CFile::shareExclusive.

Hopes this helps
jkchan
http://cgmath.blogspot.com
 
Share this answer
 
Comments
Guru_C++ 9-Mar-12 7:18am    
Sorry... Din't got :(
Guru_C++ 9-Mar-12 7:56am    
And, Also i searched about LockFile in MSDN.. Can anyone tell me how to use that LockFile function..
You have passed '0' as third parameter (dwShareMode) to CreateFile() and the file is opened in exclusive mode.

Therefore, the file can't be opened again until the handle is closed.
 
Share this answer
 
Comments
Guru_C++ 10-Mar-12 1:50am    
So.. What i need to Pass for dwShareMode ..To make file as non-exclusive mode..
Jochen Arndt 10-Mar-12 4:02am    
A combination of FILE_SHARE_DELETE, FILE_SHARE_READ and FILE_SHARE_WRITE. See the MSDN [^]
Guru_C++ 12-Mar-12 0:38am    
After combination of all these, The file becomes Read only.. Can't chage the contents of the file.. But we can copy that file to some other location.. But how we can avoid that..! Suggest me..
Jochen Arndt 12-Mar-12 3:56am    
The file becomes read only, because you are passing GENERIC_READ. To lock the file while opened, pass dwShareMode = 0. Setting one or more of the above flags opens the file in non-exclusive mode, allowing access to the file while it is opened (shared mode).

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