Alguien me pudiera aclarar como se puede obtener el número de serie de un disco físico, también el modelo del disco.
OJO!!! Que sea usando la API de Windows y en VB
GetVolumeInformationA function (fileapi.h)
https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-getvolumeinformationaEncontré el uso en C/C++ solo tradúcelo y adáptalo a tus necesidades que tampoco creo en los foros se hagan encargos de codigo a modo.
https://gist.github.com/micjabbour/a2fbe50160862e6abe439c0b0769c3fbLo modifique para listara los números de serie de todos los discos físicos instalados (es un asco como lo adapte, lo se jajajaja).
#include <windows.h>
#include <memory>
#include <string>
#include <iostream>
#include <bitset>
#include <vector>
using namespace std;
#define CTL_CODE(DeviceType, Function, Method, Access) \
(((DeviceType) << 16) | ((Access) << 14) | ((Function) << 2) | (Method))
#define METHOD_BUFFERED 0
//returns the serial number of the first physical drive in a string or an empty string in case of failure
//based on http://codexpert.ro/blog/2013/10/26/get-physical-drive-serial-number-part-1/
string getFirstHddSerialNumber(wchar_t *pDisk) {
//get a handle to the first physical drive
HANDLE h = CreateFileW(pDisk, 0, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
if(h == INVALID_HANDLE_VALUE) return {};
//an unique_ptr is used to perform cleanup automatically when returning (i.e. to avoid code duplication)
unique_ptr<remove_pointer<HANDLE>::type, void(*)(HANDLE)> hDevice{h, [](HANDLE handle){CloseHandle(handle);}};
//initialize a STORAGE_PROPERTY_QUERY data structure (to be used as input to DeviceIoControl)
STORAGE_PROPERTY_QUERY storagePropertyQuery{};
storagePropertyQuery.PropertyId= StorageDeviceProperty;
storagePropertyQuery.QueryType= PropertyStandardQuery;
//initialize a STORAGE_DESCRIPTOR_HEADER data structure (to be used as output from DeviceIoControl)
STORAGE_DESCRIPTOR_HEADER storageDescriptorHeader{};
//the next call to DeviceIoControl retrieves necessary size (in order to allocate a suitable buffer)
//call DeviceIoControl and return an empty string on failure
DWORD dwBytesReturned= 0;
if(!DeviceIoControl(hDevice.get(), IOCTL_STORAGE_QUERY_PROPERTY, &storagePropertyQuery, sizeof(STORAGE_PROPERTY_QUERY),
&storageDescriptorHeader, sizeof(STORAGE_DESCRIPTOR_HEADER), &dwBytesReturned, NULL))
return {};
//allocate a suitable buffer
const DWORD dwOutBufferSize= storageDescriptorHeader.Size;
unique_ptr<BYTE[]> pOutBuffer{new BYTE[dwOutBufferSize]{}};
//call DeviceIoControl with the allocated buffer
if(!DeviceIoControl(hDevice.get(), IOCTL_STORAGE_QUERY_PROPERTY, &storagePropertyQuery, sizeof(STORAGE_PROPERTY_QUERY),
pOutBuffer.get(), dwOutBufferSize, &dwBytesReturned, NULL))
return {};
//read and return the serial number out of the output buffer
STORAGE_DEVICE_DESCRIPTOR* pDeviceDescriptor= reinterpret_cast<STORAGE_DEVICE_DESCRIPTOR*>(pOutBuffer.get());
const DWORD dwSerialNumberOffset= pDeviceDescriptor->SerialNumberOffset;
if(dwSerialNumberOffset==0) return {};
const char* serialNumber= reinterpret_cast<const char*>(pOutBuffer.get() + dwSerialNumberOffset);
return serialNumber;
}
void printPhysicalDriversSerial() {
bitset<32> drives(GetLogicalDrives());
vector<char> goodDrives;
for (char c = 'A'; c <= 'Z'; ++c) {
if (drives[c - 'A']) {
//if (GetDriveType((c + string(":\\")).c_str()) == DRIVE_FIXED) { descomentar para solo obtener los Discos físicos internos.
goodDrives.push_back(c);
//}
}
}
for (auto & drive : goodDrives) {
string s = string("\\\\.\\") + drive + ":";
HANDLE h = CreateFileA(
s.c_str(), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
OPEN_EXISTING, FILE_FLAG_NO_BUFFERING | FILE_FLAG_RANDOM_ACCESS, NULL
);
if (h == INVALID_HANDLE_VALUE) {
cerr << "Drive " << drive << ":\\ cannot be opened" << endl;
continue;
}
DWORD bytesReturned;
VOLUME_DISK_EXTENTS vde;
if (!DeviceIoControl(
h, IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS,
NULL, 0, &vde, sizeof(vde), &bytesReturned, NULL
)) {
cerr << "Drive " << drive << ":\\ cannot be mapped into physical drive" << endl;
continue;
}
for (int i = 0; i < vde.NumberOfDiskExtents; ++i) {
wchar_t disk [MAX_PATH];
swprintf(disk, MAX_PATH, L"\\\\.\\PhysicalDrive%d", vde.Extents[i].DiskNumber);
string serialNumber = getFirstHddSerialNumber(disk);
fputws ( disk, stdout );
cout << endl;
if(serialNumber.empty()) {
cout << "failed to retrieve serial number" << endl;
} else {
cout << "serial number: " << serialNumber << endl;
}
}
}
}
int main() {
printPhysicalDriversSerial();
return 0;
}
Para corroborar los datos pintados, ejecutar en CMD:
wmic diskdrive get Name, Manufacturer, Model, InterfaceType, MediaType, SerialNumber
Saludos.