c程序打开光驱__2018.09.08

代码:

#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); 
} 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值