Member Avatar for Member #570982

Hi everybody,
I'm looking for a way of getting a machine-specific string on windows, something like cpu or hard drive or bios serial #. It has to be implemented in c, not c++. I've already browsed the api quite a lot but I'm not sure (and I don't know windows that much - I'm doing a win32 port of a linux app.) So, well, any clue would be quite appreciated...
Thanks in advance.

Dani AI

Generated

As noted, you want a machine-specific string for a Win32 port and pointed out a generated unique identifier is a common route. Below are practical Windows-side options, tradeoffs, and short C examples so the choice can be implemented cleanly.

A quick, simple source is the volume serial for the system drive (returned by GetVolumeInformation). It is easy to read from plain C and works without COM, but it changes when the disk is reformatted or the image is cloned:

#include <windows.h>
#include <stdio.h>

int main(void) {
    DWORD serial = 0, maxLen = 0, flags = 0;
    if (GetVolumeInformationA("C:\\", NULL, 0, &serial, &maxLen, &flags, NULL, 0)) {
        printf("Volume serial: %08X\n", serial);
    } else {
        printf("GetVolumeInformation failed: %lu\n", GetLastError());
    }
    return 0;
}

If you need the physical drive serial, use CreateFile on "\\.\PhysicalDriveN" and call DeviceIoControl with IOCTL_STORAGE_QUERY_PROPERTY to obtain a STORAGE_DEVICE_DESCRIPTOR; the descriptor gives a SerialNumberOffset you can read. That approach can require admin rights, may return manufacturer strings, and sometimes yields no useful serial (varies by vendor). WMI (Win32_PhysicalMedia / Win32_BIOS) can also expose BIOS or media serials but requires COM/WMI plumbing.

A robust alternative is to generate and persist an app-specific GUID on first run (CoCreateGuid or UuidCreate), then store the string in HKCU/HKLM or a file under %PROGRAMDATA% or %APPDATA%. This survives most hardware changes and avoids privacy/legal pitfalls tied to hardware identifiers. When binding to hardware for licensing, combine multiple indicators, hash them, and be explicit about failure modes (VMs, cloned drives, network adapters that can be spoofed).

Cautions: hardware IDs can change, virtualization can mask uniqueness, and some identifiers require elevation. Choose the simplest source that matches your reliability and security needs, then document how your app will detect and handle changes.

Recommended Answers

All 2 Replies

Are you looking to tie your program to that specific machine, or are you just looking for something which is unique?

If it's the latter, then consider a http://en.wikipedia.org/wiki/Uuid

Member Avatar for Member #570982

Using uuid seems the best way, thank you for your help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.