如何实现U盘的安全移除
谁能告诉我如何实现U盘的安全移除?
用BCB做
[解决办法]
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <stdio.h>
class TDeviceEject
{
public:
typedef enum
{
deec_Ok = 0,
deec_UnsupportedOS = 1,
deec_ErrorLoadingLibrary = 2,
deec_LibraryFunctionMissing = 3,
deec_DeviceNotFound = 4,
deec_DeviceNotRemovable = 5,
deec_DeviceHasProblem = 6,
deec_NativeError = 7,
deec_ErrorGettingDevNodeStatus = 8,
deec_ErrorGettingDeviceIdSize = 9,
deec_ErrorGettingDeviceId = 10,
deec_ErrorGettingChild = 11,
deec_ErrorGettingSibling = 12,
deec_ErrorLocatingDevNode = 13,
deec_ErrorDeviceEject = 14,
deec_ErrorEjectVetoed = 15,
deec_LAST
} TDeviceEjectErrorCode;
TDeviceEjectErrorCode DeviceEjectErrorCode;
// Fehlercodes des Configuration Managers
enum
{
CR_SUCCESS = 0x00,
CR_DEFAULT = 0x01,
CR_OUT_OF_MEMORY = 0x02,
CR_INVALID_POINTER = 0x03,
CR_INVALID_FLAG = 0x04,
CR_INVALID_DEVNODE = 0x05,
CR_INVALID_RES_DES = 0x06,
CR_INVALID_LOG_CONF = 0x07,
CR_INVALID_ARBITRATOR = 0x08,
CR_INVALID_NODELIST = 0x09,
CR_DEVNODE_HAS_REQS = 0x0A,
CR_INVALID_RESOURCEID = 0x0B,
CR_DLVXD_NOT_FOUND = 0x0C,
CR_NO_SUCH_DEVNODE = 0x0D,
CR_NO_MORE_LOG_CONF = 0x0E,
CR_NO_MORE_RES_DES = 0x0F,
CR_ALREADY_SUCH_DEVNODE = 0x10,
CR_INVALID_RANGE_LIST = 0x11,
CR_INVALID_RANGE = 0x12,
CR_FAILURE = 0x13,
CR_NO_SUCH_LOGICAL_DEV = 0x14,
CR_CREATE_BLOCKED = 0x15,
CR_NOT_SYSTEM_VM = 0x16,
CR_REMOVE_VETOED = 0x17,
CR_APM_VETOED = 0x18,
CR_INVALID_LOAD_TYPE = 0x19,
CR_BUFFER_SMALL = 0x1A,
CR_NO_ARBITRATOR = 0x1B,
CR_NO_REGISTRY_HANDLE = 0x1C,
CR_REGISTRY_ERROR = 0x1D,
CR_INVALID_DEVICE_ID = 0x1E,
CR_INVALID_DATA = 0x1F,
CR_INVALID_API = 0x20,
CR_DEVLOADER_NOT_READY = 0x21,
CR_NEED_RESTART = 0x22,
CR_NO_MORE_HW_PROFILES = 0x23,
CR_DEVICE_NOT_THERE = 0x24,
CR_NO_SUCH_VALUE = 0x25,
CR_WRONG_TYPE = 0x26,
CR_INVALID_PRIORITY = 0x27,
CR_NOT_DISABLEABLE = 0x28,
CR_FREE_RESOURCES = 0x29,
CR_QUERY_VETOED = 0x2A,
CR_CANT_SHARE_IRQ = 0x2B,
CR_NO_DEPENDENT = 0x2C,
CR_SAME_RESOURCES = 0x2D,
CR_NO_SUCH_REGISTRY_KEY = 0x2E,
CR_INVALID_MACHINENAME = 0x2F,
CR_REMOTE_COMM_FAILURE = 0x30,
CR_MACHINE_UNAVAILABLE = 0x31,
CR_NO_CM_SERVICES = 0x32,
CR_ACCESS_DENIED = 0x33,
CR_CALL_NOT_IMPLEMENTED = 0x34,
CR_INVALID_PROPERTY = 0x35,
CR_DEVICE_INTERFACE_ACTIVE = 0x36,
CR_NO_SUCH_DEVICE_INTERFACE = 0x37,
CR_INVALID_REFERENCE_STRING = 0x38,
CR_INVALID_CONFLICT_LIST = 0x39,
CR_INVALID_INDEX = 0x3A,
CR_INVALID_STRUCTURE_SIZE = 0x3B
};
DWORD NativeErrorCode;
TDeviceEject(BOOL AVerbose,BOOL ADebug);
~TDeviceEject();
TDeviceEjectErrorCode Eject(PCHAR EjectDevSpec,BOOL EjectDevSpecIsName,PDWORD NativeErrorCode,PDWORD EjectedCount);
static BOOL GetDriveDeviceId(PCHAR DriveSpec,PCHAR DeviceId,DWORD MaxDeviceIdSize);
private:
BOOL Verbose;
BOOL Debug;
HMODULE Lib;
BOOL UnloadLib;
typedef DWORD DEVINST,*PDEVINST;
typedef CHAR *DEVINSTID_A;
typedef enum
{
PNP_VetoTypeUnknown,
PNP_VetoLegacyDevice,
PNP_VetoPendingClose,
PNP_VetoWindowsApp,
PNP_VetoWindowsService,
PNP_VetoOutstandingOpen,
PNP_VetoDevice,
PNP_VetoDriver,
PNP_VetoIllegalDeviceRequest,
PNP_VetoInsufficientPower,
PNP_VetoNonDisableable,
PNP_VetoLegacyDriver,
PNP_VetoInsufficientRights
} PNP_VETO_TYPE,* PPNP_VETO_TYPE;
enum
{
DN_REMOVABLE = 0x00004000
};
typedef DWORD CONFIGRET;
typedef CONFIGRET (WINAPI *TCM_Locate_DevNodeA) (OUT PDEVINST pdnDevInst,IN DEVINSTID_A pDeviceID,OPTIONAL IN ULONG ulFlags);
typedef CONFIGRET (WINAPI *TCM_Get_Child) (OUT PDEVINST pdnDevInst,IN DEVINST dnDevInst,IN ULONG ulFlags);
typedef CONFIGRET (WINAPI *TCM_Get_Sibling) (OUT PDEVINST pdnDevInst,IN DEVINST DevInst,IN ULONG ulFlags);
typedef CONFIGRET (WINAPI *TCM_Get_DevNode_Status) (OUT PULONG pulStatus,OUT PULONG pulProblemNumber,IN DEVINST dnDevInst,IN ULONG ulFlags);
typedef CONFIGRET (WINAPI *TCM_Get_Device_ID_Size) (OUT PULONG pulLen,IN DEVINST dnDevInst,IN ULONG ulFlags);
typedef CONFIGRET (WINAPI *TCM_Get_Device_IDA) (IN DEVINST dnDevInst,OUT PCHAR Buffer,IN ULONG BufferLen,IN ULONG ulFlags);
typedef CONFIGRET (WINAPI *TCM_Request_Device_EjectA)(IN DEVINST dnDevInst,OUT PPNP_VETO_TYPE pVetoType,OUT LPSTR pszVetoName,IN ULONG ulNameLength,IN ULONG ulFlags);
TCM_Locate_DevNodeA Func_LocateDevNode;
TCM_Get_Child Func_GetChild;
TCM_Get_Sibling Func_GetSibling;
TCM_Get_DevNode_Status Func_GetDevNodeStatus;
TCM_Get_Device_ID_Size Func_GetDeviceIDSize;
TCM_Get_Device_IDA Func_GetDeviceID;
TCM_Request_Device_EjectA Func_RequestDeviceEject;
typedef struct _TEnumDeviceInfo
{
struct _TEnumDeviceInfo *Parent;
DEVINST DevInst;
ULONG Status;
ULONG ProblemNumber;
} TEnumDeviceInfo,* PEnumDeviceInfo;
TDeviceEjectErrorCode EnumDevices(PEnumDeviceInfo ParentEnumDeviceInfo,PDWORD EjectDeviceFound,PCHAR EjectDevSpec,BOOL EjectDevSpecIsName,DEVINST DevInst,int Indent,PDWORD NativeErrorCode);
BOOL GetFriendlyName(PCHAR DeviceId,PCHAR Name,DWORD MaxNameSize);
};
typedef TDeviceEject * PDeviceEject;
[解决办法]
codeproject上有个for vc console模式的,可自己改一下
http://www.codeproject.com/system/RemoveDriveByLetter.asp