open cd tray

im trying to open the cd drive with this code:
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <windows.h> 
#include <mmsystem.h>

using namespace std;

int main()
{
mciSendString("open CDAudio", NULL, 0, NULL);
mciSendString("set CDAudio door open", NULL, 0, NULL);
return 0;
}


but when i compile it, i get these errors
1
2
Error	1	error LNK2019: unresolved external symbol__imp__mciSendStringA@16 referenced in function _main
Error	2	error LNK1120: 1 unresolved externals


my comiler is visual c++ 2010

if anyone knows whats wrong could they please help me

thanks
Last edited on
I'm sorry to be the bearer of bad new but that is NOT what the "open" command does for MCI devices.

As per MSDN:
"The open command initializes a device. All MCI devices recognize this command."
Here:
http://msdn.microsoft.com/en-us/library/dd743638(VS.85).aspx


Initialization is not the same as having the tray physically open itself.

EDIT: I forgot to include the hyperlink.

RE-EDIT: It occured to me that I could be misunderstanding a non-native english speaker in which case if you do understand what this function does and are interested in using it for that purpose I will be glad to help you further.
Last edited on
i just want to eliminate the errors i get when i compile
Ok, then you are sending the wrong arguments. First of all you are forgetting your NULL TERMINATION at the end of the string. Second it's very unlikely that the device_name given to your CD Player is CDAudio. Now that I look at it I don't even think you're passing the parameters for this command correctly.

I haven't used this command before but I'll give it a shot and post what I find in a little while.
Ok so I got rid of that particular error by EXPLICITLY linking to the winmm.lib file. My biggest gripe about MS Visual Studio is that it takes leg work like this out of the equation about %90 of the time, so when you DO run across that other %10 situation it kicks you right in the pants.

The version of winmm.lib that I linked to is in the WinDDK version 7600.16385 under

%InstallationPath%\WinDDK\7600.16385.0\lib\wxp\i386

That is of course because I am running a x86 processor on a Wndows XP system so YMMV. As for the syntax of your command you doing it wrong, see here:

http://msdn.microsoft.com/en-us/library/dd797790(VS.85).aspx

Because of one reason or another I had to change the "stprintf_s(...) command to a straight up "sprintf(...)" command but since you're using Visual C++ 2010 that shouldn't be an issue once you're all linked up. Enjoy and feel free to ask more questions if you have them.

EDIT: Spelling and such, I know there is more to edit but I don't see it right now.

RE-EDIT: By the way I'm using wxDev-C++ so the method of linking ISA different but you still have to DO the link.
Last edited on
ok i did what you said an now i get even more errors

could you just show me what your code looks like or is there another way to open the cd tray

edit: nevermind i fixed it just by using my original code but linking it to winmm.lib
Last edited on
So are we all set? Did you have any more questions?
i wanted to see if the tray would continuously open and close if i looped it like so:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <windows.h> 
#include <mmsystem.h>

using namespace std;

int main()
{
	while(2>1){
		mciSendString("open CDAudio", NULL, 0, NULL);
		mciSendString("set CDAudio door open", NULL, 0, NULL);
		mciSendString("set CDAudio door closed", NULL, 0, NULL);
	}
	return 0;
}


it worked but my computer crashed (probably because i set it in an infinite loop) is there a way to loop it without that happening.
If you're looping infinitely (which I don't recommend), while (true) is a little more coherent.

--------

I don't know much about the mciSendString but according to MSDN what you're doing is spamming the device with notifications & it doesn't surprise me that it's crashing. One solution would be to use the Sleep function to send the commands on some kind of interval.

Also, if you're opening CDAudio then you'd probably want to close it, too.
http://msdn.microsoft.com/en-us/library/dd797784%28v=VS.85%29.aspx

Why do you want to open and close the CD tray in a loop anyway?

--------

I use the [tt] tag too much...
wow..i see my friend doing such coding as he wanted to do the same opening cd tray by just programming but he could not find a way how to do it,..thanks for sharing some techniques and coding. i'll be recommending this site to him.
thank you :)
i wanted to loop it for the LOLs.
Topic archived. No new replies allowed.