Well, it didn't work. Amended the code to show the system reason. When "drn" contains, "e:", the actual optical drive ID, the system message is "Access denied." With anything else it reports can't find the file or the path. To "\\e:" it reported couldn't find the network drive. "e:" worked with an earlier call to GetVolumeInformation, so I conclude that must be the expected name format.
"Access denied" smacks of administrative privilege. So I ran the program under Task Manager, giving it administrative privilege. Same result: "Access denied."
Guess when running this program I'll have to keep a File Manager window open to click "eject." Sigh.
Code:
#include <windows.h>
#include <winioctl.h>
#include <tchar.h>
#include <stdio.h>
/* Eject optical disk */
void eject(drn)
char *drn; /* drive ID letter (& :) */
{
HANDLE hvol;
int dwfl;
DWORD byr;
char msbuf[256];
/* entry */
hvol = CreateFile(drn,GENERIC_READ,
0,0,OPEN_EXISTING,0,0);
if (hvol == INVALID_HANDLE_VALUE) {
byr = GetLastError();
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,0,byr,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),msbuf,256,0);
puts(msbuf);
printf("Manually eject %s\n",drn);
return;
}
DeviceIoControl(hvol,IOCTL_STORAGE_EJECT_MEDIA,0,0,0,0,&byr,0);
CloseHandle(hvol);
return;
}