visual studio

Asked 2 years ago, Updated 2 years ago, 92 views

c winapi mbr

2022-09-22 18:55

1 Answers

CreateFile depends on the value of character set in the project settings shown in the picture below.

A preprocessing macro called UNICODE is defined when the value of character set is set to Unicode character set.

UNICODE determines whether CreateFileW or CreateFileA is used as shown below.

Looking at the warning, UNICODE is currently set, and CreateFile means CreateFileW.

CreateFileW must use a wide character string (L"hello") beginning with L. However, the "\\\\.Where \\physicalDrive0" is Multi-byte string .

In Windows, a wide character string determines that one character is 2 bytes, so if you run it as it is, it will not behave normally.

There are two solutions to this.

_T Use

_T automatically changes the type of string based on the settings described at the beginning.

HANDLE hFile = CreateFile(_T("\\\\.\\physicalDrive0"), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);

Use CreateFileA or CreateFileW

You can use a function that fits the string you want to use from the beginning so that the project properties are not affected.

HANDLE hFile = CreateFileA("\\\\.\\physicalDrive0", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);

I tried to run the log by adding it.

The device could not be opened when the normal run was performed as shown below.

The device opened as shown below when running with administrator privileges.

If you reboot after that, you can see that the boot failed as shown below.

This means that programs that access the device directly could not open the device by running it without administrator privileges, resulting in no write operations. Of course, if you didn't change it to CreateFileA, the device wouldn't have opened because it's not the right path.

For your information, it has been terminated due to 0 code means that the program returned zero. If the program returns zero, it means that the program ended normally.


2022-09-22 18:55

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.