Announcement

Collapse
No announcement yet.

How to detect if Flashdrive is unplug from pc?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to detect if Flashdrive is unplug from pc?

    Hello every one, can anyone help me achieve this idea..

    1. I want my app.exe to run only inside the USB Flashdrive.
    - So if the user copy the exe and put it on desktop or other location outside the USB the application will not run...
    2. If the App is running it will detect the USB.. if the usb is remove the app will notify the user to insert the USB again..
    - If different USB it app will not accept it.

    Thanks...

  • #2
    This Option is very important, hope some one can give an answer

    Comment


    • #3
      Yes just an idea to me and hope someone can share how it works..

      Comment


      • #4
        Well I can get you started by showing how to detect for a USB Flashdrive. But the rest would take quite a bit of applied logic and some trial & error. What you're asking is quite a complex task. The attached sample for detecting a USB flashdrive might be a bit buggy, as I haven't fully tested it yet. But it should at least get you started in the general direction.

        Nb.
        I think this task was actually a bit easier with v7 because AMSWave's Script-Plugin catered for importing VBScript - which is very good for this particular task. And which could have been done very simply, like this:
        Code:
        '[B]VBS Code - Detect USB Flashdrive[/B]
        
        Set FSO = CreateObject("Scripting.FileSystemObject")
        Set Drives = FSO.Drives
            strMessage = "USB stick is currently present"
        
        For Each DiskDrive In Drives
            If DiskDrive.DriveType = "1" Then
                strMbox = MsgBox(strMessage ,48,"Detect USB Drive")
            End If
        Next
        Unfortunately though, there's no plugin for this in v8. Maybe someone knows how to call it from Lua???

        Anyway, have a look at the attached APZ which takes a somewhat more clumsy approach . But which seems to work from my preliminary tests.

        As for detecting if USB flashdrive is unplugged, you'd need to move one of the code triggers onto a Page Timer.
        As for how to get the app to run from USB flashdrive ONLY - look into:
        Code:
        drive_type = Drive.GetType(_SourceDrive);
        ...and then code in an appropriate if/then statement.

        Will be happy to help further, if you make a fair-dinkum start on the project and upload your progress. But ain't no-one gonna write this one for you. It's quite a big project and would take some fairly intense logic and effort. There it is, man.
        Attached Files

        Comment


        • #5
          EDIT,

          I just noticed a number of errors in the APZ example I posted earlier - sorry about that. Have made some minor but important changes to the code and re-upped the 'corrected' version. Note also that the code should go in either the Page On Show event or Globals to circumvent errors with the sSerial variable being buffered in memory.

          I should also add that far better solutions for this exist in C# (and although it's currently beyond the scope of my knowledge, a DLL for AMS8 could readily be developed from existing code freely available on the net).
          Attached Files

          Comment


          • #6
            Hi BioHazard ,
            Sorry to hijack but I call a VBS script by just using shell.execute - works well from my USB app stick (no probs on all wins)
            I created vb code to get my internal & external ip's on Nics including when connected to iphone as AMS get lan info stuffs up if you have virtual nics

            Code:
            --run lan info vbscript file
            result = File.DoesExist("AutoPlay\\Docs\\myip.vbs");
            if result then
            Shell.Execute("AutoPlay\\Docs\\myip.vbs", "open", "", "", SW_SHOWNORMAL, false);
            else
            Dialog.Message("Notice", "Cannot find vbs file.", MB_OK, MB_ICONINFORMATION, MB_DEFBUTTON1);
            end
            Also if telco requires an encoded serial # for each usb he could try this to play with, I think he should search forums more

            System Drive Check.apz

            Cheers

            Comment


            • #7
              Originally posted by BioHazard View Post
              EDIT,

              I just noticed a number of errors in the APZ example I posted earlier - sorry about that. Have made some minor but important changes to the code and re-upped the 'corrected' version. Note also that the code should go in either the Page On Show event or Globals to circumvent errors with the sSerial variable being buffered in memory.

              I should also add that far better solutions for this exist in C# (and although it's currently beyond the scope of my knowledge, a DLL for AMS8 could readily be developed from existing code freely available on the net).
              Thanks Bio maybe you are in your first post that this idea is need a more logical mind to acheive. Thanks all your help i will study all of this ideas.

              Comment


              • #8
                Originally posted by BioHazard View Post
                EDIT,

                I just noticed a number of errors in the APZ example I posted earlier - sorry about that. Have made some minor but important changes to the code and re-upped the 'corrected' version. Note also that the code should go in either the Page On Show event or Globals to circumvent errors with the sSerial variable being buffered in memory.

                I should also add that far better solutions for this exist in C# (and although it's currently beyond the scope of my knowledge, a DLL for AMS8 could readily be developed from existing code freely available on the net).
                Thanks Bio maybe you are in your first post that this idea is need a more logical mind to acheive. Thanks all your help i will study all of this ideas.

                Comment


                • #9
                  Originally posted by colc View Post
                  Hi BioHazard ,
                  Sorry to hijack but I call a VBS script by just using shell.execute - works well from my USB app stick (no probs on all wins)
                  I created vb code to get my internal & external ip's on Nics including when connected to iphone as AMS get lan info stuffs up if you have virtual nics

                  Code:
                  --run lan info vbscript file
                  result = File.DoesExist("AutoPlay\\Docs\\myip.vbs");
                  if result then
                  Shell.Execute("AutoPlay\\Docs\\myip.vbs", "open", "", "", SW_SHOWNORMAL, false);
                  else
                  Dialog.Message("Notice", "Cannot find vbs file.", MB_OK, MB_ICONINFORMATION, MB_DEFBUTTON1);
                  end
                  Also if telco requires an encoded serial # for each usb he could try this to play with, I think he should search forums more

                  [ATTACH]n299864[/ATTACH]

                  Cheers
                  Hi colc thank for this share i will also look and study all the shared apz..
                  sorry for the multiple post internet become weird hangup and after refresh multiple post display..

                  Comment


                  • #10
                    Originally posted by colc View Post
                    Hi BioHazard ,
                    Sorry to hijack but I call a VBS script by just using shell.execute ...

                    Also if telco requires an encoded serial # for each usb he could try this to play with, I think he should search forums more

                    Cheers
                    Gday colc, always nice to hear from you. Yes sure, Shell.Execute (or os.execute) will run any file - however the intent here is to be able to capture the output of the VBS into an AMS variable - which is why I mentioned the v7 Script plugin by AMSWaves (he made some damm good stuff - too bad he's moved on to other things).

                    As for the System Drive Check.apz that you've upped, it uses the same DriverSer.dll that I used in my own APZ example (mine just employs simpler/less code by focusing specifically on the task of identifying USB flashdrives. (I believe sside made that DLL if memory serves correct - or maybe it was Worm - dunno, too long ago)).

                    There is of course also the option of simply using the native AMS Drive.GetType & Drive Get.Information to return drive-type and drive serial but the caveat is that AMS's native functions cannot distinguish between different kinds of Type 2 removeable drives, and so will return false positives on floppy drives (yeah, I know - who uses floppy drives any more, right?), zip-drives and usb-modems. Hence the need for the DriverSer.dll.

                    And just as an afterthought, I think it might even be possible to use the native Windows API to detect removal of USB flashdrives via DLL.CallFunction. I just don't know how to do it - but would look at the user32.dll in the System32 folder as a start point. Or even by looking at the WinAPI plugin to monitor WinHandles for this event. Just a thought.

                    Have a good Easter, buddy.

                    Comment


                    • #11
                      Hi all. When a removable media device is connected or disconnected, when a network drive is mapped / unmapped and when media is injected/ejected from a device Windows sends the WM_DEVICECHANGE message to all top-level windows. With MemoryEx, you can listen for these with the Subclass function set.

                      This requires the MemoryEx plugin and uses the following function sets:
                      - Subclass
                      - Structures (MemoryEx.DefineStruct / MemoryEx.AssignStruct)
                      - Bitwise (to convert the dbcv_unitmask to drive letters)

                      Good luck, Happy Easter!
                      Attached Files
                      Bas Groothedde
                      Imagine Programming :: Blog

                      AMS8 Plugins
                      IMXLH Compiler

                      Comment


                      • #12
                        Hello IP,

                        Thanks for this great contribution - your stuff always blows me away.

                        I notice however that even with this approach, that specifically on the DeviceChangeMonitor.OnMediaConnected/Disconnected events, that it still can't differentiate between USB Flashdrives and USB HiLink modems which is problematic considering how prevalent / ubiquitous both of these devices are at present.

                        It seems to be an issue of contention across the board - whenever using the listen for WM_DEVICECHANGE approach. See here, for instance: https://www.autoitscript.com/forum/t...rive-insertion

                        I haven't seen CodeProject successfully address this issue either: https://www.codeproject.com/Articles...and-or-Removal

                        Is a solution even possible, using your approach with MemoryEX?

                        Comment


                        • #13
                          Hello BIO and colc thanks for those apz.. when i try it and make manipulation i found out that get.drive info only get the volume serial and the volume serial can be change using a small software..
                          ex: i can make the 2 usb in identical volume serial. so by this user can easily make identical of my application using change volume serial.

                          Comment


                          • #14
                            Hello IP.. thanks for your share.. i check the code and it blows my mind... i need to study it more..

                            Comment


                            • #15
                              Originally posted by telco View Post
                              ...when i try it and make manipulation i found out that get.drive info only get the volume serial and the volume serial can be change using a small software ...
                              Yes, I remember having this discussion way back in 2010.
                              An old post made by Cybergraph is relevant here:

                              Originally posted by Cybergraph View Post
                              Source: https://forums.indigorose.com/forum/...706#post229706

                              The serial number of a drive can be retrieved using AMS internal function Drive.GetInformation.
                              Sside's DLL "DriveInfo.dll" could be useful to grab also the hardware serial but, in my experience, it works only on internal drives (many external drives I have, identified as "type 3", don't produce an hardware serial with this DLL).

                              The mentioned Dll retrieves only the hardware serial and works also on removable drives.

                              Using the Drive.enumerate and attemping to index (in a listbox, for example) the found drives, could happen that a CD or a Floppy drive are found - they have a drive letter, but if the media in them is absent (no cd o floppy disk inside the drive), an error of "attempting to index a nil value" is generated.

                              To avoid this problem (and also to distinguish a genuine type 2 drive, that could be USB but also Firewire, from other usb devices), I usually check if the root folder exists (<drive>:\).

                              Correction: the DriveSerial.dll used by Mystica retrieves the "volume serial" of the drive (the same you can obtain using the DOS "dir" command), not the hardware serial nor the serial obtained by AMS Drive.GetInformation.
                              Original Thread Source: https://forums.indigorose.com/forum/...n-drive-letter

                              The DriveInfo.DLL (not to be confused with the DriveSerial.DLL) mentioned here by Cybergraph is attached.
                              Note though - as Cybergraph already mentioned - that it only seems to grab the hardware serial for internal drives.

                              Attached Files

                              Comment

                              Working...
                              X