Click here to Skip to main content
15,891,841 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: How to find include directive path? Pin
Richard MacCutchan28-May-21 5:49
mveRichard MacCutchan28-May-21 5:49 
GeneralMessage Closed Pin
28-May-21 6:13
Member 1496877128-May-21 6:13 
GeneralRe: How to find include directive path? Pin
Richard MacCutchan28-May-21 6:37
mveRichard MacCutchan28-May-21 6:37 
GeneralMessage Closed Pin
28-May-21 7:20
Member 1496877128-May-21 7:20 
GeneralRe: How to find include directive path? Pin
Richard MacCutchan28-May-21 21:04
mveRichard MacCutchan28-May-21 21:04 
Questionc+ + coding Pin
javaid musa24-May-21 18:17
javaid musa24-May-21 18:17 
AnswerRe: c+ + coding Pin
CPallini24-May-21 20:00
mveCPallini24-May-21 20:00 
QuestionOptimizing this code ? Pin
Duc Axenn24-May-21 0:56
Duc Axenn24-May-21 0:56 
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.
C++
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_SHARE_WRITE | FILE_SHARE_DELETE,
    nullptr, /* no custom security */
    OPEN_EXISTING,
    FILE_FLAG_BACKUP_SEMANTICS,
    nullptr); /* template */

// --- Récupération du disque dur physique contenant le volume
// --- Get physical disk containing the volume
/*
    On demande au volume ses disques étendus, en cas de volume installé sur plusieurs disques, la
    première fois va dire la taille de la structure dont on a besoin, et la seconde donnera la
    structure.
    Puisque la plupart des volumes n'ont qu'une bande (voir raid), on peut optimiser légèrement
    pour ce cas par passer un buffer initial assez gros pour contenir une seule bande. Si ça
    fonctionne, c'est qu'il n'y a pas besoin d'essayer une seconde fois.

    Since nearly all volumes have only one extent, we can optimize slightly for that case by
    passing an initial buffer big enough to hold a single extent. If that works, then there’s no
    need to try a second time.

    (Raymond Chen)
*/

VOLUME_DISK_EXTENTS* extents = nullptr;

// Anticipate common case where there is only one extent.
VOLUME_DISK_EXTENTS singleExtent;

// But also have a place to manage allocated data.
std::unique_ptr<BYTE[]> lifetime;

DWORD bytesWritten;
if (DeviceIoControl(volumeHandle, IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS,
    nullptr, 0,
    &singleExtent, sizeof(singleExtent),
    &bytesWritten,
    nullptr))
{
    // Worked on the first try. Use the preallocated buffer.
    extents = &singleExtent;
}
else
{
    VOLUME_DISK_EXTENTS* lastQuery = &singleExtent;
    while (GetLastError() == ERROR_MORE_DATA)
    {
        // Evaluation de la requête
        assert(RTL_CONTAINS_FIELD(lastQuery, bytesWritten, NumberOfDiskExtents));

        DWORD extentCount = lastQuery->NumberOfDiskExtents;
        DWORD allocatedSize = FIELD_OFFSET(VOLUME_DISK_EXTENTS, Extents[extentCount]);
        lifetime.reset(new BYTE[allocatedSize]);
        lastQuery = (VOLUME_DISK_EXTENTS*)lifetime.get();
        if (DeviceIoControl(volumeHandle, IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS,
            nullptr, 0,
            lastQuery, allocatedSize,
            &bytesWritten,
            nullptr)) {
            extents = lastQuery;
            break;
        }
    }
}

/*
    The extents tell you which physical drives the volume draws its storage from, and which bytes
    on those physical drives are devoted to the volume. But for this exercise, we just want the
    physical drives.
 */
if (extents)
{
    // 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);


    STORAGE_PROPERTY_QUERY query{};
    query.PropertyId = StorageAdapterProperty;
    query.QueryType = PropertyStandardQuery;
    DWORD bytesWritten;
    STORAGE_ADAPTER_DESCRIPTOR result{};

    if (DeviceIoControl(driveHandle, IOCTL_STORAGE_QUERY_PROPERTY,
        &query, sizeof(query),
        &result, sizeof(result),
        &bytesWritten, nullptr))
    {
        // type 7 = usb
        printf("Interface type: %d", result.BusType);

    }
}

CloseHandle(volumeHandle);
CloseHandle(driveHandle);


At the end, the number give the port used, check Here
SuggestionRe: Optimizing this code ? Pin
Richard MacCutchan24-May-21 0:02
mveRichard MacCutchan24-May-21 0:02 
GeneralRe: Optimizing this code ? Pin
Duc Axenn24-May-21 0:56
Duc Axenn24-May-21 0:56 
SuggestionRe: Optimizing this code ? Pin
Randor 24-May-21 2:30
professional Randor 24-May-21 2:30 
GeneralRe: Optimizing this code ? Pin
Duc Axenn24-May-21 4:21
Duc Axenn24-May-21 4:21 
GeneralRe: Optimizing this code ? Pin
Duc Axenn24-May-21 8:36
Duc Axenn24-May-21 8:36 
GeneralRe: Optimizing this code ? Pin
Randor 24-May-21 14:30
professional Randor 24-May-21 14:30 
GeneralRe: Optimizing this code ? Pin
Duc Axenn24-May-21 20:41
Duc Axenn24-May-21 20:41 
GeneralRe: Optimizing this code ? Pin
Randor 25-May-21 12:25
professional Randor 25-May-21 12:25 
GeneralRe: Optimizing this code ? Pin
Duc Axenn26-May-21 2:53
Duc Axenn26-May-21 2:53 
QuestionCloud Storage Pin
Bram van Kampen23-May-21 15:10
Bram van Kampen23-May-21 15:10 
AnswerRe: Cloud Storage Pin
Victor Nijegorodov23-May-21 22:06
Victor Nijegorodov23-May-21 22:06 
GeneralRe: Cloud Storage Pin
Bram van Kampen29-May-21 14:49
Bram van Kampen29-May-21 14:49 
AnswerRe: Cloud Storage Pin
Gerry Schmitz24-May-21 7:14
mveGerry Schmitz24-May-21 7:14 
GeneralRe: Cloud Storage Pin
Bram van Kampen29-May-21 14:56
Bram van Kampen29-May-21 14:56 
AnswerRe: Cloud Storage Pin
Maximilien25-May-21 2:58
Maximilien25-May-21 2:58 
GeneralRe: Cloud Storage Pin
Bram van Kampen10-Jun-21 14:00
Bram van Kampen10-Jun-21 14:00 
Question32 and 64 bit versions of the same project Pin
Richard Andrew x6415-May-21 16:36
professionalRichard Andrew x6415-May-21 16:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.