当前位置: 移动技术网 > IT编程>开发语言>C/C++ > 弹出光驱代码

弹出光驱代码

2019年04月19日  | 移动技术网IT编程  | 我要评论
/*  * eject cds  */      #include <windows.h>  #
/*
 * eject cds
 */ 
 
 
#include <windows.h> 
#include <winioctl.h> 
#include <stdio.h> 
#include <stdlib.h> 
 
/* options */ 
static int unmount_only; 
static int eject_all; 
 
/* wrapper for getdrivetypew */ 
static dword get_drive_type( wchar drive ) 

    static const wchar rootw[] = {'a',':','\\',0}; 
    wchar path[16]; 
 
    memcpy( path, rootw, sizeof(rootw) ); 
    path[0] = drive; 
    return getdrivetypew( path ); 

 
static bool eject_cd( wchar drive ) 

    static const wchar devicew[] = {'\\','\\','.','\\','a',':',0}; 
    prevent_media_removal removal; 
    wchar buffer[16]; 
    handle handle; 
    dword result; 
 
    if (get_drive_type( drive ) != drive_cdrom) 
    { 
        printf( "drive %c: is not a cd or is not mounted\n", (char)drive ); 
        return false; 
    } 
 
    memcpy( buffer, devicew, sizeof(devicew) ); 
    buffer[4] = drive; 
    handle = createfilew( buffer, 
                          generic_write | generic_read, 
                          file_share_read|file_share_write, 
                          null, open_existing, 0, 0 ); 
    if (handle == invalid_handle_value) 
    { 
        printf( "cannot open device for drive %c:\n", (char)drive ); 
        return false; 
    } 
 
    printf( "ejecting %c:\n", (char)drive ); 
 
    if (!deviceiocontrol( handle, fsctl_lock_volume, null, 0, null, 0, &result, null )) 
        printf( "fsctl_lock_volume failed with err %d\n", getlasterror() ); 
 
    if (!deviceiocontrol( handle, fsctl_dismount_volume, null, 0, null, 0, &result, null )) 
        printf( "fsctl_dismount_volume failed with err %d\n", getlasterror() ); 
 
    removal.preventmediaremoval = false; 
    if (!deviceiocontrol( handle, ioctl_storage_media_removal, &removal, sizeof(removal), null, 0, &result, null )) 
        printf( "ioctl_storage_media_removal failed with err %d\n", getlasterror() ); 
 
    if (!unmount_only) 
    { 
        if (!deviceiocontrol( handle, ioctl_storage_eject_media, null, 0, null, 0, &result, null )) 
            printf( "ioctl_storage_eject_media failed with err %d\n", getlasterror() ); 
    } 
     
    if (!deviceiocontrol( handle, fsctl_unlock_volume, null, 0, null, 0, &result, null )) 
        printf( "fsctl_unlock_volume  failed with err %d\n", getlasterror() ); 
 
    closehandle( handle ); 
    return true; 

 
/* find the cd drive, and die if we find more than one */ 
static wchar find_cd_drive(void) 

    wchar ret = 0, drive; 
 
    for (drive = 'c'; drive <= 'z'; drive++) 
    { 
        if (get_drive_type( drive ) != drive_cdrom) continue; 
        if (ret) 
        { 
            printf( "multiple cd drives found (%c: and %c:), you need to specify the one you want.\n", 
                          (char)ret, (char)drive ); 
            exit(1); 
        } 
        ret = drive; 
    } 
    return ret; 

 
static void usage(void) 

    printf( "usage: eject [-u] [-a] [-h] [x:]...\n" ); 
    printf( "    -a  eject all the cd drives we find\n" ); 
    printf( "    -h  display this help message\n" ); 
    printf( "    -u  unmount only, don't eject the cd\n" ); 
    printf( "    x:  eject drive x:\n" ); 
    exit(1); 

 
static void parse_options( int *argc, char *argv[] ) 

    int i; 
    char *opt; 
 
    for (i = 1; i < *argc; i++) 
    { 
        if (argv[i][0] != '-') 
        { 
            /* check for valid drive argument */ 
            if (strlen(argv[i]) != 2 || argv[i][1] != ':') usage(); 
            continue; 
        } 
        for (opt = argv[i] + 1; *opt; opt++) switch(*opt) 
        { 
        case 'a': eject_all = 1; break; 
        case 'u': unmount_only = 1; break; 
        case 'h': usage(); break; 
        default: 
            printf( "unknown option -%c\n", *opt ); 
            usage(); 
        } 
        memmove( argv + i, argv + i + 1, (*argc - i) * sizeof(*argv) ); 
        (*argc)--; 
        i--; 
    } 

 
int main( int argc, char *argv[] ) 

    parse_options( &argc, argv ); 
 
    if (eject_all) 
    { 
        wchar drive; 
 
        for (drive = 'c'; drive <= 'z'; drive++) 
        { 
            if (get_drive_type( drive ) != drive_cdrom) continue; 
            if (!eject_cd( drive )) exit(1); 
        } 
    } 
    else if (argc > 1) 
    { 
        int i; 
 
        for (i = 1; i < argc; i++) 
            if (!eject_cd( argv[i][0] )) exit(1); 
    } 
    else 
    { 
        wchar drive = find_cd_drive(); 
 
        if (!drive) 
        { 
            printf( "no cd drive found\n" ); 
            exit(1); 
        } 
        if (!eject_cd( drive )) exit(1); 
    } 
    exit(0); 


 摘自  tody_guo的专栏
 

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网