hi Antonio,
i have posted only line 111 - 153 so the missing CODE are before Line 111
here is the full HB_FUNC.PRG CODE
Code: Select all | Expand
#pragma begindump
#include <windows.h>
#include <winioctl.h>
#include <tchar.h>
#include <stdio.h>
#include "hbapi.h"
#include "hbapifs.h"
// Prototypes
BOOL EjectVolume(TCHAR cDriveLetter);
HANDLE OpenVolume(TCHAR cDriveLetter);
BOOL LockVolume(HANDLE hVolume);
BOOL DismountVolume(HANDLE hVolume);
BOOL PreventRemovalOfVolume(HANDLE hVolume, BOOL fPrevent);
BOOL AutoEjectVolume(HANDLE hVolume);
BOOL CloseVolume(HANDLE hVolume);
LPTSTR szVolumeFormat = TEXT("\\\\.\\%c:");
LPTSTR szRootFormat = TEXT("%c:\\");
HANDLE OpenVolume(TCHAR cDriveLetter)
{
HANDLE hVolume;
UINT uDriveType;
TCHAR szVolumeName[8];
TCHAR szRootName[5];
DWORD dwAccessFlags;
wsprintf(szRootName, szRootFormat, cDriveLetter);
uDriveType = GetDriveType(szRootName);
switch(uDriveType)
{
case DRIVE_REMOVABLE:
dwAccessFlags = GENERIC_READ | GENERIC_WRITE;
break;
case DRIVE_CDROM:
dwAccessFlags = GENERIC_READ;
break;
default:
return INVALID_HANDLE_VALUE;
}
wsprintf(szVolumeName, szVolumeFormat, cDriveLetter);
hVolume = CreateFile( szVolumeName,
dwAccessFlags,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
0,
NULL );
return hVolume;
}
BOOL CloseVolume(HANDLE hVolume)
{
return CloseHandle(hVolume);
}
#define LOCK_TIMEOUT 10000 // 10 Seconds
#define LOCK_RETRIES 20
BOOL LockVolume( HANDLE hVolume )
{
DWORD dwBytesReturned;
DWORD dwSleepAmount;
int nTryCount;
dwSleepAmount = LOCK_TIMEOUT / LOCK_RETRIES;
for( nTryCount = 0; nTryCount < LOCK_RETRIES; nTryCount++ )
{
if( DeviceIoControl( hVolume, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0,
&dwBytesReturned, NULL ) )
return TRUE;
Sleep( dwSleepAmount );
}
return FALSE;
}
BOOL DismountVolume( HANDLE hVolume )
{
DWORD dwBytesReturned;
return DeviceIoControl( hVolume, FSCTL_DISMOUNT_VOLUME, NULL, 0, NULL, 0,
&dwBytesReturned, NULL );
}
BOOL PreventRemovalOfVolume(HANDLE hVolume, BOOL fPreventRemoval)
{
DWORD dwBytesReturned;
PREVENT_MEDIA_REMOVAL PMRBuffer;
PMRBuffer.PreventMediaRemoval = fPreventRemoval;
return DeviceIoControl( hVolume, IOCTL_STORAGE_MEDIA_REMOVAL,
&PMRBuffer, sizeof(PREVENT_MEDIA_REMOVAL),
NULL, 0, &dwBytesReturned, NULL );
}
AutoEjectVolume( HANDLE hVolume )
{
DWORD dwBytesReturned;
return DeviceIoControl( hVolume, IOCTL_STORAGE_EJECT_MEDIA, NULL, 0, NULL, 0,
&dwBytesReturned,
NULL );
}
BOOL EjectVolume( TCHAR * cDriveLetter )
{
HANDLE hVolume;
BOOL fRemoveSafely ; // = FALSE;
BOOL fAutoEject ; // = FALSE;
BOOL fRemoveSafely = FALSE;
BOOL fAutoEject = FALSE;
hVolume = OpenVolume(cDriveLetter);
if( hVolume == INVALID_HANDLE_VALUE )
return FALSE;
if( LockVolume(hVolume) && DismountVolume(hVolume) )
fRemoveSafely = TRUE;
{
if (PreventRemovalOfVolume(hVolume, FALSE) && AutoEjectVolume(hVolume))
fAutoEject = TRUE;
}
if( ! CloseVolume(hVolume) )
return FALSE;
return TRUE;
}
HB_FUNC( EJECTREMOVABLE )
{
const char* szDrive = hb_parc(1);
TCHAR tszDrive[3] = TEXT("\0\0");
if (szDrive && szDrive[0] != '\0')
{
#ifdef UNICODE
MultiByteToWideChar(CP_ACP, 0, szDrive, 1, tszDrive, 1);
#else
tszDrive[0] = szDrive[0];
#endif
tszDrive[1] = TEXT(':');
}
hb_retl(EjectVolume(tszDrive));
}
#pragma enddump
and here the Main Programm CODE
Code: Select all | Expand
#include "FIVEWIN.ch"
#include "common.ch"
PROCEDURE MAIN()
LOCAL oDlg, oListbox
LOCAL cDrive := SPACE( 2 )
LOCAL cTitle := "Please, select " + FWVERSION
LOCAL aDrives := aDrives()
LOCAL bSetGet := { | x | cDrive := x }
LOCAL bChange
LOCAL bValid
LOCAL bLDblClicked
LOCAL bWhen
LOCAL bDrawItem
DEFINE DIALOG oDlg FROM 5, 10 TO 24, 55 TITLE cTitle
oListbox := TListBox() :New( 10, 20, bSetGet, aDrives, 145, 95, bChange, ;
oDlg, bValid,,, .T., .F., ;
bLDblClicked,, "", .T., bWhen,, ;
bDrawItem, .F., .F. )
@ 7, 7 BUTTON "&OK" OF oDlg SIZE 40, 12 ;
ACTION( USBeject( cDrive ), oDlg:End() ) DEFAULT
@ 7, 17 BUTTON "&Cancel" OF oDlg SIZE 40, 12 ;
ACTION( cDrive := nil, oDlg:End() )
#IFDEF __HMG__
END DIALOG
#ENDIF
ACTIVATE DIALOG oDlg CENTERED
RETURN
STATIC PROCEDURE USBeject(cDriveLetter )
IF EjectRemovable( cDriveLetter )
MsgInfo( "The Drive " + cDriveLetter + " Can be Safely Removed", "Atenttion ! , Remove USB Ok" )
ELSE
MsgInfo( "Failed to Safely Remove/Eject Drive " + cDriveLetter , "Atenttion ! , Failed Remove USB" )
ENDIF
RETURN
*+ EOF: FWEJECT.PRG