I try create file in visual studio c++.
But it now work, what is wrong?
CreateFile("1",
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED,
NULL);
If you try to create a file (not open it), you should not specify the OPEN_EXISTING flag. Instead, pass the CREATE_NEW constant:
CreateFile("1",
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
CREATE_NEW,
FILE_FLAG_OVERLAPPED,
NULL);
This code tries to open existing file: OPEN_EXISTING. Replace it with CREATE_NEW to create new file.
Related
This is from some old code that worked well for several years, but in our new project it got error. I have replaced all function arguments to constants, and tried using both wide-char and ASCII version and all got same result:
// compile with UNICODE=1
HANDLE handle = CreateFile( L"\\\\.\\C", 0, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
// or
HANDLE handle = CreateFileW( L"\\\\.\\C", 0, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
// or
HANDLE handle = CreateFileA( "\\\\.\\C", 0, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
The returned handle is 0xffffffff that is INVALID_HANDLE_VALUE, and GetLastError() return 2 which means no file found.
Running program with administrator privilege got same result.
I am trying to read raw bytes from a pen drive 'E:', but it gives error code 32 when trying to open the drive using CreateFIle(). My code is as follows:
wchar_t wszDrive[7];
wszDrive[0] = '\\';
wszDrive[1] = '\\';
wszDrive[2] = '.';
wszDrive[3] = '\\';
wszDrive[4] = 'e';
wszDrive[5] = ':';
wszDrive[6] = '\0';
hDevice = CreateFile(wszDrive, //drive name to open
GENERIC_READ | GENERIC_WRITE, ////must be opened with exclusive access(No Sharing)
0, // no access to the drive
NULL, // default security attributes
OPEN_EXISTING, // disposition i.e. if file already exist
0, // file attributes
NULL); // do not copy file attributes
if (hDevice == INVALID_HANDLE_VALUE) // cannot open the drive
{
printf("CreateFile() failed! from read with error %d.\n", GetLastError());// Program prints this line. with error code 32.
return (FALSE);
}
else
cout << "\nCreateFile() successful! in read";
Edit:
The CreateFile() runs fine without errors if I use FILE_SHARE_READ | FILE_SHARE_WRITE:
hDevice = CreateFile(wszDrive,
GENERIC_READ |
GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
0,
NULL);
Why cannot I run with exclusive access?
Error 32 is ERROR_SHARING_VIOLATION.
The process cannot access the file because it is being used by another process.
It means there is already an open handle to the drive, and that handle is using access/sharing rights which are not compatible with the access/sharing rights you are requesting.
That is why you can't open the drive for exclusive access, but you can open it for read/write sharing - the drive is already open elsewhere for reading/writing.
If you want to know where exactly, you can use a tool like SysInternals Process Explorer to see which processes have open handles to which files/folders, devices, etc.
I am using the following code to memory-map a file for reading in windows (Windows 10):
HANDLE windowsFileHdl = CreateFileA(filePath, GENERIC_READ, FILE_SHARE_WRITE, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if (windowsFileHdl == INVALID_HANDLE_VALUE)
{
return false;
}
HANDLE fileMappedHdl = CreateFileMappingA(windowsFileHdl, 0, PAGE_READONLY, 0, 0, nullptr);
char* baseFileData = (char*)MapViewOfFile(fileMapedHdl, FILE_MAP_READ, 0, 0, 0);
I would still like others to write to this file while I am reading it. Is this possible?
Currently, I get an error like this:
The process cannot access the file because it is being used by another process.
The above error was received by trying to open the file in a separate program (Notepad) while I had it memory mapped.
Is this a limitation with how the OS maps the file to memory or am I missing some flag?
EDIT:
#zett42 pointed out that I am missing FILE_SHARE_READ adding this makes the code:
HANDLE windowsFileHdl = CreateFileA(filePath, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if (windowsFileHdl == INVALID_HANDLE_VALUE)
{
return false;
}
HANDLE fileMappedHdl = CreateFileMappingA(windowsFileHdl, 0, PAGE_READONLY, 0, 0, nullptr);
char* baseFileData = (char*)MapViewOfFile(fileMapedHdl, FILE_MAP_READ, 0, 0, 0);
Now, notepad is able to open and write to the file!
However other programs like Visual Studio, Notepad++, or Emacs still have errors when writing to the file.
Visual Studio error:
The process cannot access the file because it is being used by another process.
Notepad++ error:
The file cannot be saved and it may be protected. Do you want to launch Notepad++ in Administrator mode?
I ran into an interesting problem. A searched many ways but I coundn't found any solution.
The problem is that I'm trying to open a shared printer in a domain with CreateFile function but it returns ERROR_FILE_NOT_FOUND errorcode.
If I use prompt and type
copy file.txt \\computer\printer
than it successfully completes the copy with result
1 file copied.
The CreateFile parameters is the following:
HANDLE file = CreateFile(printer, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (file == ERROR_INVALID_HANDLE)
{
char buffer[256];
sprintf_s(buffer, sizeof(buffer), "ErrorCode: %i", GetLastError());
MessageBox(NULL, buffer, "Error opening the file", MB_ICONERROR);
}
or
HANDLE file = CreateFile(printer, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (file == ERROR_INVALID_HANDLE)
{
char buffer[256];
sprintf_s(buffer, sizeof(buffer), "ErrorCode: %i", GetLastError());
MessageBox(NULL, buffer, "Error opening the file", MB_ICONERROR);
}
where printer containing the '\\host\printer' string according to the debugger.
I build it with Visual Studio 2005 SP1.
I tried to add the printer as Local priter as found on many places but it just resulted that the CreateFile just returned immediately.
Edit: On Windows XP it work perfectly. Windows 7 is the issue.
Edit2: The FOPEN function solves the problem so very likely the windows 7 implementation of function will be the issue.
Createfile fails while reading mbr on WinXP. Returns -1 i.e INVALID_DEVICE_HANDLE
HANDLE hDisk = CreateFile((LPCWSTR)"\\\\.\\PhysicalDrive0", GENERIC_READ| GENERIC_WRITE, FILE_SHARE_READ| FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0,NULL );
Any idea why???
You forgot to add 'L' to the string constant "\\.\PhysicalDrive0".
HANDLE hDisk = CreateFile(L"\\.\PhysicalDrive0", GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
This is right only when you are using the unicode version of the API, i.e. CreateFileW().
Use this:
HANDLE hDisk = CreateFile(L"\\\\.\\PhysicalDrive0", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
It's working for me.