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. Optimizing this code ?

Optimizing this code ?

Scheduled Pinned Locked Moved C / C++ / MFC
performancehelpcsharpc++com
10 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.
  • D Offline
    D Offline
    Duc Axenn
    wrote on last edited by
    #1

    Hi, i'm a beginner in c++, i have already did some little projects but there is many years and i don't remember the most of things. I'm on c#, since many years (+a sickness that ruins my life, not good for the memory). After several days of research, testing wmi, registry, c# c++/cli etc to measure performance, there was something i would find, how to remote since a drive letter (or path here) to know interface type. I finally found another articles of Raymond Chen, 90% of this code was already written by him i just made the link between and add what was missing. I understood the most of this, but i forgot a lot of syntaxes, memory management and few other things, because most of them are managed by c#. Then i would submit this code to know if there wase some things i could optimize, and most important if i correctly did things to not have an hardware problem beside. Raymond Chen's blog Futhermore i hope it will help others.

    LPCWSTR filePath = L"H:";
    HANDLE volumeHandle = NULL;
    HANDLE driveHandle = NULL;
    
    
    // Récupère le nom du volume
    // Get volume name
    TCHAR volumePath\[MAX\_PATH\]; // for expository purposes
    bool res = GetVolumePathName(filePath, volumePath, ARRAYSIZE(volumePath));
    wprintf(L"%s \\n", volumePath);
    
    /\*
    	Récupère le GUID (ne marche pas si c'est pas un disque dur local) \\\\?\\Volume{guid}\\
        Get Guid (doesn't work if it's not a local disk) \\\\?\\Volume{guid}
    \*/
    TCHAR volumeName\[MAX\_PATH\]; // for expository purposes
    GetVolumeNameForVolumeMountPoint(volumePath, volumeName, ARRAYSIZE(volumeName));
    
    wprintf(L"%s \\n", volumeName);
    
    /\* Raymond Chen's notes:
    	If you pass that path to the CreateFile function with the trailing backslash intact, then you 
        are opening a handle to the root directory of the volume.
    	If you pass that path to the CreateFile function with the trailing backslash removed, then you 
        are opening a handle to the volume itself.
    \*/
    
    // In our case, we want to open a handle to the volume itself, so we need to remove that trailing    backslash.The call to CreateFile looks like this:
    CString test = volumeName;
    test.TrimRight('\\\\');
    
    LPCWSTR volumeNameWithoutTrailingBackslash = test;
    
    // Handle sur le volume (en retirant le dernier \\)
    // Handle on volum (by removing last \\)
    volumeHandle = CreateFile(volumeNameWithoutTrailingBackslash,
    	0, /\* no special access requested \*/
    	FILE\_SHARE\_READ | FILE\_SHAR
    
    L 1 Reply Last reply
    0
    • D Duc Axenn

      Hi, i'm a beginner in c++, i have already did some little projects but there is many years and i don't remember the most of things. I'm on c#, since many years (+a sickness that ruins my life, not good for the memory). After several days of research, testing wmi, registry, c# c++/cli etc to measure performance, there was something i would find, how to remote since a drive letter (or path here) to know interface type. I finally found another articles of Raymond Chen, 90% of this code was already written by him i just made the link between and add what was missing. I understood the most of this, but i forgot a lot of syntaxes, memory management and few other things, because most of them are managed by c#. Then i would submit this code to know if there wase some things i could optimize, and most important if i correctly did things to not have an hardware problem beside. Raymond Chen's blog Futhermore i hope it will help others.

      LPCWSTR filePath = L"H:";
      HANDLE volumeHandle = NULL;
      HANDLE driveHandle = NULL;
      
      
      // Récupère le nom du volume
      // Get volume name
      TCHAR volumePath\[MAX\_PATH\]; // for expository purposes
      bool res = GetVolumePathName(filePath, volumePath, ARRAYSIZE(volumePath));
      wprintf(L"%s \\n", volumePath);
      
      /\*
      	Récupère le GUID (ne marche pas si c'est pas un disque dur local) \\\\?\\Volume{guid}\\
          Get Guid (doesn't work if it's not a local disk) \\\\?\\Volume{guid}
      \*/
      TCHAR volumeName\[MAX\_PATH\]; // for expository purposes
      GetVolumeNameForVolumeMountPoint(volumePath, volumeName, ARRAYSIZE(volumeName));
      
      wprintf(L"%s \\n", volumeName);
      
      /\* Raymond Chen's notes:
      	If you pass that path to the CreateFile function with the trailing backslash intact, then you 
          are opening a handle to the root directory of the volume.
      	If you pass that path to the CreateFile function with the trailing backslash removed, then you 
          are opening a handle to the volume itself.
      \*/
      
      // In our case, we want to open a handle to the volume itself, so we need to remove that trailing    backslash.The call to CreateFile looks like this:
      CString test = volumeName;
      test.TrimRight('\\\\');
      
      LPCWSTR volumeNameWithoutTrailingBackslash = test;
      
      // Handle sur le volume (en retirant le dernier \\)
      // Handle on volum (by removing last \\)
      volumeHandle = CreateFile(volumeNameWithoutTrailingBackslash,
      	0, /\* no special access requested \*/
      	FILE\_SHARE\_READ | FILE\_SHAR
      
      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      If this is supposed to be an alternative to Raymond Chen's article, then you should post it there.

      D 1 Reply Last reply
      0
      • L Lost User

        If this is supposed to be an alternative to Raymond Chen's article, then you should post it there.

        D Offline
        D Offline
        Duc Axenn
        wrote on last edited by
        #3

        Hi, it's not an alternative i just filled the element missing to perform what i need, 90% of the code came from Raymond Chen's blog. I've seen many people would know as me, how to find if a logical drive was usb, etc... I'm not enough good to c++ to make an article with it. I just want to verify with the community if there is no problem with the way i close handle, if there is things i didn't see. Et cetera. c++ code i made by the past was not about windows api, was just simple ui then i gone to java then c# (worst is i have a sickess doing i lost a big part of what i learnt). About the code, I saw many people using WMI but WMI could reach until 8s as i measured yesterday to retrieve information (i've hard 7 hard drive, 4 in usb 3) what is for a local application is a non-sense. Some others think DriveType is an hard disk is usb, what is false. An hard disk is considerate as "fixed" (Worse is for c# developper as me, there is no all possibilities we have with c++ except by c++/cli and it's not really a good idea to check logical drive by logical drive with c++/cli, it's slow in compareason. We need DriveInfo in c# it's slow. This is why step by step i search with c++/windows api how to do and have something quick and each time i find an algorithm doing the job (by combining, searching etc...) i post for others. Things evoluted since 2003, and i found hard to sort what it's deprecated, most of answers i found, people use workarounds. And c# get logical drives give me not at all what i need, i'm frustrated :cool:. Sorry for this long post. :java:

        L 1 Reply Last reply
        0
        • D Duc Axenn

          Hi, it's not an alternative i just filled the element missing to perform what i need, 90% of the code came from Raymond Chen's blog. I've seen many people would know as me, how to find if a logical drive was usb, etc... I'm not enough good to c++ to make an article with it. I just want to verify with the community if there is no problem with the way i close handle, if there is things i didn't see. Et cetera. c++ code i made by the past was not about windows api, was just simple ui then i gone to java then c# (worst is i have a sickess doing i lost a big part of what i learnt). About the code, I saw many people using WMI but WMI could reach until 8s as i measured yesterday to retrieve information (i've hard 7 hard drive, 4 in usb 3) what is for a local application is a non-sense. Some others think DriveType is an hard disk is usb, what is false. An hard disk is considerate as "fixed" (Worse is for c# developper as me, there is no all possibilities we have with c++ except by c++/cli and it's not really a good idea to check logical drive by logical drive with c++/cli, it's slow in compareason. We need DriveInfo in c# it's slow. This is why step by step i search with c++/windows api how to do and have something quick and each time i find an algorithm doing the job (by combining, searching etc...) i post for others. Things evoluted since 2003, and i found hard to sort what it's deprecated, most of answers i found, people use workarounds. And c# get logical drives give me not at all what i need, i'm frustrated :cool:. Sorry for this long post. :java:

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Duc Axenn wrote:

          I just want to verify with the community if there is no problem

          The code is technically wrong. But don't worry... it's probably correct 99.99% of the time. :) Did you write this code? The top half seems to be written by someone who knows that the file can span across multiple physical disks. The bottom half is incorrectly returning the STORAGE_BUS_TYPE of only the last disk extent. Microsoft Windows supports spanned volumes[^] which means that in very rare cases a single file might be located on multiple physical disks. Those physical drives could be a mixture of SCSI, SATA, USB, ATAPI, NVME, etcetera... To make your code more correct you would need to loop through all of those disk extents and return all of the storage bus types[^]. Best Wishes, -David Delaune edit: fixed typo

          D 2 Replies Last reply
          0
          • L Lost User

            Duc Axenn wrote:

            I just want to verify with the community if there is no problem

            The code is technically wrong. But don't worry... it's probably correct 99.99% of the time. :) Did you write this code? The top half seems to be written by someone who knows that the file can span across multiple physical disks. The bottom half is incorrectly returning the STORAGE_BUS_TYPE of only the last disk extent. Microsoft Windows supports spanned volumes[^] which means that in very rare cases a single file might be located on multiple physical disks. Those physical drives could be a mixture of SCSI, SATA, USB, ATAPI, NVME, etcetera... To make your code more correct you would need to loop through all of those disk extents and return all of the storage bus types[^]. Best Wishes, -David Delaune edit: fixed typo

            D Offline
            D Offline
            Duc Axenn
            wrote on last edited by
            #5

            thanks for your answer i was pretty sure to have an answer like that, i asked myself for the special cases. As i said most of the code is not mine, it began by an example about path, i will modify function to ask the path but it is the purpose. I was reading a code here in vba yesterday, that uses also the CreateFile instruction when suddenly i remembered about extents ... But i didn't know it was possible to mix with different ports because of the controler. But i forget about virtual raid and some other things... I think about the same problem will come in the other case (i didn't finish this part) if i begin by list all physical drive => volume => drive letter. In my case if i take in charge your advice, enumerate all as you said behind with this information the function calling will have to manage according of its own purpose. Staying neutral by just transmit ports used... And you are right, i was so happy to get the port that i totally forgot about the case you discribed. Big thanks :thumbsup:, but i hate you as much i like you :laugh: ... i must work more in c++ now :-D . If you see anything else i made wrong i will happy to get more advices. Best regards.

            1 Reply Last reply
            0
            • L Lost User

              Duc Axenn wrote:

              I just want to verify with the community if there is no problem

              The code is technically wrong. But don't worry... it's probably correct 99.99% of the time. :) Did you write this code? The top half seems to be written by someone who knows that the file can span across multiple physical disks. The bottom half is incorrectly returning the STORAGE_BUS_TYPE of only the last disk extent. Microsoft Windows supports spanned volumes[^] which means that in very rare cases a single file might be located on multiple physical disks. Those physical drives could be a mixture of SCSI, SATA, USB, ATAPI, NVME, etcetera... To make your code more correct you would need to loop through all of those disk extents and return all of the storage bus types[^]. Best Wishes, -David Delaune edit: fixed typo

              D Offline
              D Offline
              Duc Axenn
              wrote on last edited by
              #6

              Seriously... i hate myself, I was so sure that it will be something weird that i 've make a quick refresh of my knowledges on c++, as i said there was years i touch it. I took some notes for the future this time, i'm pretty sure i have already dit it but i don't remember where are this notes... Thanks to that damn sickness. I can't verify for a spanned volume, if somebody can it will great, last time i tried JODB it was on a netgear NAS that doesn't want anything else except it or raid 1 for the two bay it has. I tried JODB then one of the drives failed i understood how life could be fun... For the code... if you could give me your feedback, because i recognize it's a bit special to understand how DeviceIOControl works with IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS, but finally.. basically by running in debug mode i explored content of extents and realized there were an array... *ù*ù$^% of me. What could say QBert ? "@!#@?!" or something like these. I was so sure to found something weird, another handle with a special iteration behind... No. Then i modified the part at the end, i let the if conditionnal to avoid problems in case of i retrieve nothing. I don't know if c++ allow to use something like extents?.Extents.Any(). Is there something near that in c++ ?

              if (extents)
              {
              vector interfaces;
              for (DWORD i = 0; i < extents->NumberOfDiskExtents; i++)
              {
              // process the extents
              int physicalDriveNumber = extents->Extents->DiskNumber;

              		/\*
              			Once you have the physical drive numbers, you can convert them to physical drive  
                          handles by building a path of the form
              			\\\\.\\PhysicalDrive# where the # is the decimal expansion of the drive number.
              		\*/
              
              		wchar\_t physicalDrivePath\[80\];
              		swprintf\_s(physicalDrivePath, L"\\\\\\\\.\\\\PhysicalDrive%d", physicalDriveNumber);
              		driveHandle = CreateFile(physicalDrivePath,
              			0, FILE\_SHARE\_READ | FILE\_SHARE\_WRITE | FILE\_SHARE\_DELETE,
              			nullptr, OPEN\_EXISTING, 0, nullptr);
              
              		interfaces.push\_back( GetInterfaceType(driveHandle));
              	}
              
              	return interfaces;
              }
              

              In the example i stay with int values that could be used as i want, i'm pretty sure i will stay on c#, i want to enhance an existing explorer system i made for my other applications and i don't know for the while how to call a windows written with something else than winform. I don't even know if it's possible. Do you think it's ok now ? I'm very dumb to didn't see it at the beginning. Edit --- (Thanks to Randor)

              L 1 Reply Last reply
              0
              • D Duc Axenn

                Seriously... i hate myself, I was so sure that it will be something weird that i 've make a quick refresh of my knowledges on c++, as i said there was years i touch it. I took some notes for the future this time, i'm pretty sure i have already dit it but i don't remember where are this notes... Thanks to that damn sickness. I can't verify for a spanned volume, if somebody can it will great, last time i tried JODB it was on a netgear NAS that doesn't want anything else except it or raid 1 for the two bay it has. I tried JODB then one of the drives failed i understood how life could be fun... For the code... if you could give me your feedback, because i recognize it's a bit special to understand how DeviceIOControl works with IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS, but finally.. basically by running in debug mode i explored content of extents and realized there were an array... *ù*ù$^% of me. What could say QBert ? "@!#@?!" or something like these. I was so sure to found something weird, another handle with a special iteration behind... No. Then i modified the part at the end, i let the if conditionnal to avoid problems in case of i retrieve nothing. I don't know if c++ allow to use something like extents?.Extents.Any(). Is there something near that in c++ ?

                if (extents)
                {
                vector interfaces;
                for (DWORD i = 0; i < extents->NumberOfDiskExtents; i++)
                {
                // process the extents
                int physicalDriveNumber = extents->Extents->DiskNumber;

                		/\*
                			Once you have the physical drive numbers, you can convert them to physical drive  
                            handles by building a path of the form
                			\\\\.\\PhysicalDrive# where the # is the decimal expansion of the drive number.
                		\*/
                
                		wchar\_t physicalDrivePath\[80\];
                		swprintf\_s(physicalDrivePath, L"\\\\\\\\.\\\\PhysicalDrive%d", physicalDriveNumber);
                		driveHandle = CreateFile(physicalDrivePath,
                			0, FILE\_SHARE\_READ | FILE\_SHARE\_WRITE | FILE\_SHARE\_DELETE,
                			nullptr, OPEN\_EXISTING, 0, nullptr);
                
                		interfaces.push\_back( GetInterfaceType(driveHandle));
                	}
                
                	return interfaces;
                }
                

                In the example i stay with int values that could be used as i want, i'm pretty sure i will stay on c#, i want to enhance an existing explorer system i made for my other applications and i don't know for the while how to call a windows written with something else than winform. I don't even know if it's possible. Do you think it's ok now ? I'm very dumb to didn't see it at the beginning. Edit --- (Thanks to Randor)

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                Hi, The only problem I see this new code snippet is that NumberOfDiskExtents is DWORD (unsigned long) and your for-loop conditional is signed. It's not a big deal but code reviewers might reject it. You didn't show us the definition of your GetInterfaceType() function but I have guessed it just does the STORAGE_PROPERTY_QUERY and returns the STORAGE_BUS_TYPE. I did see some other issues in your original top post but I refrained mentioning them. You should avoid mixing TCHAR with both CString and LPCWSTR but rather choose Unicode or MBCS and use the same character set throughout your entire project when possible. Text and Strings in Visual C++[^]

                Duc Axenn wrote:

                Do you think it's ok now?

                Yes, it works. I compiled it and it found my SATA and NVMe volumes.

                Duc Axenn wrote:

                I'm very dumb to didn't see it at the beginning.

                You seem like a fast learner, reading code that Raymond Chen wrote; understanding what it does and then extended it. That's fantastic for a beginner. Congratulations and keep up the good work. Best Wishes, -David Delaune

                D 1 Reply Last reply
                0
                • L Lost User

                  Hi, The only problem I see this new code snippet is that NumberOfDiskExtents is DWORD (unsigned long) and your for-loop conditional is signed. It's not a big deal but code reviewers might reject it. You didn't show us the definition of your GetInterfaceType() function but I have guessed it just does the STORAGE_PROPERTY_QUERY and returns the STORAGE_BUS_TYPE. I did see some other issues in your original top post but I refrained mentioning them. You should avoid mixing TCHAR with both CString and LPCWSTR but rather choose Unicode or MBCS and use the same character set throughout your entire project when possible. Text and Strings in Visual C++[^]

                  Duc Axenn wrote:

                  Do you think it's ok now?

                  Yes, it works. I compiled it and it found my SATA and NVMe volumes.

                  Duc Axenn wrote:

                  I'm very dumb to didn't see it at the beginning.

                  You seem like a fast learner, reading code that Raymond Chen wrote; understanding what it does and then extended it. That's fantastic for a beginner. Congratulations and keep up the good work. Best Wishes, -David Delaune

                  D Offline
                  D Offline
                  Duc Axenn
                  wrote on last edited by
                  #8

                  Thanks i will read all of that, it's not as simply as c# ^^.(i must remember DWORD is unsigned, it's more simple with c#, with int or uint,...). I just saw it's a byte value furthermore. It's true for the function. I think to myself it was implicitly said, but it's true that for the one that never seen it before i should think to put the function. I was working about how use handle and reference, it's more easy in c# about that.

                  L 1 Reply Last reply
                  0
                  • D Duc Axenn

                    Thanks i will read all of that, it's not as simply as c# ^^.(i must remember DWORD is unsigned, it's more simple with c#, with int or uint,...). I just saw it's a byte value furthermore. It's true for the function. I think to myself it was implicitly said, but it's true that for the one that never seen it before i should think to put the function. I was working about how use handle and reference, it's more easy in c# about that.

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #9

                    Duc Axenn wrote:

                    Thanks

                    You are welcome. Not sure if you know this... but the username you are using on codeproject perfectly translates to 'Master/Leader of asking questions' in Middle English. Even the alveolarized double nn is period and would indicate that the vowel e is pronounced as a monophthong. It's probably just a coincidence. But I thought I would mention it. :) Best Wishes, -David Delaune

                    D 1 Reply Last reply
                    0
                    • L Lost User

                      Duc Axenn wrote:

                      Thanks

                      You are welcome. Not sure if you know this... but the username you are using on codeproject perfectly translates to 'Master/Leader of asking questions' in Middle English. Even the alveolarized double nn is period and would indicate that the vowel e is pronounced as a monophthong. It's probably just a coincidence. But I thought I would mention it. :) Best Wishes, -David Delaune

                      D Offline
                      D Offline
                      Duc Axenn
                      wrote on last edited by
                      #10

                      lol , i didn't know it, it's cool to learn something new. I took this username on google with reference to The big Lebowski's movie, i'm pretty sure it's a wrong translation. Perhaps it's dude in english but in french he wants to be called The Duc, what is a total paradox with all of he is. It's true that it could interpreted as something pretentious, i'm happy you understood it was not at all for this reason. It's interesting to discover a particularity of another language. Thanks for all, sinceraly. - Alexandre CODOUL.

                      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