참조 링크 : http://forums.codeguru.com/showthread.php?263490-C-Source-For-Ejecting-CD-ROM


 설정하는 참조 링크에 존재하는 소스


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
#include <windows.h>
#include <winioctl.h>
#include <stdio.h>
 
#define LOCK_TIMEOUT  10000       // 10 Seconds
#define LOCK_RETRIES  20
 
HANDLE OpenVolume(TCHAR cDriveLetter)
{
    HANDLE hVolume;
    UINT   uDriveType;
    char   szVolumeName[8];
    char   szRootName[5];
    DWORD  dwAccessFlags;
   
    sprintf(szRootName, "%c:\\", cDriveLetter);
 
    uDriveType = GetDriveType(szRootName);
    switch(uDriveType) {
      case DRIVE_REMOVABLE:
           dwAccessFlags = GENERIC_READ | GENERIC_WRITE;
           break;
      case DRIVE_CDROM:
           dwAccessFlags = GENERIC_READ;
           break;
      default:
           printf("Cannot eject.  Drive type is incorrect.\n");
           return INVALID_HANDLE_VALUE;
    }
 
    sprintf(szVolumeName, "\\\\.\\%c:", cDriveLetter);
 
    hVolume = CreateFile( szVolumeName,
                          dwAccessFlags,
                          FILE_SHARE_READ | FILE_SHARE_WRITE,
                          NULL,
                          OPEN_EXISTING,
                          0,
                          NULL );
 
    if (hVolume == INVALID_HANDLE_VALUE)
      printf("CreateFile error %d\n", GetLastError());
    return hVolume;
}
 
BOOL CloseVolume(HANDLE hVolume)
{
    return CloseHandle(hVolume);
}
 
BOOL LockVolume(HANDLE hVolume)
{
    DWORD dwBytesReturned;
    DWORD dwSleepAmount;
    int nTryCount;
 
    dwSleepAmount = LOCK_TIMEOUT / LOCK_RETRIES;
 
    // Do this in a loop until a timeout period has expired
    for (nTryCount = 0; nTryCount < LOCK_RETRIES; nTryCount++) {
      if (DeviceIoControl(hVolume,
                          FSCTL_LOCK_VOLUME,
                          NULL0,
                          NULL0,
                          &dwBytesReturned,
                          NULL))
         return TRUE;
 
      Sleep(dwSleepAmount);
    }
    return FALSE;
}
 
BOOL DismountVolume(HANDLE hVolume)
{
    DWORD dwBytesReturned;
 
    return DeviceIoControl( hVolume,
                            FSCTL_DISMOUNT_VOLUME,
                            NULL0,
                            NULL0,
                            &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),
                            NULL0,
                            &dwBytesReturned,
                            NULL);
}
 
AutoEjectVolume(HANDLE hVolume)
{
    DWORD dwBytesReturned;
 
    return DeviceIoControl( hVolume,
                            IOCTL_STORAGE_EJECT_MEDIA,
                            NULL0,
                            NULL0,
                            &dwBytesReturned,
                            NULL);
}
 
BOOL EjectVolume(TCHAR cDriveLetter)
{
    HANDLE hVolume;
 
    BOOL fRemoveSafely = FALSE;
    BOOL fAutoEject = FALSE;
 
    hVolume = OpenVolume(cDriveLetter);
    if (hVolume == INVALID_HANDLE_VALUE)
      return FALSE;
 
    // Lock and dismount the volume.
    if (LockVolume(hVolume) && DismountVolume(hVolume)) {
      fRemoveSafely = TRUE;
      // Set prevent removal to false and eject the volume.
      if (PreventRemovalOfVolume(hVolume, FALSE) && AutoEjectVolume(hVolume))
        fAutoEject = TRUE;
    }
 
    // Close the volume so other processes can use the drive.
    if (!CloseVolume(hVolume))
      return FALSE;
 
    if (fAutoEject)
      printf("Media in Drive %c has been ejected safely.\n", cDriveLetter);
    else {
      if (fRemoveSafely)
        printf("Media in Drive %c can be safely removed.\n", cDriveLetter);
    }
    return TRUE;
}
 
void Usage()
{
    printf("Usage: Eject <drive letter>\n\n");
    return ;
}
 
void main(int argc, char * argv[])
{
    if (argc != 2) {
      Usage();
      return ;
    }
 
    if (!EjectVolume(argv[1][0]))
      printf("Failure ejecting drive %c.\n", argv[1][0]);
 
    return ;
}
cs


'연습' 카테고리의 다른 글

PowerShell CD Eject/Close 설정하는 방법.  (0) 2017.01.03
7z 사용법 -압축방법-  (0) 2017.01.02
최신 코덱인 AV1 현황.  (0) 2016.12.24
powershell 비프음 발생시키기.  (0) 2016.11.15
구글 드라이브 검색 사용법  (0) 2016.11.14
Posted by JunkMam
,