Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 640233
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:55:51+00:00 2026-05-13T20:55:51+00:00

Background I’ve been using Win32_DiskDrive to find flash memory (usb pens, SD cards, etc.),

  • 0

Background
I’ve been using Win32_DiskDrive to find flash memory (usb pens, SD cards, etc.), but after some tests on other computers I noticed that they weren’t always discovered. So I am using Win32_LogicalDisk and since it has DriveType I don’t have to associate with two classes (e.g. partition) to find first the drives then their drive letters.

The problem is that external harddrives are detected as DriveType 3 (Local Disk) in LogicalDisk and doesn’t have 7 (Supports Removable Media) in Capabilities in DiskDrive. So I can’t tell the difference between an internal and external drive.

Question
How do I tell the difference between an internal and an external harddrive using LogicalDisk (or DiskDrive if you really have to) or something third.

Alright. The question has been answered!
Here’s the code, if anyone is interested.

program GetWMI_USBConnectedInfo;

{$APPTYPE CONSOLE}

uses
  Windows,
  Classes,
  ActiveX,
  Variants,
  SysUtils,
  WbemScripting_TLB, // Using the .pas supplied by the wrapper as it seems to be the XP version of 1.2
  magwmi,
  magsubs1;

function CheckType(Str: string): boolean;
var
  I: Integer;
  Str2: string;
begin
  Result := False;
  for I := 1 to Length(Str) - 1 do if Str[I] = '\' then begin
    Str2 := Copy(Str, 1, I-1);
    Str2 := LowerCase(Str2);
    if (Str2 = 'usbstor') or (Str2 = 'flashmedia') then
      Result := True;
    Break;
  end;
end;

procedure  GetUSBDiskDriveInfo;
var
  I, II, III:  Integer;
  Start, Stop, Freq: Int64;
  instances, instances2, instances3: integer ;
  WmiResults, WmiResults2, WmiResults3: T2DimStrArray ;
  errstr: string ;
begin
  QueryPerformanceFrequency(Freq);
  QueryPerformanceCounter(Start);
  try
    MagWmiGetInfoEx('.', 'root\CIMV2', '', '', 'SELECT * FROM Win32_DiskDrive', WmiResults, instances, errstr);
    for I := 1 to instances do begin
      MagWmiGetInfoEx('.', 'root\CIMV2', '', '', 'ASSOCIATORS OF {Win32_DiskDrive.DeviceID=''' + WmiResults[I, 12] + '''} WHERE AssocClass = Win32_DiskDriveToDiskPartition', WmiResults2, instances2, errstr);
      for II := 1 to instances2 do begin
        MagWmiGetInfoEx('.', 'root\CIMV2', '', '', 'ASSOCIATORS OF {Win32_DiskPartition.DeviceID=''' + WmiResults2[II, 11] + '''} WHERE AssocClass = Win32_LogicalDiskToPartition', WmiResults3, instances3, errstr);
        for III := 1 to instances3 do begin
          if CheckType(WmiResults[I, 32]) or (Pos('7', WmiResults[I, 3])>0) then begin
            Write(WmiResults3[III, 4]);
            Write(WmiResults3[III, 39]);
            Writeln;
          end;
        end;
        WmiResults3 := nil;
      end;
      WmiResults2 := nil;
    end;
    WmiResults := nil;
  except
    Writeln;
    Writeln('error: '+errstr);
  end;
  Writeln;
  QueryPerformanceCounter(Stop);
  if (Freq > 0) then
    Writeln('It took ' + FormatFloat('0.#0', (Stop-Start) / Freq) + ' seconds to complete.');
end;

begin
  try
    CoInitialize(nil);
    GetUSBDiskDriveInfo;
    Readln;
    CoUninitialize;
  except
    on E:Exception do begin
      CoUninitialize;
      Writeln(E.Classname, ': ', E.Message);
      Readln;
    end;
  end;
end.

One more thing!
Call this a dirty hack or whatever, but I commented out this part of MagWmiGetInfoEx (line 298 in magwmi) in order to make it work:

//        if Pos ('SELECT', Arg) = 1 then
            wmiObjectSet := wmiServices.ExecQuery (Arg, 'WQL', wbemFlagReturnImmediately, nil)
//        else
//            wmiObjectSet := wmiServices.InstancesOf (Arg, wbemFlagReturnImmediately or
//                                                             wbemQueryFlagShallow, nil)
;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T20:55:52+00:00Added an answer on May 13, 2026 at 8:55 pm

    I would suggest sticking with WMI. There is a good delphi wrapper available which includes full source to get you started.

    A query to get you started is “SELECT * FROM WIN32_DiskDrive” which would return all of the information for all of the disk drives in your system. the PNPDeviceID field should start with USBSTOR for any USB drives. A good resource for what fields come back is the MSDN website. Just translate the objects into queries.

    If your going to be calling this from a thread, you may need to add initialize COM (ComInitialize) before making any calls. Before destroying your thread, call ComUnitialialize.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Background: I'm using the (fantastic) Vim plugin python-mode , which includes the pep8 linter.
Background - I am using paramiko to put files on a bunch of remote
Background : I'm trying to convert some JavaScript code which uses the the Crossfilter
Background: Using unix, codeigniter from localhost. I'd like to run a controllers via a
Background: I am developing an app which sends an SMS to users after registration
BACKGROUND I'm using VS 2010 on a machine where I installed .Net 4.5 which
Background: I have some existing apps in the App Store and I have just
Background: I am getting a Internal Server 500 24 50 error after deploying an
Background: I am using Eclipse to develop an Android app. I have an xml
Background: I am writing a flash game and have encountered a problem. I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.