Here is a very simple C++ console application that writes some information about the currently playing song to stdout. Note that it requires ATL which does not work all that well with Visual C++ 2005 Express Edition, though the code can be converted to plain COM code. On the other hand, it compiles under VS2003 or even VC6 (untested). You also have to link foobar2000_i.c from the comserver2/include directory with your code.
stdafx.h:
#pragma once
#include <iostream>
#include <tchar.h>
#define _ATL_CSTRING_EXPLICIT_CONSTRUCTORS // some CString constructors are explicit
#include <atlbase.h>
#include <foobar2000_h.h> // add comserver2/include to your project's include directories
cpptest.cpp:
// cpptest.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
static void g_report_error(HRESULT hRes, LPCTSTR pszInfo) {
HLOCAL hBuffer = NULL;
if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
NULL, hRes, MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), (LPTSTR)(LPVOID)&hBuffer, 0, NULL) > 0) {
LPCTSTR pszMessage = (LPCTSTR)::LocalLock(hBuffer);
if (pszMessage != NULL) {
_tprintf(_T("%s: %s"), pszInfo, pszMessage);
::LocalUnlock(hBuffer);
}
} else {
_tprintf(_T("%s: 0x%08X"), pszInfo, hRes);
}
if (hBuffer != NULL)
::LocalFree(hBuffer);
}
static void g_test() {
CComPtr<IVBApplication> pApp;
HRESULT hRes = pApp.CoCreateInstance(CLSID_Application07);
if (FAILED(hRes)) {
g_report_error(hRes, _T("Object could not be created"));
return;
}
CComPtr<IVBPlayback> pPlayback;
hRes = pApp->get_Playback(&pPlayback);
if (FAILED(hRes)) {
g_report_error(hRes, _T("Playback control could not be accessed"));
return;
}
CComBSTR strFormat;
strFormat = L"%artist% - %title%";
CComBSTR strResult;
hRes = pPlayback->FormatTitle(strFormat, &strResult);
if (FAILED(hRes)) {
g_report_error(hRes, _T("Title formatting failed"));
return;
}
#ifdef _UNICODE
_tprintf(strResult);
#else
#error port me!
#endif
}
int _tmain(int argc, _TCHAR* argv[]) {
::CoInitialize(NULL);
// TODO parse arguments
g_test();
::CoUninitialize();
return 0;
}