Detecting Memory Card inserts
Detecting Memory Card inserts
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.
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
Re: Detecting Memory Card inserts
see here: http://msdn.microsoft.com/en-us/library ... 85%29.aspx
or here: http://www.purebasic.fr/english/viewtop ... 597#p34597
Greetings -Thomas
or here: http://www.purebasic.fr/english/viewtop ... 597#p34597
Greetings -Thomas
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.

Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.

Re: Detecting Memory Card inserts
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
Re: Detecting Memory Card inserts
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
Re: Detecting Memory Card inserts
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.

Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.

Re: Detecting Memory Card inserts
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
Re: Detecting Memory Card inserts
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?
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
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
Re: Detecting Memory Card inserts
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
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
Re: Detecting Memory Card inserts
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.
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
Re: Detecting Memory Card inserts
Pretty much all card readers are connected as internal USB storage media or externally via USB.
Re: Detecting Memory Card inserts
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
• 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
Re: Detecting Memory Card inserts
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
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
Re: Detecting Memory Card inserts
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:
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
EndIfRe: Detecting Memory Card inserts
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.

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.
ʽʽ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



