shriharsha
shriharsha

Reputation:

how to get system unique id using vc++ code?

I'm new to vc++ language so I want to get system unique id using vc++ language. so please can anybody help me in coding how to get system unique id?

Upvotes: 3

Views: 3595

Answers (6)

Code Name Jack
Code Name Jack

Reputation: 3333

You can do it via WMI query to "Win32_ComputerSystemProduct" class.

HRESULT hres;

// Step 1: --------------------------------------------------
// Initialize COM. ------------------------------------------

hres = CoInitializeEx(0, COINIT_MULTITHREADED);
if (FAILED(hres)) { return 1; }

// Step 2: --------------------------------------------------
// Set general COM security levels --------------------------

hres = CoInitializeSecurity(nullptr, -1, nullptr, nullptr, RPC_C_AUTHN_LEVEL_DEFAULT, RPC_C_IMP_LEVEL_IMPERSONATE,
    nullptr, EOAC_NONE, nullptr);
if (FAILED(hres)) { return 1; }

// Step 3: ---------------------------------------------------
// Obtain the initial locator to WMI -------------------------
IWbemLocator *pLoc = nullptr;
hres = CoCreateInstance(CLSID_WbemLocator, nullptr, CLSCTX_INPROC_SERVER, IID_IWbemLocator, reinterpret_cast<LPVOID *>(&pLoc));
if (FAILED(hres)) { return 1; }

// Step 4: -----------------------------------------------------
// Connect to WMI through the IWbemLocator::ConnectServer method

IWbemServices *pSvc = nullptr;

// Connect to the root\cimv2 namespace with
// the current user and obtain pointer pSvc
// to make IWbemServices calls.
hres = pLoc->ConnectServer(_bstr_t(L"ROOT\\CIMV2"), nullptr, nullptr, nullptr, NULL, nullptr, nullptr, &pSvc);

if (FAILED(hres)) { pLoc->Release();        CoUninitialize();        return 1; }


// Step 5: --------------------------------------------------
// Set security levels on the proxy -------------------------

hres = CoSetProxyBlanket(pSvc, RPC_C_AUTHN_WINNT, RPC_C_AUTHZ_NONE, nullptr,
    RPC_C_AUTHN_LEVEL_CALL, RPC_C_IMP_LEVEL_IMPERSONATE, nullptr, EOAC_NONE);

if (FAILED(hres)) { pSvc->Release();        pLoc->Release();        CoUninitialize();        return 1; }

// Step 6: --------------------------------------------------
// Use the IWbemServices pointer to make requests of WMI ----

// For example, get the name of the operating system
IEnumWbemClassObject* pEnumerator = nullptr;
hres = pSvc->ExecQuery(
    bstr_t("WQL"),
    bstr_t("SELECT * FROM Win32_ComputerSystemProduct"),
    WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
    nullptr,
    &pEnumerator);

if (FAILED(hres)) { pSvc->Release(); pLoc->Release(); CoUninitialize(); return 1; }

// Step 7: -------------------------------------------------
// Get the data from the query in step 6 -------------------

IWbemClassObject *pclsObj = nullptr;
ULONG uReturn = 0;

HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn);
VARIANT vtProp;

// Get the value of the Name property
hr = pclsObj->Get(L"UUID", 0, &vtProp, 0, 0);
wcout << " OS Name : " << vtProp.bstrVal << endl;
VariantClear(&vtProp);

pclsObj->Release();

// Cleanup
// ========

pSvc->Release();
pLoc->Release();
pEnumerator->Release();
CoUninitialize();

return 0;  

Upvotes: 0

anon
anon

Reputation:

As I think the answers so far and the responses to them indicate, the real answer is not to write code that requires a unique system ID. This is very easy to do, in fact (as there is no such ID) it is the only sensible way to write applications. Anything that depends on drive serial number, MAC addresses etc. will inevitably break when the system is changed or upgraded.

Upvotes: 0

Joris Timmermans
Joris Timmermans

Reputation: 10988

One value that I've seen used is the hard disk volume ID of the C-drive. It will change when you swap the drive though.

Upvotes: 0

Maciek
Maciek

Reputation: 19903

IF you're looking to create a unique ID based on a specific machine, one of the ways I can think of is using good old boost.

For instance, you can look up one of the boost candidate libraries called UUID (GUID generation) and you could look up boost::filesystem. Using the filesystem you could get creation dates on some of the system files, and use those strings to generate a GUID.

Just a thought, hope it helps

  • you can find the UUID library here

    • you can find the Filesystem library documentation here

Upvotes: 1

Rich
Rich

Reputation: 5002

Why not use the MAC address from the network card. This should be unique but will change if your change the network card.

See the api GetAdaptersInfo. reference http://msdn.microsoft.com/en-us/library/aa365917(VS.85).aspx

Upvotes: 2

Blindy
Blindy

Reputation: 67574

You want to look at CoCreateGuid (http://msdn.microsoft.com/en-us/library/ms688568(VS.85).aspx).

Upvotes: 2

Related Questions