Detecting Memory Card inserts

Just starting out? Need help? Post your questions and find answers here.
User avatar
Thunder93
Addict
Addict
Posts: 1788
Joined: Tue Mar 21, 2006 12:31 am
Location: Canada

Detecting Memory Card inserts

Post 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.
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: Detecting Memory Card inserts

Post by ts-soft »

PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 5001
Joined: Sun Apr 12, 2009 6:27 am

Re: Detecting Memory Card inserts

Post 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
Egypt my love
User avatar
Thunder93
Addict
Addict
Posts: 1788
Joined: Tue Mar 21, 2006 12:31 am
Location: Canada

Re: Detecting Memory Card inserts

Post by Thunder93 »

Thanks you two! However, none of those will work for a card reader.
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: Detecting Memory Card inserts

Post by ts-soft »

You are right, insert a card in the cardreader doesn't make a change, only insert/remove the reader itself.
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
User avatar
Thunder93
Addict
Addict
Posts: 1788
Joined: Tue Mar 21, 2006 12:31 am
Location: Canada

Re: Detecting Memory Card inserts

Post 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.
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
Ramihyn_
Enthusiast
Enthusiast
Posts: 314
Joined: Fri Feb 24, 2006 9:40 am

Re: Detecting Memory Card inserts

Post 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?
User avatar
Thunder93
Addict
Addict
Posts: 1788
Joined: Tue Mar 21, 2006 12:31 am
Location: Canada

Re: Detecting Memory Card inserts

Post by Thunder93 »

Simply to detect when the card is inserted, not worried about the filesystems.
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 5001
Joined: Sun Apr 12, 2009 6:27 am

Re: Detecting Memory Card inserts

Post 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
Egypt my love
User avatar
Thunder93
Addict
Addict
Posts: 1788
Joined: Tue Mar 21, 2006 12:31 am
Location: Canada

Re: Detecting Memory Card inserts

Post 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.
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
Ramihyn_
Enthusiast
Enthusiast
Posts: 314
Joined: Fri Feb 24, 2006 9:40 am

Re: Detecting Memory Card inserts

Post by Ramihyn_ »

Pretty much all card readers are connected as internal USB storage media or externally via USB.
User avatar
Thunder93
Addict
Addict
Posts: 1788
Joined: Tue Mar 21, 2006 12:31 am
Location: Canada

Re: Detecting Memory Card inserts

Post 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
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
User avatar
Thunder93
Addict
Addict
Posts: 1788
Joined: Tue Mar 21, 2006 12:31 am
Location: Canada

Re: Detecting Memory Card inserts

Post 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
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
User avatar
Shardik
Addict
Addict
Posts: 2070
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: Detecting Memory Card inserts

Post 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
User avatar
Thunder93
Addict
Addict
Posts: 1788
Joined: Tue Mar 21, 2006 12:31 am
Location: Canada

Re: Detecting Memory Card inserts

Post 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
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
Post Reply