Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to get the SerialNumber of disk driver?

How to get the SerialNumber of disk driver?

Scheduled Pinned Locked Moved C / C++ / MFC
securitycomsysadminhelptutorial
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    whiteclouds
    wrote on last edited by
    #1

    Hi all! Now I am developing an application to read the disk driver's SerialNumber. I want to read it by WMI. The code is as following:

    BOOL CData::GetSerialNumber(BYTE* out)
    {
    BOOL bRet = FALSE;
    HRESULT hr;
    hr = CoInitializeEx(0, COINIT_MULTITHREADED);
    if (FAILED(hr))
    {
    cout << "Failed to initialize COM library. Error code = 0x"
    << hex << hr << endl;
    return bRet;
    }
    hr = CoInitializeSecurity(
    NULL, // Security descriptor
    -1, // COM negotiates authentication service
    NULL, // Authentication services
    NULL, // Reserved
    RPC_C_AUTHN_LEVEL_DEFAULT, // Default authentication level for proxies
    RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation level for proxies
    NULL, // Authentication info
    EOAC_NONE, // Additional capabilities of the client or server
    NULL); // Reserved

    if (FAILED(hr))
    {
       cout << "Failed to initialize security. Error code = 0x" 
    		<< hex << hr << endl;
       CoUninitialize();
       return bRet;                  // Program has failed.
    }
    IWbemLocator \*pLoc = 0;
    
    hr = CoCreateInstance(CLSID\_WbemLocator, 0, 
        CLSCTX\_INPROC\_SERVER, IID\_IWbemLocator, (LPVOID \*) &pLoc);
    
    if (FAILED(hr))
    {
        cout << "Failed to create IWbemLocator object. Err code = 0x"
             << hex << hr << endl;
        CoUninitialize();
        return bRet;     // Program has failed.
    }
    IWbemServices \*pSvc = 0;
    
    // Connect to the root\\default namespace with the current user.
    hr = pLoc->ConnectServer(
            BSTR(L"ROOT\\\\CIMV2"), 
            NULL, NULL, 0, NULL, 0, 0, &pSvc);
    
    if (FAILED(hr))
    {
        cout << "Could not connect. Error code = 0x" 
             << hex << hr << endl;
        pLoc->Release();
        CoUninitialize();
        return bRet;      // Program has failed.
    }
    
    cout << "Connected to WMI" << endl;
    HRESULT hres;
    
    // Set the proxy so that impersonation of the client occurs.
    hres = CoSetProxyBlanket(pSvc,
       RPC\_C\_AUTHN\_WINNT,
       RPC\_C\_AUTHZ\_NONE,
       NULL,
       RPC\_C\_AUTHN\_LEVEL\_CALL,
       RPC\_C\_IMP\_LEVEL\_IMPERSONATE,
       NULL,
       EOAC\_NONE
    );
    
    if (FAILED(hres))
    {
        cout << "Could not set proxy blanket. Error c
    
    D 1 Reply Last reply
    0
    • W whiteclouds

      Hi all! Now I am developing an application to read the disk driver's SerialNumber. I want to read it by WMI. The code is as following:

      BOOL CData::GetSerialNumber(BYTE* out)
      {
      BOOL bRet = FALSE;
      HRESULT hr;
      hr = CoInitializeEx(0, COINIT_MULTITHREADED);
      if (FAILED(hr))
      {
      cout << "Failed to initialize COM library. Error code = 0x"
      << hex << hr << endl;
      return bRet;
      }
      hr = CoInitializeSecurity(
      NULL, // Security descriptor
      -1, // COM negotiates authentication service
      NULL, // Authentication services
      NULL, // Reserved
      RPC_C_AUTHN_LEVEL_DEFAULT, // Default authentication level for proxies
      RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation level for proxies
      NULL, // Authentication info
      EOAC_NONE, // Additional capabilities of the client or server
      NULL); // Reserved

      if (FAILED(hr))
      {
         cout << "Failed to initialize security. Error code = 0x" 
      		<< hex << hr << endl;
         CoUninitialize();
         return bRet;                  // Program has failed.
      }
      IWbemLocator \*pLoc = 0;
      
      hr = CoCreateInstance(CLSID\_WbemLocator, 0, 
          CLSCTX\_INPROC\_SERVER, IID\_IWbemLocator, (LPVOID \*) &pLoc);
      
      if (FAILED(hr))
      {
          cout << "Failed to create IWbemLocator object. Err code = 0x"
               << hex << hr << endl;
          CoUninitialize();
          return bRet;     // Program has failed.
      }
      IWbemServices \*pSvc = 0;
      
      // Connect to the root\\default namespace with the current user.
      hr = pLoc->ConnectServer(
              BSTR(L"ROOT\\\\CIMV2"), 
              NULL, NULL, 0, NULL, 0, 0, &pSvc);
      
      if (FAILED(hr))
      {
          cout << "Could not connect. Error code = 0x" 
               << hex << hr << endl;
          pLoc->Release();
          CoUninitialize();
          return bRet;      // Program has failed.
      }
      
      cout << "Connected to WMI" << endl;
      HRESULT hres;
      
      // Set the proxy so that impersonation of the client occurs.
      hres = CoSetProxyBlanket(pSvc,
         RPC\_C\_AUTHN\_WINNT,
         RPC\_C\_AUTHZ\_NONE,
         NULL,
         RPC\_C\_AUTHN\_LEVEL\_CALL,
         RPC\_C\_IMP\_LEVEL\_IMPERSONATE,
         NULL,
         EOAC\_NONE
      );
      
      if (FAILED(hres))
      {
          cout << "Could not set proxy blanket. Error c
      
      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      Other than strID not being null terminated, your code works fine for me.

      "One man's wage rise is another man's price increase." - Harold Wilson

      "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

      "Man who follows car will be exhausted." - Confucius

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups