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. C++ HDD Serial Number

C++ HDD Serial Number

Scheduled Pinned Locked Moved C / C++ / MFC
c++tutorial
8 Posts 5 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.
  • A Offline
    A Offline
    anwar1026
    wrote on last edited by
    #1

    I want to know how to find the HDD Serial Number from using C++ or C or any assembly code. Thanks, Anwar.

    S L V 3 Replies Last reply
    0
    • A anwar1026

      I want to know how to find the HDD Serial Number from using C++ or C or any assembly code. Thanks, Anwar.

      S Offline
      S Offline
      Sarath C
      wrote on last edited by
      #2

      This one might help you[^] SaRath.
      "Don't Do Different things... Do Things Differently..." Understanding State Pattern in C++

      1 Reply Last reply
      0
      • A anwar1026

        I want to know how to find the HDD Serial Number from using C++ or C or any assembly code. Thanks, Anwar.

        L Offline
        L Offline
        Laxman Auti
        wrote on last edited by
        #3

        anwar1026 wrote:

        I want to know how to find the HDD Serial Number from using C++ or C or any assembly code.

        Check GetVolumeInformation() API where you get the information of the volume serial number in lpVolumeSerialNumber parameter. Here you get more information[^] Knock out 't' from can't, You can if you think you can :cool: -- modified at 8:01 Friday 9th June, 2006

        1 Reply Last reply
        0
        • A anwar1026

          I want to know how to find the HDD Serial Number from using C++ or C or any assembly code. Thanks, Anwar.

          V Offline
          V Offline
          Viorel
          wrote on last edited by
          #4

          If you need volume's serial number (for logical disks C:, D:, etc.), you can use GetVolumeInformation function. If you need serial numbers of your hard disks as physical devices, you can follow the famous Windows Management Instrumentation (WMI) way, which does not look so simple:

          #define _WIN32_WINNT 0x0400
          #include <objbase.h>
          #include <comdef.h>
          #include <Wbemidl.h>
          
          # pragma comment(lib, "wbemuuid.lib")
          
          _COM_SMARTPTR_TYPEDEF(IWbemLocator, __uuidof(IWbemLocator));
          _COM_SMARTPTR_TYPEDEF(IWbemServices, __uuidof(IWbemServices));
          _COM_SMARTPTR_TYPEDEF(IEnumWbemClassObject, __uuidof(IEnumWbemClassObject));
          _COM_SMARTPTR_TYPEDEF(IWbemClassObject, __uuidof(IWbemClassObject));
          
          
          int main()
          {
          
              CoInitializeEx(NULL, COINIT_MULTITHREADED);
              CoInitializeSecurity(
                  NULL,
                  -1,
                  NULL,
                  NULL,
                  RPC_C_AUTHN_LEVEL_DEFAULT,
                  RPC_C_IMP_LEVEL_IMPERSONATE,
                  NULL,
                  EOAC_NONE,
                  NULL);
          
              IWbemLocatorPtr locator;
          
              CoCreateInstance(
                  CLSID_WbemLocator,             
                  0, 
                  CLSCTX_INPROC_SERVER, 
                  IID_IWbemLocator, 
                  (LPVOID *)&locator
                  );
          
              IWbemServicesPtr services;
          
              locator->ConnectServer(
                  _bstr_t(L"ROOT\\CIMV2"),
                  NULL,
                  NULL,
                  0,
                  NULL,
                  0,
                  0,
                  &services
                  );
          
          
              IEnumWbemClassObjectPtr enumerator;
              services->ExecQuery(
                  bstr_t("WQL"), 
                  bstr_t("SELECT * FROM Win32_DiskDrive"),
                  WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, 
                  NULL,
                  &enumerator);
          
              IWbemClassObjectPtr object = 000;
          
              for( ; ; )
              {
                  ULONG ret;
                  enumerator->Next(WBEM_INFINITE, 1, &object, &ret);
          
                  if( ret == 0) break;
          
                  _variant_t variant;
          
                  object->Get(L"Caption", 0, &variant, 0, 0);
          
                  MessageBox(0, _bstr_t(variant), "The serial number is:", MB_OK);
              }
          
              return 0;
              //
              // TODO: check any call for errors
              //
          }
          

          Hope it helps.

          A 1 Reply Last reply
          0
          • V Viorel

            If you need volume's serial number (for logical disks C:, D:, etc.), you can use GetVolumeInformation function. If you need serial numbers of your hard disks as physical devices, you can follow the famous Windows Management Instrumentation (WMI) way, which does not look so simple:

            #define _WIN32_WINNT 0x0400
            #include <objbase.h>
            #include <comdef.h>
            #include <Wbemidl.h>
            
            # pragma comment(lib, "wbemuuid.lib")
            
            _COM_SMARTPTR_TYPEDEF(IWbemLocator, __uuidof(IWbemLocator));
            _COM_SMARTPTR_TYPEDEF(IWbemServices, __uuidof(IWbemServices));
            _COM_SMARTPTR_TYPEDEF(IEnumWbemClassObject, __uuidof(IEnumWbemClassObject));
            _COM_SMARTPTR_TYPEDEF(IWbemClassObject, __uuidof(IWbemClassObject));
            
            
            int main()
            {
            
                CoInitializeEx(NULL, COINIT_MULTITHREADED);
                CoInitializeSecurity(
                    NULL,
                    -1,
                    NULL,
                    NULL,
                    RPC_C_AUTHN_LEVEL_DEFAULT,
                    RPC_C_IMP_LEVEL_IMPERSONATE,
                    NULL,
                    EOAC_NONE,
                    NULL);
            
                IWbemLocatorPtr locator;
            
                CoCreateInstance(
                    CLSID_WbemLocator,             
                    0, 
                    CLSCTX_INPROC_SERVER, 
                    IID_IWbemLocator, 
                    (LPVOID *)&locator
                    );
            
                IWbemServicesPtr services;
            
                locator->ConnectServer(
                    _bstr_t(L"ROOT\\CIMV2"),
                    NULL,
                    NULL,
                    0,
                    NULL,
                    0,
                    0,
                    &services
                    );
            
            
                IEnumWbemClassObjectPtr enumerator;
                services->ExecQuery(
                    bstr_t("WQL"), 
                    bstr_t("SELECT * FROM Win32_DiskDrive"),
                    WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, 
                    NULL,
                    &enumerator);
            
                IWbemClassObjectPtr object = 000;
            
                for( ; ; )
                {
                    ULONG ret;
                    enumerator->Next(WBEM_INFINITE, 1, &object, &ret);
            
                    if( ret == 0) break;
            
                    _variant_t variant;
            
                    object->Get(L"Caption", 0, &variant, 0, 0);
            
                    MessageBox(0, _bstr_t(variant), "The serial number is:", MB_OK);
                }
            
                return 0;
                //
                // TODO: check any call for errors
                //
            }
            

            Hope it helps.

            A Offline
            A Offline
            anwar1026
            wrote on last edited by
            #5

            I cannot find this file Wbemidl.h Anwar.

            V D 2 Replies Last reply
            0
            • A anwar1026

              I cannot find this file Wbemidl.h Anwar.

              V Offline
              V Offline
              Viorel
              wrote on last edited by
              #6

              The "WbemIdl.h" file is located in "Vc*\PlatformSDK\Include\WbemIdl.h" subdirectory of Visual Studio 2003 or 2005. Are you using another compiler? Or may be you have a reduced variant of Visual Studio. WMI includes more header files, libraries and DLLs. In Windows 98, WMI has to be installed separately. I think you would have to download WMI from Microsoft site.

              A 1 Reply Last reply
              0
              • A anwar1026

                I cannot find this file Wbemidl.h Anwar.

                D Offline
                D Offline
                David Crow
                wrote on last edited by
                #7

                It's part of the Platform SDK.


                "The largest fire starts but with the smallest spark." - David Crow

                "Judge not by the eye but by the heart." - Native American Proverb

                1 Reply Last reply
                0
                • V Viorel

                  The "WbemIdl.h" file is located in "Vc*\PlatformSDK\Include\WbemIdl.h" subdirectory of Visual Studio 2003 or 2005. Are you using another compiler? Or may be you have a reduced variant of Visual Studio. WMI includes more header files, libraries and DLLs. In Windows 98, WMI has to be installed separately. I think you would have to download WMI from Microsoft site.

                  A Offline
                  A Offline
                  anwar1026
                  wrote on last edited by
                  #8

                  Well... I am using Visual Studio 6.0 And it would not be possible for me to move to Visual Studio .NET 2003 or higher. Anwar.

                  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