Page 1 of 2

Detecting Memory Card inserts

Posted: Sat Oct 22, 2011 9:14 pm
by Thunder93
Hi folks,

I was looking on here for code to demonstrate how to detect when inserting a memory card .. to no avail, any help be much appreciated.

Re: Detecting Memory Card inserts

Posted: Sat Oct 22, 2011 10:02 pm
by ts-soft

Re: Detecting Memory Card inserts

Posted: Sat Oct 22, 2011 10:22 pm
by RASHAD

Code: Select all


Procedure USB_CBack(hwnd, uMsg, wParam, lParam)
 result = #PB_ProcessPureBasicEvents
   Select uMsg
       Case #WM_DEVICECHANGE
          Select wParam
             Case #DBT_DEVICEARRIVAL
                MessageRequester("USB Status :","USB Device Inserted",#PB_MessageRequester_Ok)
                
             Case #DBT_DEVICEREMOVECOMPLETE
                MessageRequester("USB Status :","USB Device Removed",#PB_MessageRequester_Ok)
                
          EndSelect
   EndSelect
ProcedureReturn result 
EndProcedure

OpenWindow(0,0,0,300,300,"Test",#PB_Window_SystemMenu|#PB_Window_Minimize)

SetWindowCallback(@USB_CBack())


Repeat
  Select WaitWindowEvent()
      Case #PB_Event_CloseWindow
        Quit = 1
   EndSelect
Until Quit = 1

Re: Detecting Memory Card inserts

Posted: Sat Oct 22, 2011 10:37 pm
by Thunder93
Thanks you two! However, none of those will work for a card reader.

Re: Detecting Memory Card inserts

Posted: Sat Oct 22, 2011 10:45 pm
by ts-soft
You are right, insert a card in the cardreader doesn't make a change, only insert/remove the reader itself.

Re: Detecting Memory Card inserts

Posted: Sat Oct 22, 2011 10:52 pm
by Thunder93
I have also a portable USB Card reader device, but my intent is to detect cards being inserted into my laptops, netbook and desktop internal card readers.

Re: Detecting Memory Card inserts

Posted: Sat Oct 22, 2011 10:57 pm
by Ramihyn_
I had a lot trouble detecting card reader changes myself due to the different types of readers, media and their use. It really depends on what you want to achieve - remember that you can have memory cards which have no partition at all and also you can have memory cards with multiple partitions on them.

What do you really want to achieve? Detecting the inserted memory card or actually detecting the newly available filesystems on the cards?

Re: Detecting Memory Card inserts

Posted: Sat Oct 22, 2011 11:06 pm
by Thunder93
Simply to detect when the card is inserted, not worried about the filesystems.

Re: Detecting Memory Card inserts

Posted: Sat Oct 22, 2011 11:18 pm
by RASHAD
Card Readers are using special hardware so it uses special device drivers
In that case you can use the device driver API ( Provided by the manufacturer )
Or I know that when you insert a card it will have a logical drive letter(Name)
So if you can detect if a new logical drive just added or removed

It just an idea

Re: Detecting Memory Card inserts

Posted: Sun Oct 23, 2011 12:00 am
by Thunder93
I have a card reader for a desktop PC installed like a floppy disk drive, it has support for several different types of memory cards, it plugs to USB2 on the motherboard, no driver disk needed.

It creates several different entries under ‘Devices with Removal Storage’, when you insert a memory card, the AutoPlay screen appears and you can access the media.

Re: Detecting Memory Card inserts

Posted: Sun Oct 23, 2011 12:08 am
by Ramihyn_
Pretty much all card readers are connected as internal USB storage media or externally via USB.

Re: Detecting Memory Card inserts

Posted: Sun Oct 23, 2011 5:31 pm
by Thunder93
It was mentioned that I should be able to achieve this by monitoring for a WMI Win32_DeviceChangeEvent, or RegisterDeviceNotification.
• Win32_DeviceChangeEvent: http://msdn.microsoft.com/en-us/library ... S.85).aspx
• RegisterDeviceNotification: http://msdn.microsoft.com/en-us/library ... S.85).aspx

beyond my skill level.

Anyone know how to hook into Shell Hardware detection? heh

Re: Detecting Memory Card inserts

Posted: Mon Oct 24, 2011 2:23 am
by Thunder93
Not sure how to convert it over to PB properly, but a step in the right direction.

Using the shell to receive notification of removable media being inserted or removed. - http://www.codeproject.com/KB/shell/shc ... ister.aspx

Re: Detecting Memory Card inserts

Posted: Tue Oct 25, 2011 9:07 pm
by Shardik
This code example detects insertion and removal of an SD card on my
iMac running Windows 7 x64 with PB 4.51 x86 and PB 4.51 x64 and on
a standard PC with Windows XP SP2:

Code: Select all

; Converted from
; "Using the shell to receive notification of removable media being inserted
; or removed" by Obliterator
; http://www.codeproject.com/KB/shell/shchangenotifyregister.aspx

EnableExplicit

#SHCNE_DISKEVENTS    = $2381F
#SHCNE_MEDIAINSERTED = $20
#SHCNE_MEDIAREMOVED  = $40
#WM_USER_MEDIACHANGED = #WM_USER + 1

Structure SHChangeNotifyEntry
  pidl.I
  fRecursive.I
EndStructure

Structure SHNotifyStruct
  dwItem1.I
  dwItem2.I
EndStructure

Procedure.S GetDrivePath(*ItemIDList)
  Protected DrivePath.S = Space(#MAX_PATH)

  SHGetPathFromIDList_(*ItemIDList, @DrivePath)

  ProcedureReturn DrivePath
EndProcedure

Procedure WindowCallback(WindowHandle, Msg, WParam, LParam)
  Protected DrivePath.S
  Protected *SHNotify.SHNotifyStruct

  If Msg = #WM_USER_MEDIACHANGED
    Select LParam
      Case #SHCNE_MEDIAINSERTED
        *SHNotify = WParam
        MessageRequester("Info", "Media inserted into " + GetDrivePath(*SHNotify\dwItem1), #MB_ICONINFORMATION)
      Case #SHCNE_MEDIAREMOVED
        *SHNotify = WParam
        MessageRequester("Info", "Media removed from " + GetDrivePath(*SHNotify\dwItem1), #MB_ICONINFORMATION)
    EndSelect
  EndIf

  ProcedureReturn #PB_ProcessPureBasicEvents
EndProcedure

Define ChangeNotifyEntry.SHChangeNotifyEntry
Define ItemIDListPointer.I
Define RegistrationID.I

Dim ChangeNotification(0)

OpenWindow(0, 0, 0, 280, 100, "Detect insertion of removable media", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
SetWindowCallback(@WindowCallback(), 0)

If SHGetSpecialFolderLocation_(WindowID(0), #CSIDL_DESKTOP, @ItemIDListPointer) = #S_OK
  ChangeNotifyEntry\pidl = ItemIDListPointer
  ChangeNotifyEntry\fRecursive = #True

  RegistrationID = SHChangeNotifyRegister_(WindowID(0), #SHCNE_DISKEVENTS, #SHCNE_MEDIAINSERTED | #SHCNE_MEDIAREMOVED, #WM_USER_MEDIACHANGED, 1, @ChangeNotifyEntry)
  
  If RegistrationID
    Repeat
    Until WaitWindowEvent() = #PB_Event_CloseWindow
    
    SHChangeNotifyDeregister_(RegistrationID)
  EndIf
EndIf

Re: Detecting Memory Card inserts

Posted: Tue Oct 25, 2011 10:55 pm
by Thunder93
Absolutely wonderful! Thank you very much Shardik!!

This is what I like about the PureBasic product, the PB community are surrounded by so many knowledgeable people who always trying to help others, even with things that seems trivia.

btw; running Windows 7 x64 here, and the converted code works flawlessly. :o :D