Createfile fails while reading mbr - winapi

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.

Related

Is it possible for a child process to read a file which is locked by parent process?

I have a Parent process which creates a file using CreateFile() and locks it. below is the code:
m_hWriterLockFile = ::CreateFile("C:\\Test.txt",
GENERIC_READ | GENERIC_WRITE,
0, // exclusive
NULL, // default security
OPEN_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);
Now, I want only the child process to access this locked file. I do not want any other process to read this file.
I have created a child process. Below is the sample code:
// Initialize a security attributes structure.
SECURITY_ATTRIBUTES sa;
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.lpSecurityDescriptor = pSD;
sa.bInheritHandle = TRUE;
if (!CreateProcess("FileReader.exe", // I want to invoke this exe
"C:\\Test.txt",
&sa,
NULL,
TRUE,
0,
NULL,
NULL,
&si,
&pi))
{
std::cout << "Create Process Faild (%d) " << GetLastError() << '\n';
}
Unfortunately, I am not able to achieve my goal, is there anyone who can help me? Is there any other way to achieve this? I will share more information(code) if needed.
Note: I didn't share the complete code to make the post shorter.
Here is the answer of of my question :
create the file with an inheritable handle and pass that handle to the child process. A simple way to do this is to pass it as a command line parameter. We must pass a SECURITY_ATTRIBUTES structure that specifies TRUE for bInheritHandle to CreateFile and also pass TRUE to the bInheritHandles parameter of the call to CreateProcess.
Create File
Ex:
SECURITY_ATTRIBUTES sa{ sizeof sa, nullptr, TRUE };
HANDLE hFile = CreateFileA("C:\\Test.txt",
GENERIC_READ | GENERIC_WRITE,
0,
&sa,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);
Child Process:
UINT_PTR uiHandle = reinterpret_cast<UINT_PTR>(hFile);
sprintf_s(szCmdLine, "\"%s\" %Iu", szExePath, uiHandle);
if (CreateProcessA(nullptr,
szCmdLine,
nullptr,
nullptr,
TRUE,
CREATE_NEW_CONSOLE,
nullptr,
nullptr,
&si,
&pi))
{
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
}

CreateFile winapi failed on volume C

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.

How Can I Get Exclusive Lock On Drive / Partition Win-Api 32?

How to lock lock on specific drive till i perform some operation(read/write) on that drive.
When i have captured lock on drive, no other process should able to write on that drive unitl I release lock on that drive, I have found one API i.e. DeviceIoControl(),
but I want access on drive for my process only, but the above API 'MAY' block access of all the processes. I'm not quite sure how lockfile() and lockfileex() works. Please help me find some way / API/ Logic to do this!
...
...
UPDATED :-
with referrance to your answer i have tried this :-->
HANDLE hdest,hf;
DWORD b,write;
hdest = CreateFile("\\\\?\\E:",
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
NULL,
NULL);
if (hdest == INVALID_HANDLE_VALUE)
{
MessageBox(NULL,TEXT("NOT OK"),TEXT("NOT OK"),0);
}
if (DeviceIoControl( hdest,FSCTL_LOCK_VOLUME,NULL,0,NULL,0,&b,NULL))
{
MessageBox(NULL,TEXT("OK"),TEXT("OK"),0);
hf = CreateFile(TEXT("E:\\M_lucky.txt"), GENERIC_WRITE, 0, NULL,CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
if (hf == INVALID_HANDLE_VALUE)
{
MessageBox(NULL,TEXT("NOT OK AFTER LOCK I"),TEXT("NOT OK AFTRE LOCK"),0);
}
WriteFile(hf, TEXT("M very lucky"), 13, &write, NULL);
}
CloseHandle((HANDLE)hdest);
CloseHandle((HANDLE)hf);
DeviceIoControl( hdest,FSCTL_UNLOCK_VOLUME,NULL,0,NULL,0,&b,NULL);
hf = CreateFile(TEXT("E:\\MNOT_lucky.txt"), GENERIC_WRITE, 0, NULL,CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
if (hf == INVALID_HANDLE_VALUE)
{
MessageBox(NULL,TEXT("NOT OK AFTER LOCK"),TEXT("NOT OK AFTRE LOCK"),0);
}
WriteFile(hf, TEXT("M lucky"), 8, &write, NULL);
CloseHandle((HANDLE)hf);
""but........""
after getting exclusive lock on E drive, i am unable to create file.
(i have read that : only hdest(HANDLE) gets exclusive lock on that drive. )
here..!!! i want to create M_LUCKY.txt file..!!
(I need EXLUSIVE access on drive with context to PROCESS)
plz help! thanks a lottt!!
is there any other method for doing this??
Use DeviceIoControl with FSCTL_LOCK_VOLUME control code.
http://msdn.microsoft.com/en-us/library/windows/desktop/aa364575(v=vs.85).aspx

C CreateFileMapping error 5 Access Denied ALWAYS

I would like to ask for help with WINAPI function CreateFileMapping (), which returns constantly NULL. After GetLastError() I get 5 - "ERROR_ACCESS_DENIED 5 (0x5) Access is denied". The file has been created after CreateFile with no problem, but following CreateFileMapping never has bee succesful.
int MapDestFile(LPCWSTR fPath)
{
hDestFile = CreateFile(
fPath,
GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (hSourceFile == NULL)
{
printf("%d\n", GetLastError());
}
hDestMapFile = CreateFileMapping(
hDestFile,
NULL,
PAGE_READWRITE,
0,
10,
NULL
);
if (hDestMapFile == NULL)
{
// here always tell error number 5
printf("%d\n", GetLastError());
}
lpMapAddressDestFile = MapViewOfFile(
hDestMapFile,
FILE_MAP_WRITE,
0,
0,
0);
if (lpMapAddressDestFile == NULL)
{
printf("%d\n", GetLastError());
}
return 1;
}
I would appreciate any suggestions.
Thanks
You need to create the file with GENERIC_WRITE | GENERIC_READ to match PAGE_READWRITE.
That seems self-evident when you think about it. How can you have memory that you can read from backed by a file that you cannot read from? The documentation does call this out explicitly in any case:
PAGE_READWRITE
The file handle that the hFile parameter specifies must be created with the GENERIC_READ and GENERIC_WRITE access rights.
On top of that your error checking on the call to CreateFile is wrong. Take another look at the documentations. Error is indicated by a return value of INVALID_FILE_HANDLE.

How to create file in Visual c++

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.

Resources