1. how auto-detect flash drive?

Forked from Re: Eu on USB flash drive?

CoJaBo said...

I used this set of batch files to create a portable Eu environment. This assumes its always running off of drive H:\, but it shouldn't be hard to make it auto-detect. Modify as needed.

Ok, for a different application, I'd like to be able to auto-detect a flash drive, using Win32Lib, and wasn't ever able to figure out how, so if it shouldn't be hard, can anyone show me how?

Dan M.

new topic     » topic index » view message » categorize

2. Re: how auto-detect flash drive?

DanM said...

Forked from Re: Eu on USB flash drive?

CoJaBo said...

I used this set of batch files to create a portable Eu environment. This assumes its always running off of drive H:\, but it shouldn't be hard to make it auto-detect. Modify as needed.

Ok, for a different application, I'd like to be able to auto-detect a flash drive, using Win32Lib, and wasn't ever able to figure out how, so if it shouldn't be hard, can anyone show me how?

Dan M.

I use this code originally posted by Al Getz that determines drive types. While nort exactly what you're looking for, I hope it points you in the right direction.

Yours, OtterDad

--requires Windows 95 OEM Service Release 2 (OSR2) or above. 
-- originally written and posted by Al Getz 
 
-- slightly modified by Otterdad. this version does NOT seek status 
-- of the floppy drive - see notes 
 
include dll.e 
include msgbox.e 
without warning 
 
global atom hKernel32_dll,hGetLogicalDrives,hGetDiskFreeSpaceEx,hGetDriveType 
 
--related constants (not directly used): 
global constant 
    DRIVE_UNKNOWN=0, 
    DRIVE_NO_ROOT_DIR=1, 
    DRIVE_REMOVABLE=2, 
    DRIVE_FIXED=3, 
    DRIVE_REMOTE=4, 
    DRIVE_CDROM=5, 
    DRIVE_RAMDISK=6, 
-- added by OtterDad 
    GETDRIVEINFO_ERROR_UNKNOWN=7 
 
global constant 
         DRIVESINFO_DRIVE_LETTER=1, 
         DRIVESINFO_READY_STATUS=2, 
         DRIVESINFO_KBYTES_FREE_TO_CALLER=3, 
         DRIVESINFO_KBYTES_TOTAL=4, 
         DRIVESINFO_KBYTES_FREE=5, 
         DRIVESINFO_DISK_TYPE=6 
 
 
global constant DriveTypeStrings={"Unknown","No Root Directory","Removable", 
                       "Fixed","Remote","CD Rom","Ram Disk","error"} 
global constant DriveStatusStrings={"Not Ready  ","    Ready    "} 
global constant DriveStatusStrings2={"OffLine","ok"} 
                                                     
hKernel32_dll=open_dll("kernel32.dll") 
 
if hKernel32_dll=0 then 
    puts(1,"could not locate Kernel32.dll") 
    abort(1) 
end if 
 
 
hGetLogicalDrives= 
    define_c_func(hKernel32_dll, "GetLogicalDrives", {}, C_ULONG) 
if hGetLogicalDrives<0 then 
    puts(1,"could not link to c function"& 
        "GetLogicalDrives in Kernel32.dll") 
    abort(1) 
end if 
 
hGetDiskFreeSpaceEx= 
    define_c_func(hKernel32_dll, "GetDiskFreeSpaceExA", 
    {C_POINTER,C_POINTER,C_POINTER,C_POINTER}, C_ULONG) 
if hGetDiskFreeSpaceEx<0 then 
    puts(1, "could not link to c function"& 
    "GetDiskFreeSpaceEx in Kernel32.dll") 
    abort(1) 
end if 
 
 
hGetDriveType= 
    define_c_func(hKernel32_dll, "GetDriveTypeA", 
    {C_POINTER}, C_UINT) 
if hGetDriveType<0 then 
    puts(1,"could not link to c function"& 
        "GetDriveType in Kernel32.dll") 
    abort(1) 
end if 
 
 
global function peek_pui64_to_ui3232(atom pui64) 
    --This function returns {lower32 bits,upper32 bits}. 
    --pui64 must be a valid ui64 pointer. 
 
    return peek4u({pui64,2}) 
end function 
 
 
global function peek_pui64_to_atom(atom pui64) 
    --This function works for lower range ui64's. 
    --ui64's close to the upper limit will round off, 
    --which is probably around 2^59.  This means a ui64 
    --cant always be converted to an atom accurately. 
    --Its ok for disk sizes though as there arent 
    --any 524,288 gigabyte drives around (yet), and 
    --we are rounding to nearest kilobyte anyway. 
    --If more range is needed, call peek_pui64_to_ui3232 
    --and handle result as a two part number instead of 
    --converting to atom. 
 
    sequence ui3232 
 
    ui3232=peek_pui64_to_ui3232(pui64) 
    return ui3232[1]+ui3232[2]*#100000000 
      --really requires 20 digits for the full range, 
      --but we dont need that many for disk sizes 
      --rounded to nearest kilobyte. 
end function 
 
 
global function GetDriveInfo(sequence drive) 
    --works with any size disk 
    --returns: 
    --  {readystatus,kbytes free to caller,total kbytes,free bytes,disk type} 
    --where 
    --  readystatus=1 for drive ready, 0 for not ready 
    --  kbytes free to caller= kbytes free to program that calls this 
    --  total kbytes=total kbytes on logical disk 
    --  disk type = pointer to string in DiskTypeStrings[] (above) 
    --  which can take on only one of the following values: 
    --    DRIVE_UNKNOWN=0, 
    --    DRIVE_NO_ROOT_DIR=1, 
    --    DRIVE_REMOVABLE=2, 
    --    DRIVE_FIXED=3, 
    --    DRIVE_REMOTE=4, 
    --    DRIVE_CDROM=5, 
    --    DRIVE_RAMDISK=6, 
    --    GETDRIVEINFO_ERROR_UNKNOWN=7 
 
    atom lpPathName,pui64FreeBytesToCaller,pui64TotalBytes, 
         pui64FreeBytes,bool,retv,kbdiv 
    sequence info 
 
    kbdiv=1/1024 
    lpPathName=allocate_string(drive) 
    pui64FreeBytesToCaller=allocate(8) 
    pui64TotalBytes=allocate(8) 
    pui64FreeBytes=allocate(8) 
 
    bool=c_func(hGetDiskFreeSpaceEx, 
         {lpPathName, 
          pui64FreeBytesToCaller, 
          pui64TotalBytes, 
          pui64FreeBytes 
         }) 
 
    retv=c_func(hGetDriveType,{lpPathName}) 
    if retv<0 or retv>6 then 
      retv=7 
    end if 
   
    kbdiv=kbdiv*bool 
 
    info={ 
          bool, 
          peek_pui64_to_atom(pui64FreeBytesToCaller)*kbdiv, 
          peek_pui64_to_atom(pui64TotalBytes)*kbdiv, 
          peek_pui64_to_atom(pui64FreeBytes)*kbdiv, 
          retv 
         } 
 
    free(lpPathName) 
    free(pui64FreeBytesToCaller) 
    free(pui64TotalBytes) 
    free(pui64FreeBytes) 
 
    return info 
end function 
 
 
global function GetDrivesInfo() 
--get info on all logical drives in the system 
 
-- modified by OtterDad to ignore drive a: 
-- if run in automatic mode and there is 
-- no floppy in drive a: the program stops 
-- and generates an error box 
 
    sequence drivesinfo,info,drive 
    atom bit,retv 
    integer DriveLetter 
    atom temp 
     
    bit=1 
    drivesinfo={} 
    retv=c_func(hGetLogicalDrives,{}) 
     
    for k=1 to 26 do 
      if and_bits(bit,retv) then 
        DriveLetter=k+96--change to "DriveLetter=k+64" for caps 
        drive=DriveLetter&":\\" 
 
        -- remove next line to check the floppy drive         
        if DriveLetter != 'a' then 
 
            info=GetDriveInfo(drive) 
            drivesinfo=append(drivesinfo,{drive}&info) 
 
        -- remove next line to check the floppy drive         
        end if 
 
      end if 
      bit=bit*2 
    end for 
    return drivesinfo 
end function 
 
new topic     » goto parent     » topic index » view message » categorize

3. Re: how auto-detect flash drive?

Looks reasonable, OtterDad, I'll see what I can do with it, thanks.

Dan

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu