Sunday, July 24, 2022

CPP: Windows: DeviceIoControl

CPP: Windows: DeviceIoControl

An application can use the DeviceIoControl function to perform direct input and output operations on, or retrieve information about, a floppy disk drive, hard disk drive, tape drive, or CD-ROM drive.

The following example demonstrates how to retrieve information about the first physical drive in the system. It uses the CreateFile function to retrieve the device handle to the first physical drive, and then uses DeviceIoControl with the IOCTL_DISK_GET_DRIVE_GEOMETRY control code to fill a DISK_GEOMETRY structure with information about the drive.
#include <windows.h>
#include <winioctl.h>
#include <stdio.h>
LPCWSTR wszDrive = L"\\\\.\\PhysicalDrive0";
BOOL GetDriveGeometryInfo(LPCWSTR wszPath, DISK_GEOMETRY* pdg)
{
HANDLE hDevice = INVALID_HANDLE_VALUE;
BOOL bResult = FALSE;
DWORD junk = 0;
hDevice = CreateFileW(wszPath,
0,
FILE_SHARE_READ |
FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
0,
NULL);
if (hDevice == INVALID_HANDLE_VALUE)
{
return (FALSE);
}
bResult = DeviceIoControl(hDevice,
IOCTL_DISK_GET_DRIVE_GEOMETRY,
NULL, 0,
pdg, sizeof(*pdg),
&junk,
(LPOVERLAPPED)NULL);
CloseHandle(hDevice);
return (bResult);
}
int wmain(int argc, wchar_t* argv[])
{
DISK_GEOMETRY pdg = { 0 };
BOOL bResult = FALSE;
ULONGLONG DiskSize = 0;
bResult = GetDriveGeometryInfo(wszDrive, &pdg);
if (bResult)
{
wprintf(L"Drive path = %ws\n", wszDrive);
wprintf(L"Cylinders = %I64d\n", pdg.Cylinders.QuadPart);
wprintf(L"Tracks/cylinder = %ld\n", (ULONG)pdg.TracksPerCylinder);
wprintf(L"Sectors/track = %ld\n", (ULONG)pdg.SectorsPerTrack);
wprintf(L"Bytes/sector = %ld\n", (ULONG)pdg.BytesPerSector);
DiskSize = pdg.Cylinders.QuadPart * (ULONG)pdg.TracksPerCylinder *
(ULONG)pdg.SectorsPerTrack * (ULONG)pdg.BytesPerSector;
wprintf(L"Disk size = %I64d (Bytes)\n"
L" = %.2f (Gb)\n",
DiskSize, (double)DiskSize / (1024 * 1024 * 1024));
}
else
{
wprintf(L"GetDriveGeometry failed. Error %ld.\n", GetLastError());
}
return ((int)bResult);
}

Notes:
- Tested using Visual Studio 2022 (17.2.6) C++ Windows Console template
Reference(s):
https://docs.microsoft.com/en-us/windows/win32/devio/calling-deviceiocontrol

Saturday, July 23, 2022

CPP: Windows: GetLogicalDriveStrings

CPP: Windows: GetLogicalDriveStrings

Fills a buffer with strings that specify valid drives in the system.
#include <iostream>
#include <Windows.h>
#include <vector>
int main()
{
#ifdef UNICODE
typedef std::wstring String;
#else
typedef std::string String;
#endif
const DWORD A2Z = 26;
const DWORD BUFFER = sizeof(TCHAR) * A2Z;
TCHAR lpBuffer[BUFFER];// buffer for drive string storage
GetLogicalDriveStrings(BUFFER, lpBuffer);
std::vector<String> drives_strings;
const TCHAR* current_drive = lpBuffer;
String str;
std::cout << "The logical drives of this machine are:" << std::endl;
while (*current_drive) {
str = current_drive;
drives_strings.push_back(str);
current_drive += drives_strings.back().length() + 1;
}
for (String s : drives_strings) {
#ifdef UNICODE
std::wcout << s << std::endl;
#else
std:cout << s < std::endl;
#endif
}
return 0;
}

Notes:
- Tested using Visual Studio 2022 C++ Windows Console template
Reference(s):
https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-getlogicaldrivestringsw

Friday, July 22, 2022

Python: Convert string to and from base64 format

Python: Convert string to and from base64 format

Base64 encoding is a format that can be used for data transfer in HTML or general data representation as you don't need to escape it. To convert a stream of byte object to base64 and vice versa, use the following example.
import base64
data = base64.b64encode(b'Hello World')
print(base64.b64decode(data))
# Hello World
view raw basetest.py hosted with ❤ by GitHub

Monday, July 18, 2022

Python: Print all ASCII upper case characters

Python: Print all ASCII upper case characters

import string
print(string.ascii_uppercase)
view raw uppercase.py hosted with ❤ by GitHub

Sunday, July 17, 2022

CPP: Windows: Get logical drives

CPP: Windows: Get logical drives

Retrieves a bitmask representing the currently available disk drives.
#include <iostream>
#include <Windows.h>
int main()
{
DWORD drives = GetLogicalDrives();
char start = 'A';
for (int i = 1; i <= 32; i++) {
auto x = i - 1;
auto driveBit = unsigned(1 << x);
if (drives & driveBit) {
std::cout << (char)(start + i - 1) << ":" << std::endl;
}
}
return 0;
}

Notes:
- Tested using Visual Studio 2022 C++ Windows Console template
Reference(s):
https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-getlogicaldrives

Configuring TUN/TAP virtual network interface for use with QEMU on Xubuntu 24.04

Configuring TUN/TAP virtual network interface for use with QEMU on Xubuntu 24.04 I am planning to run qemu-system-ppc to play around QEMU ...