IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C++Builder Discussion :

Ejecter une clef usb


Sujet :

C++Builder

  1. #1
    Membre éclairé Avatar de Baxter67
    Profil pro
    Inscrit en
    Juin 2005
    Messages
    270
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2005
    Messages : 270
    Par défaut Ejecter une clef usb
    Bonjours, apres des recherches infructueuses, je m'en remet a nouveau a vous.

    Je voulais savoir si il existais une methode simple pour ejecter une clef usb (comme si on cliquais sur licone en bas a droite).

    J'aimerais le faire parceque mon application va tourner sur un écran tactile et c'est assez délicat de choper l'icone en bas a droite si l'utilisateur a de gros doight.

    Voila jai regarder dans les sources (trouver des fonctions pour ejecter un cd) mais c'est pas sa.
    Jai également regarder sur le site msdn mais j'ai rien trouvé non plus .

    voila en espérant que quelqu'un puisse maider

    Cordialement Baxter

  2. #2
    Membre éclairé Avatar de Baxter67
    Profil pro
    Inscrit en
    Juin 2005
    Messages
    270
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2005
    Messages : 270
    Par défaut
    jai trouver ceci en delphi :

    http://www.developpez.net/forums/sho...=%E9jecter+usb

    mais je n'y comprend pas grand chose et la plupart de fonctions de sont pas reconnue et meme l'include il le conais pas

    Cordialement baxter

  3. #3
    say
    say est déconnecté
    Membre Expert
    Avatar de say
    Profil pro
    Inscrit en
    Août 2002
    Messages
    1 176
    Détails du profil
    Informations personnelles :
    Âge : 47
    Localisation : France

    Informations forums :
    Inscription : Août 2002
    Messages : 1 176
    Par défaut
    tu as bien installer les biblothèques qu'il cite?

  4. #4
    Membre éclairé Avatar de Baxter67
    Profil pro
    Inscrit en
    Juin 2005
    Messages
    270
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2005
    Messages : 270
    Par défaut
    bon, jai réusi a me le rendre semi automatique lol

    sa regle deja le probleme de lutilsateur qui a du mal a appuyer sur le bouton eject en bas a droite a cause du tactile

    jai une utiliser une comande shell qui affiche la boite de dialogue ejction périphérique

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    ShellExecute(0, 0, "Rundll32.exe" , "Shell32.dll Control_RunDLLA hotplug.dll", 0, SW_SHOWNORMAL);
    mais cela ne me plait qu'a moitier

    Cordialement Baxter

  5. #5
    Membre éclairé Avatar de Baxter67
    Profil pro
    Inscrit en
    Juin 2005
    Messages
    270
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2005
    Messages : 270
    Par défaut
    jai trouver une source builder qui fonctionne bien :

    http://olmon.free.fr/index.php?post/...ne-de-commande

    mais je ne comprend pas bien le programme

    Quelqu'un pourais m'aider a le comprendre ? ou tous simplement me dire la partie de code qui ejecte ...

    Merci
    Cordialement Baxter

  6. #6
    Membre éclairé Avatar de Baxter67
    Profil pro
    Inscrit en
    Juin 2005
    Messages
    270
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2005
    Messages : 270
    Par défaut
    sa y est, jai réusi a faire l'éjection d'une clef usb par programme C++ builder sans action utilisateur.
    Je me suis inspiré du dernier lien que jai envoyer.

    Sa peut faire une bonne FAQ a mon avie, etant donné que pendant mes recherches jai vue une multitude de personne qui cherchais a le faire

    Cordialement Baxter

  7. #7
    Expert confirmé

    Avatar de pottiez
    Homme Profil pro
    Développeur C++
    Inscrit en
    Novembre 2005
    Messages
    7 152
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Développeur C++
    Secteur : Industrie

    Informations forums :
    Inscription : Novembre 2005
    Messages : 7 152
    Par défaut
    Eventuellement, tu post ton code et une petite explication dans le forum contribuez



    PS : n'oublie pas

  8. #8
    Rédacteur/Modérateur
    Avatar de ero-sennin
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Juillet 2005
    Messages
    2 965
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 38
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juillet 2005
    Messages : 2 965
    Par défaut
    Salut,

    Je viens mettre mon grain de sel
    Sur la MSDN, il y a tout ce qu'il faut ... c'est par ici

    A+

  9. #9
    Membre éclairé Avatar de Baxter67
    Profil pro
    Inscrit en
    Juin 2005
    Messages
    270
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2005
    Messages : 270
    Par défaut
    bon voila mon code


    UnitEjectionUSB.cpp :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    426
    427
    428
    429
    430
    431
    432
    433
    434
    435
    436
    437
    438
    439
    440
    441
    442
    443
    444
    445
    446
    447
    448
    449
    450
    451
    452
    453
    454
    455
    456
    457
    458
    459
    460
    461
    462
    463
    464
    465
    466
    467
    468
    469
    470
    471
    472
    473
    474
    475
    476
    477
    478
    479
    480
    481
    482
    483
    484
    485
    486
    487
    488
    489
    490
    491
    492
    493
    494
    495
    496
    497
    498
    499
    500
    501
    502
    503
    504
    505
    506
    507
    508
    509
    510
    511
    512
    513
    514
    515
    516
    517
    518
    519
    520
    521
    522
    523
    524
    525
    526
    527
    528
    529
    530
    531
    532
    533
    534
    535
    536
    537
    538
    539
    540
    541
    542
    543
    544
    545
    546
    547
    548
    549
    550
    551
    552
    553
     
    #pragma hdrstop
    #include "UnitEjectionUSB.h"
     
    #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;
                            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;
     
    TDeviceEject::TDeviceEject(BOOL AVerbose,BOOL ADebug)
    {
      DeviceEjectErrorCode    = deec_Ok;
      NativeErrorCode         = 0;
      Verbose                 = AVerbose;
      Debug                   = ADebug;
      Lib                     = NULL;
      UnloadLib               = FALSE;
      Func_LocateDevNode      = NULL;
      Func_GetChild           = NULL;
      Func_GetSibling         = NULL;
      Func_GetDevNodeStatus   = NULL;
      Func_GetDeviceIDSize    = NULL;
      Func_GetDeviceID        = NULL;
      Func_RequestDeviceEject = NULL;
     
      BOOL OSOk = FALSE;
      DWORD V = GetVersion();
            if (!(V & 0x80000000))
            {
                    BYTE MajorVersion = (BYTE)V;
                    if (MajorVersion >= 5)
                            OSOk = TRUE;
            }
            if (!OSOk)
                    DeviceEjectErrorCode = deec_UnsupportedOS;
            else
            {
                    const PCHAR LibFileName = "setupapi.dll";
                    Lib = GetModuleHandle(LibFileName);
                    if (!Lib)
                    {
                            Lib = LoadLibrary(LibFileName);
                            if (Lib)
                                    UnloadLib = TRUE;
                    }
                    if (!Lib)
                    {
                            DeviceEjectErrorCode = deec_ErrorLoadingLibrary;
                            NativeErrorCode      = GetLastError();
                    }
                    else
                    {
                            Func_LocateDevNode      = (TCM_Locate_DevNodeA)      GetProcAddress(Lib,"CM_Locate_DevNodeA");
                            Func_GetChild           = (TCM_Get_Child)            GetProcAddress(Lib,"CM_Get_Child");
                            Func_GetSibling         = (TCM_Get_Sibling)          GetProcAddress(Lib,"CM_Get_Sibling");
                            Func_GetDevNodeStatus   = (TCM_Get_DevNode_Status)   GetProcAddress(Lib,"CM_Get_DevNode_Status");
                            Func_GetDeviceIDSize    = (TCM_Get_Device_ID_Size)   GetProcAddress(Lib,"CM_Get_Device_ID_Size");
                            Func_GetDeviceID        = (TCM_Get_Device_IDA)       GetProcAddress(Lib,"CM_Get_Device_IDA");
                            Func_RequestDeviceEject = (TCM_Request_Device_EjectA)GetProcAddress(Lib,"CM_Request_Device_EjectA");
                    }
            }
    }
     
    TDeviceEject::~TDeviceEject()
    {
            if (Lib && UnloadLib)
                    FreeLibrary(Lib);
    }
     
    BOOL TDeviceEject::GetDriveDeviceId(PCHAR DriveSpec,PCHAR DeviceId,DWORD MaxDeviceIdSize)
    {
      BOOL Result = FALSE;
            if (DeviceId && MaxDeviceIdSize)
                    DeviceId[0] = '\0';
            if (DriveSpec && DriveSpec[0] && DeviceId && MaxDeviceIdSize)
            {
                    HKEY Key;
                    LONG L = RegOpenKeyEx(HKEY_LOCAL_MACHINE,"SYSTEM\\MountedDevices",0,KEY_QUERY_VALUE,&Key);
                    if (L == ERROR_SUCCESS)
                    {
                            DWORD Type;
                            BYTE  Value[1024];
                            DWORD ValueSize = sizeof Value;
                            CHAR  ValueName[256];
                            strcpy(ValueName,"\\DosDevices\\");
                            strncat(ValueName,DriveSpec,sizeof ValueName - 1);
                            ValueName[sizeof ValueName - 1] = '\0';
                            L = RegQueryValueEx(Key,ValueName,0,&Type,(BYTE *)Value,&ValueSize);
                            RegCloseKey(Key);
                            if ((L == ERROR_SUCCESS) && (Type == REG_BINARY))
                            {
                                    // Beispielsweise:
                                    //   \??\SBP2#API-903-95__&1394_Storage_+_Repeater_&LUN0#0050c564500068e2#{53f5630d-b6bf-11d0-94f2-00a0c91efb8b}
                                    // ->
                                    //   SBP2\API-903-95__&1394_STORAGE_+_REPEATER_&LUN0\0050C564500068E2
                                    PWSTR p1 = (PWSTR)Value;
                                    ValueSize /= sizeof *p1;
                                    if ((p1[0] == '\\') && (p1[1] == '?') && (p1[2] == '?') && (p1[3] == '\\'))
                                    {
                                            Result = TRUE;
                                            p1        += 4;
                                            ValueSize -= 4;
                                            PCHAR p2 = DeviceId;
                                            for (unsigned int i = 0;i < ValueSize;++i)
                                            {
                                                    CHAR c = p1[i];
                                                    if ((c == '{') || ((c == '#') && (p1[i + 1] == '{')))
                                                            break;
                                                    if (c == '#')
                                                            c = '\\';
                                                    if ((p2 - DeviceId) < (MaxDeviceIdSize - 1))
                                                            *p2++ = c;
                                            }
                                            *p2 = '\0';
                                    }
                            }
                    }
            }
            return Result;
    }
     
    BOOL TDeviceEject::GetFriendlyName(PCHAR DeviceId,PCHAR Name,DWORD MaxNameSize)
    {
      BOOL Result = FALSE;
            if (Name && MaxNameSize)
                    Name[0] = '\0';
            if (DeviceId && DeviceId[0] && Name && MaxNameSize)
            {
                    HKEY Key;
                    CHAR KeyName[1024];
                    strcpy(KeyName,"SYSTEM\\CurrentControlSet\\Enum\\");
                    strncat(KeyName,DeviceId,sizeof KeyName - 1);
                    KeyName[sizeof KeyName - 1] = '\0';
                    LONG L = RegOpenKeyEx(HKEY_LOCAL_MACHINE,KeyName,0,KEY_QUERY_VALUE,&Key);
                    if (L == ERROR_SUCCESS)
                    {
                            DWORD Type;
                            BYTE  Value[1024];
                            DWORD ValueSize = sizeof Value;
                            L = RegQueryValueEx(Key,"FriendlyName",0,&Type,(BYTE *)Value,&ValueSize);
                            if (L != ERROR_SUCCESS)
                                    L = RegQueryValueEx(Key,"DeviceDesc",0,&Type,(BYTE *)Value,&ValueSize);
                            RegCloseKey(Key);
                            if ((L == ERROR_SUCCESS) && (Type == REG_SZ))
                            {
                                    strncpy(Name,(PCHAR)Value,MaxNameSize - 1);
                                    Name[MaxNameSize - 1] = '\0';
                                    Result = TRUE;
                            }
                    }
            }
            return Result;
    }
     
    static BOOL CompareWithWildcard(PCHAR s1,PCHAR s2)
    {
            if (s1 && s2)
            for (;;)
            {
                    if (*s2 == '*')
                            return TRUE;
                    if (!*s1)
                    {
                            if (!*s2)
                                    return TRUE;
                            break;
                    }
                    if (!*s2)
                    {
                            if (!*s1)
                                    return TRUE;
                            break;
                    }
                    if (toupper(*s1++) != toupper(*s2++))
                            break;
            }
            return FALSE;
    }
     
    TDeviceEject::TDeviceEjectErrorCode TDeviceEject::EnumDevices(PEnumDeviceInfo ParentEnumDeviceInfo,PDWORD EjectDeviceFound,PCHAR EjectDevSpec,BOOL EjectDevSpecIsName,DEVINST DevInst,int Indent,PDWORD NativeErrorCode)
    {
      TDeviceEjectErrorCode Result = deec_Ok;
     
      DEVINST ThisDevInst = DevInst;
            while (Result == deec_Ok)
            {
                    ULONG Status; // DN_...
                    ULONG ProblemNumber;
                    CONFIGRET r = Func_GetDevNodeStatus(&Status,&ProblemNumber,ThisDevInst,0);
                    if (r == CR_NO_SUCH_DEVNODE)
                    {
                    }
                    else if (r != CR_SUCCESS)
                    {
                            Result = deec_ErrorGettingDevNodeStatus;
                            *NativeErrorCode = r;
                    }
                    if (Result == deec_Ok)
                    {
                            ULONG DeviceIDLen;
                            r = Func_GetDeviceIDSize(&DeviceIDLen,ThisDevInst,0);
                            if (r != CR_SUCCESS)
                            {
                                    Result = deec_ErrorGettingDeviceIdSize;
                                    *NativeErrorCode = r;
                            }
                            else
                            {
                                    PCHAR DeviceID = new CHAR[DeviceIDLen + 1];
                                    if (DeviceID)
                                    {
                                            r = Func_GetDeviceID(ThisDevInst,DeviceID,DeviceIDLen + 1,0);
                                            if (r != CR_SUCCESS)
                                            {
                                                    Result = deec_ErrorGettingDeviceId;
                                                    *NativeErrorCode = r;
                                            }
                                            else
                                            {
                                                    CHAR FriendlyName[512];
                                                    if (!GetFriendlyName(DeviceID,FriendlyName,sizeof FriendlyName))
                                                            FriendlyName[0] = '\0';
     
                                                    BOOL Found = FALSE;
                                                    if (EjectDevSpec && EjectDevSpec[0])
                                                    {
                                                            if (EjectDevSpecIsName)
                                                            {
                                                                    if (FriendlyName[0])
                                                                            Found = CompareWithWildcard(FriendlyName,EjectDevSpec);
                                                            }
                                                            else
                                                                    Found = CompareWithWildcard(DeviceID,EjectDevSpec);
                                                    }
                                                    if (Found)
                                                    {
                                                            ++*EjectDeviceFound;
                                                            DEVINST EjectDevInst = ThisDevInst;
                                                            if (!(Status & DN_REMOVABLE))
                                                            {
                                                                    // Dieses Device ist nicht auswerfbar, probiere übergeordneten Eintrag
                                                                    for (PEnumDeviceInfo EDI = ParentEnumDeviceInfo;EDI;EDI = EDI->Parent)
                                                                    {
                                                                            if (EDI->Status & DN_REMOVABLE)
                                                                            {
                                                                                    EjectDevInst  = EDI->DevInst;
                                                                                    Status        = EDI->Status;
                                                                                    ProblemNumber = EDI->ProblemNumber;
                                                                                    break;
                                                                            }
                                                                    }
                                                            }
     
                                                            if (!(Status & DN_REMOVABLE))
                                                            {
                                                                    Result = deec_DeviceNotRemovable;;
                                                            }
                                                            else
                                                            {
                                                                    if (ProblemNumber)
                                                                    {
                                                                            Result = deec_DeviceHasProblem;
                                                                            *NativeErrorCode = ProblemNumber;
                                                                    }
                                                                    else
                                                                    {
                                                                            PNP_VETO_TYPE VetoType;
                                                                            CHAR          VetoName[MAX_PATH];
                                                                            r = Func_RequestDeviceEject(EjectDevInst,&VetoType,VetoName,sizeof VetoName,0);
                                                                            if (r != CR_SUCCESS)
                                                                            {
                                                                                    if (r == CR_REMOVE_VETOED)
                                                                                    {
                                                                                            Result = deec_ErrorEjectVetoed;
                                                                                            *NativeErrorCode = VetoType;
                                                                                    }
                                                                                    else
                                                                                    {
                                                                                            Result = deec_ErrorDeviceEject;
                                                                                            *NativeErrorCode = r;
                                                                                    }
                                                                            }
                                                                    }
                                                            }
                                                    }
                                            }
                                            delete [] DeviceID;
                                    }
                            }
                    }
     
                    if (Result == deec_Ok)
                    {
                            DEVINST Child;
                            r = Func_GetChild(&Child,ThisDevInst,0);
                            if (r == CR_NO_SUCH_DEVNODE)
                            {
                            }
                            else if (r != CR_SUCCESS)
                            {
                                    Result = deec_ErrorGettingChild;
                                    *NativeErrorCode = r;
                            }
                            else
                            {
                                    TEnumDeviceInfo EnumDeviceInfo;
                                    EnumDeviceInfo.Parent        = ParentEnumDeviceInfo;
                                    EnumDeviceInfo.DevInst       = ThisDevInst;
                                    EnumDeviceInfo.Status        = Status;
                                    EnumDeviceInfo.ProblemNumber = ProblemNumber;
                                    Result = EnumDevices(&EnumDeviceInfo,EjectDeviceFound,EjectDevSpec,EjectDevSpecIsName,Child,Indent + 1,NativeErrorCode);
                            }
                            if (Result == deec_Ok)
                            {
                                    DEVINST Sibling;
                                    r = Func_GetSibling(&Sibling,ThisDevInst,0);
                                    if (r == CR_NO_SUCH_DEVNODE)
                                    break;
                                    if (r != CR_SUCCESS)
                                    {
                                            Result = deec_ErrorGettingSibling;
                                            *NativeErrorCode = r;
                                    }
                                    ThisDevInst = Sibling;
                            }
                    }
            }
            return Result;
    }
     
    TDeviceEject::TDeviceEjectErrorCode TDeviceEject::Eject(PCHAR EjectDevSpec,BOOL EjectDevSpecIsName,PDWORD NativeErrorCode,PDWORD EjectedCount)
    {
      TDeviceEjectErrorCode Result = deec_Ok;
            *NativeErrorCode = 0;
            if (EjectedCount)
                    *EjectedCount = 0;
            if (!Func_LocateDevNode || !Func_GetChild || !Func_GetSibling || !Func_GetDevNodeStatus || !Func_GetDeviceIDSize || !Func_GetDeviceID || !Func_RequestDeviceEject)
                    Result = deec_LibraryFunctionMissing;
            else
            {
                    DEVINST RootDevInst;
                    CONFIGRET r = Func_LocateDevNode(&RootDevInst,NULL,0/*CM_LOCATE_DEVNODE_NORMAL*/);
                    if (r != CR_SUCCESS)
                    {
                            Result = deec_ErrorLocatingDevNode;
                            *NativeErrorCode = r;
                    }
                    else
                    {
                            DWORD EjectDeviceFound = 0;
                            Result = EnumDevices(NULL,&EjectDeviceFound,EjectDevSpec,EjectDevSpecIsName,RootDevInst,0,NativeErrorCode);
                            if ((Result == deec_Ok) && EjectDevSpec[0] && !EjectDeviceFound)
                                    Result = deec_DeviceNotFound;
                            if (EjectedCount)
                                    *EjectedCount = EjectDeviceFound;
                    }
            }
            return Result;
    }
     
    int FonctionRetirerUSB(char *argv)
    {
      int ExitCode = 0;
      BOOL ArgsOk  = TRUE;
      BOOL Verbose = FALSE;
      BOOL Debug   = FALSE;
      CHAR EjectDrive[80];
      CHAR EjectDevSpec[1024];
      EjectDrive[0]   = '\0';
      EjectDevSpec[0] = '\0';
      BOOL EjectDevSpecIsName = FALSE;
     
            if (!strncmp(argv,"-EjectDrive:",12))
            {
                    strncpy(EjectDrive,argv + 12,sizeof EjectDrive - 1);
                    EjectDrive[sizeof EjectDrive - 1] = '\0';
                    if (!TDeviceEject::GetDriveDeviceId(EjectDrive,EjectDevSpec,sizeof EjectDevSpec))
                    {
                            ArgsOk = FALSE;
                    }
                    EjectDevSpecIsName = FALSE;
            }
     
            if (ArgsOk)
            {
     
                    PDeviceEject DeviceEject = new TDeviceEject(Verbose,Debug);
                    if (!DeviceEject)
                            ExitCode = 11;
                    else
                    {
                            if (DeviceEject->DeviceEjectErrorCode)
                            {
                                    ExitCode = 12;
                            }
                            else
                            {
                                    DWORD NativeErrorCode;
                                    DWORD EjectedCount;
                                    TDeviceEject::TDeviceEjectErrorCode ErrorCode = DeviceEject->Eject(EjectDevSpec,EjectDevSpecIsName,&NativeErrorCode,&EjectedCount);
                            }
                            delete DeviceEject;
                    }
            }
            return ExitCode;
    }
    #pragma package(smart_init)
    UnitEjectionUSB.h :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
     
    //---------------------------------------------------------------------------
     
    #ifndef UnitEjectionUSBH
    #define UnitEjectionUSBH
    int FonctionRetirerUSB(char *argv);
    //---------------------------------------------------------------------------
    #endif
    Appel de la fonction ailleur

    Unit1.cpp

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
     
    #include "UnitEjectionUSB.h"
    #define CLEFUSB "E:"
     
    void __fastcall TForm16::ColorButtonEjeUSBClick(TObject *Sender)
    {
            //ShellExecute(0, 0, "Rundll32.exe" , "Shell32.dll Control_RunDLLA hotplug.dll", 0, SW_SHOWNORMAL);
      char *cTmp1 = "-EjectDrive:";
      char *cTmp2;
      char Buffer[14];
      AnsiString asTmp;
     
            asTmp = CLEFUSB;
     
            cTmp2 = new char[2];
     
            strcpy(cTmp2, asTmp.c_str());
            strcpy(Buffer, cTmp1);
            strcat(Buffer, cTmp2);
     
            FonctionRetirerUSB(Buffer);
    }

    Bon sa peut etre simplifier a mon avie mais bon, si quelqu'un me propose des amélioration je suis toujours preneur

    Cordialement Baxter


    source du code qui ma servie de base :
    http://olmon.free.fr/index.php?post/...ne-de-commande

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Ejecter une clef usb
    Par hugoclo dans le forum VB 6 et antérieur
    Réponses: 6
    Dernier message: 14/02/2010, 18h32
  2. Vérifier une Clef USB (Flash disk)
    Par kmaniche dans le forum C++Builder
    Réponses: 12
    Dernier message: 23/09/2006, 21h08
  3. Utiliser une clef USB comme de la ram de mémoire vive
    Par Lorponos dans le forum Composants
    Réponses: 28
    Dernier message: 18/08/2006, 18h32
  4. [Nero] Utiliser un CD-RW comme une clef USB
    Par Furius dans le forum Périphériques
    Réponses: 6
    Dernier message: 24/10/2005, 17h22
  5. [Système] Trouver une clef USB avec PHP
    Par chico_du_33 dans le forum Langage
    Réponses: 8
    Dernier message: 05/10/2005, 17h29

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo