• 0

[C++] iTunes COM Interface


Question

well, i'm trying to make a DLL that uses the iTunes COM Interface (the SDK can be found here), and when i try to create an IiTunes object as described in the documentation, I get the following error:

error C2259: 'IiTunes' : cannot instantiate abstract class

that's the code i used:

#include "iTunesCOMInterface.h"

HRESULT hRes;
IiTunes iITunes;

hRes = ::CoCreateInstance(CLSID_iTunesApp, NULL, CLSCTX_LOCAL_SERVER, IID_IiTunes, (PVOID *)&iITunes);

Can anyone explain me how to access the interface through C++?

Thanks a lot!

Link to comment
https://www.neowin.net/forum/topic/170611-c-itunes-com-interface/
Share on other sites

5 answers to this question

Recommended Posts

  • 0

I don't know much about COM, but I think IDispatch inherited objects are abstract, so when you define the object like you have:

IiTunes iITunes;

it attempts to make an instance of, what is an abstract class. Try making a pointer to it instead

IiTunes* iITunes;

And see if that makes a difference.

  • 0
when i use
IiTunes* iITunes;

the program crashes when i try to use the object.

586688113[/snapback]

You should always initialise a pointer to NULL and the program is crashing because your pointer is pointing to random memory, you have not created an instance of the object so you need to use the COCreateInstance function as shown in the first post to create an instance of the object.

  • 0

This should work:


int main()
{
IiTunes* piTunes;
CoInitialize(0);

if (FAILED(CoCreateInstance(CLSID_iTunesApp, NULL, CLSCTX_LOCAL_SERVER, IID_IiTunes, (void **)&piTunes))) //create iTunes object
{
	MessageBox(0,"Failed to initialize iTunes","Error",0);
	return 0;
}

piTunes->Play(); //play the current song

piTunes->Release(); //release iTunes object

CoUninitialize();

return 0;
}

This code plays current iTunes track.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.