code wont compile

Started by
12 comments, last by Mike.Popoloski 16 years, 3 months ago
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;
		}
	}
}

Advertisement
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.
after I did that it gives me
Linking...open.obj : error LNK2019: unresolved external symbol __imp__mciSendStringA@16 referenced in function _main

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.
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")
Mike Popoloski | Journal | SlimDX
oh alright thanks guys =)
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
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.
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.
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.

This topic is closed to new replies.

Advertisement