Sign in to follow this  
EnigmaticCoder

Adding a MIDI to my console project

Recommended Posts

EnigmaticCoder    296
I have searched the internet for information on this topic, and I have found a lot of resources. It seems that using MCI is the best option, but I keep receiving errors when I try to compile this code:

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

int _tmain(int argc, _TCHAR* argv[])
{
  	const LPCTSTR test = _T("open \"C:\\Documents and Settings\\John\\My Documents\\Visual Studio 2005\\Projects\\MidiTest2\\MidiTest2\\mySongForGame2.mid\" type sequencer alias mySong");
	std::string szFileName = "C:\\Documents and Settings\\John\\My Documents\\Visual Studio 2005\\Projects\\MidiTest2\\MidiTest2\\mySongForGame2.mid";
	std::string szCommand = "open \"" + szFileName + "\" type sequencer alias mySong" + szFileName;
	
	mciSendString(test, NULL, 0, 0);
	return 0;
}


mciSendString() wont take a string as the first parameter, but I cant seem to convert my string to an LPCTSTR. Here are the errors I get:
1>MidiTest2.obj : error LNK2019: unresolved external symbol __imp__mciSendStringW@16 referenced in function _wmain

1>C:\Documents and Settings\John\My Documents\Visual Studio 2005\Projects\MidiTest2\Debug\MidiTest2.exe : fatal error LNK1120: 1 unresolved externals



Any help would be appreciated, and I don't mind reading through something if it will lead me to the answer. P.S. I've looked at this tutorial already: http://www.gamedev.net/reference/articles/article2053.asp I've looked through msdn as well.

Share this post


Link to post
Share on other sites
EnigmaticCoder    296
It now builds fine, but I have a runtime error. It doesn't play the MIDI, unless I have the midi open in windows media player. Pretty funny if you ask me! :)

Here's the updated code:


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

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

int _tmain(int argc, _TCHAR* argv[])
{
const LPCTSTR open = _T("open \"C:\\MySongForGame2.mid\" type sequencer alias mySong");

mciSendString(open, NULL, 0, 0);

const LPCTSTR play = _T("play mySong from 0");
mciSendString(play, NULL, 0, 0);

int wait;
std::cin >> wait;

const LPCTSTR stop = _T("stop mySong");
mciSendString(stop, NULL, 0, 0);

const LPCTSTR unload = _T("close mySong");
mciSendString(unload, NULL, 0, 0);
return 0;
}




Am I forgetting something? Do I have to initialize a midi player?

Thanks in advance!

EDIT: By the way, is there a better way to link the program than using #pragma comment(lib, *.lib)?

Share this post


Link to post
Share on other sites
EnigmaticCoder    296
Fixed it. There was a silly mistake in there:

For those who are interested, I needed one LPCTSTR instead of four.

Here's the updated code:


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

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

int _tmain(int argc, _TCHAR* argv[])
{
LPCTSTR playSong = _T("open \"C:\\MySongForGame2.mid\" type sequencer alias mySong");

mciSendString(playSong, NULL, 0, 0);

playSong = _T("play mySong from 0");
mciSendString(playSong, NULL, 0, 0);

int wait;
std::cin >> wait;

playSong = _T("stop mySong");
mciSendString(playSong, NULL, 0, 0);

playSong = _T("close mySong");
mciSendString(playSong, NULL, 0, 0);
return 0;
}

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