Sign in to follow this  
alex9025

code wont compile

Recommended Posts

Hey guys I'm trying to compile this code, but it keeps giving my linker errors... tried DevC++ and VS2008b btw Borland C++ builder compiles it just fine. the error is
error C2664: 'mciSendStringW' : cannot convert parameter 1 from 'const char [13]' to 'LPCWSTR'
        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
#include "stdafx.h"
#include "windows.h"
#include "mmsystem.h"
#include "iostream"

using namespace std;
char choice;
bool quit;
int main()
{
	while (!quit)
	{
		cout << "Please select what you want to do" << endl;
		cout << "[O]pen CD/DVD rom" << endl;
		cout << "[C]lose CD/DVD rom" << endl;
		cout << "[Q]uit" << endl;
		cin  >> choice;
		switch (choice)
		{
			case 'O': case 'o':
				mciSendString("open CDAudio", NULL, 0, NULL);
				mciSendString("set CDAudio door open", NULL, 0, NULL);
				cout << "Its open" << endl;
				break;
			case 'C': case 'c':
				mciSendString("close CDAudio", NULL, 0, NULL);
				mciSendString("set CDAudio door closed", NULL, 0, NULL);
				cout << "Its closed" << endl;
				break;
			case 'Q': case 'q':
				quit = 1;
				break;
			default:
				cout << "You did not enter anything" << endl;
				break;
		}
	}
}

Share this post


Link to post
Share on other sites
In MSVC 2008, projects default to using Unicode versions of the Windows API functions. You can change that by going to Project Properties/Character Set and change it to Use Multi-Byte Character Set.

Share this post


Link to post
Share on other sites
Your includes at the top...


#include "stdafx.h"
#include "windows.h"
#include "mmsystem.h"
#include "iostream"



Shouldn't this be...


#include "stdafx.h"
#include <windows.h>
#include "mmsystem.h"
#include <iostream>



Since windows.h and iostream are in the compiler include path? I'm not sure what mmsystem.h is or if it's in the path as well, or if it's just part of the project.

If it's not either of those, it looks like you're using a library that you're not linking into the build. That is, including the appropriate .H file, but not telling the linker to add in the .lib.

Share this post


Link to post
Share on other sites
mmsystem.h is indeed a system file, although I was under the impression it was automatically included with windows.h. Either way, this wouldn't cause a linker error, just an include compile-time error.

alex9025, the reason you are getting that linker error is that you aren't linking to the library that provides the mciSendString function, which is found in winmm.lib. To include this library, either add it to the list in the options page, or add the following line to one of your source files:

#pragma comment(lib, "winmm.lib")

Share this post


Link to post
Share on other sites
ugh guys I need more help,
after I changed the configuration from debug to release it gives me that same error again
open.cpp(21) : error C2664: 'mciSendStringW' : cannot convert parameter 1 from 'const char [13]' to 'LPCWSTR'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

why is it doing that, only on release and if i change it to debug it compiles just fine

Share this post


Link to post
Share on other sites
You probably need to change the Character Set settings again, they're usually determined per configuration which is why the debug build builds but the release build fails.

Share this post


Link to post
Share on other sites
yeah that fixed it thanks.
but now when I send that program to another pc
(win server 2003)
it gives me an error when I try to run the file
This application has failed to start because the application configuration is incorrect. Reinstalling the application may fix this problem.

Share this post


Link to post
Share on other sites
The google keywords for this very cryptic message is Side by Side (SxS) issues and Visual C++ redistributable.

Your program dynamically links to a number of libraries, including the C and C++ runtime. Since your dynamically linking, the dlls need to be present as well when running the program.

Two ways to make them appear on another computer. Run the redistributable. VS 2008 uses VC 9 redistributable. Or go to the Visual Studio Directory / vc / redist (I think) and copy the CRT and any other needed dlls as well.

Share this post


Link to post
Share on other sites
The only reason your program is 10kb in size if because all of the stuff that makes the program work is moved out into those DLLs. You can usually tell the compiler to not load the DLLs and instead link to the runtime using static linking, which embeds them right into the exe, but that will certainly make it bigger.

Share this post


Link to post
Share on other sites
Sure. My first hit on Google turns up this article on MSDN about the different ways the C runtime library (CRT) can be linked.

If you are looking for the quick and dirty answer, you can change the way your project links to the CR by going to the Project Properties Page->Configuration Properties->C/C++->Code Generation and changing the Runtime Library option to Multithreaded instead of Multithreaded DLL (or Multithreaded Debug for the debug build).

Make sure you update both the Debug and Release versions of your build appropriately; there are different versions of the CRT depending on whether you want debugging support (which you should remove for build that you want to distribute to other machines, and which is also larger than it's release counterpart).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this