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

  • Home
  • SEARCH
  • 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 157279
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T10:28:33+00:00 2026-05-11T10:28:33+00:00

SOLVED I am using delphi 2009. My program listens for usb drives being connected

  • 0

SOLVED

I am using delphi 2009. My program listens for usb drives being connected and remove. Ive used a very similar code in 10 apps over the past year. It has always worked perfectly. When i migrated i had to give up using thddinfo to get the drive model. This has been replaced by using WMI. The WMI query requires the physical disk number and i happen to already have a function in the app for doing just that.

As i test I put this in a button and ran it and it successfully determines the psp is physical drive 4 and returns the model (all checked in the debugger and in another example using show message):

function IsPSP(Drive: String):Boolean; var Model: String; DriveNum: Byte; begin   Result := False;   Delete(Drive, 2, MaxInt);   DriveNum := GetPhysicalDiskNumber(Drive[1]);   Model := (MagWmiGetDiskModel(DriveNum));   if Pos('PSP',Model) > 0 then Result := True; end;  procedure TfrmMain.Button1Click(Sender: TObject); var DriveNum: Byte; begin   IsPSP('I'); end; 

It works perfectly that is until i allow the WMDeviceChange that ive been using for a year to call up the getphysicaldisknumber and the wmi query statement. Ive tried them by themselves theyre both a problem. GetPhysicalDiskNumber freezes real bad when its doing a CloseHandle on the logical disk but does return the number eventually. The WMI query fails with no error just returns ” debugger points into the wbemscripting_tlb where the connection just never happened. Keep in mind the only thing thats changed in a year is what im calling to get the model i was using an api call and now im using something else.

Below is the rest of the code involved at this time sans the ispsp that is displayed above:

procedure TfrmMain.WMDeviceChange(var Msg: TMessage); var Drive: String; begin   case Msg.wParam of     DBT_DeviceArrival: if PDevBroadcastHdr(Msg.lParam)^.dbcd_devicetype = DBT_DevTyp_Volume then       begin         Drive := GetDrive(PDevBroadcastVolume(Msg.lParam)) + '\';         OnDeviceInsert(Drive);       end;     DBT_DeviceRemoveComplete: if PDevBroadcastHdr(Msg.lParam)^.dbcd_devicetype = DBT_DevTyp_Volume then       begin         Drive := GetDrive(PDevBroadcastVolume(Msg.lParam)) + '\';         OnDeviceRemove(Drive);       end;   end; end;  Procedure TfrmMain.OnDeviceInsert(Drive: String); var PreviousIndex: Integer; begin   if (getdrivetype(Pchar(Drive))=DRIVE_REMOVABLE) then   begin     PreviousIndex := cbxDriveList.Items.IndexOf(cbxDriveList.Text);     cbxDriveList.Items.Append(Drive);     if PreviousIndex = -1 then //If there was no drive to begin with then set index to 0     begin       PreviousIndex := 0;       cbxDriveList.ItemIndex := 0;     end;     if isPSP(Drive) then     begin       if MessageDlg('A PSP was detect @ ' + Drive + #10#13 + 'Would you like to select this drive?',mtWarning,[mbYes,mbNo], 0) = mrYes then       cbxDriveList.ItemIndex := cbxDriveList.Items.IndexOf(Drive)       else cbxDriveList.ItemIndex := PreviousIndex;     end     else if MessageDlg('USB Drive ' + Drive + ' Detected' + #10#13 + 'Is this your target drive?',mtWarning,[mbYes,mbNo], 0) = mrYes then         cbxDriveList.ItemIndex := cbxDriveList.Items.IndexOf(Drive)     else cbxDriveList.ItemIndex := PreviousIndex;   end; end;  Procedure TfrmMain.OnDeviceRemove(Drive: String); begin   if not (getdrivetype(Pchar(Drive)) = DRIVE_CDROM) then   begin     if cbxDriveList.Text = (Drive) then ShowMessage('The selected drive (' + Drive + ') has been removed');     cbxDriveList.Items.Delete(cbxDriveList.Items.IndexOf(Drive));     if cbxDriveList.Text = '' then cbxDriveList.ItemIndex := 0;     if Drive = PSPDrive then //Check Detect PSP and remove reference if its been removed     begin       PSPDrive := '';     end;   end; end; 

Rob has said something below about im not calling the inherited message handler, ive read the document i see a couple of things i can return… but im not really sure i understand but i will look into it. Im not a very good pascal programmer but ive been learning alot. The transition to 2009 has had some rough patches as well.

The USB drive detection and all that works perfectly. If i remove the two things from is psp the user is greeted right away with wis this your whatever and adds I:\ to the list. Its just the two new things that have changed in the app that fail when called by wmdevicechange and as said before they work on their own.

EDIT – SOLVED

Alright well im using a timer as suggested and the problem seems to be solved. One note is that when called by the timer very shortly after the wmdevicechange getting the physical disk number still seems to be slow. I attribute this to the device still being attached to the system.

On that note im using a P2 450 on the regular. I hooked the PSP and app to a 1.8Ghz Dual Core Laptop and the program detected the psp and notified the user very fast. So the app wont freeze unless there on a very very slow computer and on this slow onw its only for a matter of seconds and doesnt affect the operation of the program though isnt very cool. But i feel that all modern computers will run the detection fast especially because they can attach the device alot faster.

  • 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. 2026-05-11T10:28:34+00:00Added an answer on May 11, 2026 at 10:28 am

    It’s possible that the information you’re querying becomes available only after the WMDeviceChange message handler runs. If the very same code works when called from a button, try this:

    1. Refactor your WMDeviceChange handler code into one or more separate methods.
    2. In the WMDeviceChange handler, activate a precreated timer and have it fire one second later, or something like that.
    3. Call the former WMDeviceChange handler code from the timer handler code.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 376k
  • Answers 376k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer var str1:String = "Vinoth"; var str2:String = "Babu"; var str3:String… May 14, 2026 at 8:29 pm
  • Editorial Team
    Editorial Team added an answer To delete existing cookie we actually just set its expiration… May 14, 2026 at 8:29 pm
  • Editorial Team
    Editorial Team added an answer Java only knows about the platform it is currently running… May 14, 2026 at 8:29 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.