Click here to Skip to main content
15,890,845 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
#include <tchar.h>
#include <windows.h>
#include <mmsystem.h> // for MCI function

//link to winmm.lib (usually included in project setting)
#pragma comment(lib, "winmm")

void ControlCdTray(TCHAR drive, DWORD command)
{
	//Not used here, only for debug
	MCIERROR mciError=0;
	//flags forMCI command
	DWORD mciFlags=MCI_WAIT|MCI_OPEN_SHAREABLE|
	MCI_OPEN_TYPE|MCI_OPEN_TYPE_ID|MCI_OPEN_ELEMENT;
	
	//open drive device and get device ID
	TCHAR elementName[]={drive};
	MCI_OPEN_PARMS mciOpenParms={0};
	mciOpenParms.lpstrDeviceType=(LPCTSTR)MCI_DEVTYPE_CD_AUDIO;
	mciOpenParms.lpstrElementName=elementName;
	mciError=mciSendCommand(0,
	 MCI_OPEN, mciFlags, (DWORD_PTR)&mciOpenParms);
	
	//Ejecting or closing tray using device ID
	MCI_SET_PARMS mciSetParms={0};
	mciFlags=MCI_WAIT|command;//command is send by caller
	mciError=mciSendCommand(mciOpenParms.wDeviceID,
	MCI_SET, mciFlags, (DWORD_PTR)&mciSetParms);
	
	//close device ID
	mciFlags=MCI_WAIT;
	MCI_GENERIC_PARMS mciGenericParms={0};
	mciError=mciSendCommand(mciOpenParms.wDeviceID,
	 MCI_CLOSE, mciFlags,(DWORD_PTR)&mciGenericParms);
}
	//Eject drive tray
	void EjectCdTray(TCHAR drive)
	{
		ControlCdTray(drive, MCI_SET_DOOR_CLOSED);
	}
	
	//Retract drive tray
	void CloseCdTray(TCHAR drive)
	{
		ControlCdTray(drive,MCI_SET_DOOR_CLOSED);
	}
	int_tmain(int argc,_TCHAR* argv[])
	{
		EjectCdTray(TEXT('E')); //driveletter hardcoded
		CloseCdTray(TEXT('E'));
		return 0;
	}
Posted

It is clearly some linker or project issues. My tip is, that you have some mismatch in your project settings for your build target. The easiest way is to start a new project (with the correct settings) and copy all your code or compare such project settings with yours. I guess you made a dll project but want a console solution.
 
Share this answer
 
Please note
Quote:
int_tmain(int argc,_TCHAR* argv[])

Should be instead
int _tmain(int argc,_TCHAR* argv[])
 
Share this answer
 
CPallini your solution solves the problem of winmain@16. However, this line of code is still problematic. I dont know what is wrong. Brings "undefined reference to 'mciSendCommandA@16'

mciError = mciSendCommand(mciOpenParms.wDeviceID,MCI_CLOSE, mciFlags,(DWORD_PTR)&mciGenericParms);
 
Share this answer
 
Comments
Richard MacCutchan 6-May-15 5:53am    
See the documentation at https://msdn.microsoft.com/en-us/library/dd757160(v=vs.85).aspx; you need to include Winmm.lib in your linker inputs.
Insert in your link flow the library 'winmm.lib', or use the #pragmas:
C++
#pragma lib ("winmm.lib")    //If supported by compiler
// or
#pragma comment(lib, "winmm.lib")
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900