用于Windows SDK的iTunes COM IITUserPlaylist :: AddFile访问冲突

时间:2013-04-17 11:01:25

标签: c com itunes-sdk

我只是尝试为一些练习编写简单的程序。但是用于Windows SDK的iTunes COM让我有些意外。 我想创建播放列表并向其添加文件但是当我调用IITUserPlaylist :: AddFile程序时会发生访问冲突。我可以使用其他所有方法,例如删除等。

项目(MSVC10):http://www76.zippyshare.com/v/34795637/file.html

#include <iostream>
#include <windows.h>
#include <comutil.h>
#include "iTunesCOMInterface.h"

int main(int argc, char* argv[])
{
CoInitialize(0);

HRESULT  hRes;
IiTunes* itunes = 0;

// note - CLSID_iTunesApp and IID_IiTunes are defined in iTunesCOMInterface_i.c
hRes = ::CoCreateInstance(CLSID_iTunesApp, NULL, CLSCTX_LOCAL_SERVER, IID_IiTunes, (PVOID *)&itunes);
if(hRes != S_OK && itunes == nullptr)
{
    std::cout << "Can not create iTunesApp instance" << std::endl;
    CoUninitialize();
    return 1;
}

IITLibraryPlaylist* mainLibrary = 0;
hRes = itunes->get_LibraryPlaylist(&mainLibrary);
if(hRes != S_OK || mainLibrary == 0)
{
    std::cout << "Can not get main library" << std::endl;

    itunes->Release();
    CoUninitialize();
    return 1;
}


IITSource* mainLibrarySource = 0;
hRes = itunes->get_LibrarySource(&mainLibrarySource);
if(hRes != S_OK || mainLibrarySource == 0)
{
    std::cout << "Can not get library source" << std::endl;

    mainLibrary->Release();
    itunes->Release();
    CoUninitialize();
    return 1;
}

IITPlaylistCollection* playlistCollection = 0;
hRes = mainLibrarySource->get_Playlists(&playlistCollection);
if(hRes != S_OK || playlistCollection == 0)
{
    std::cout << "Can not get source playlists" << std::endl;

    mainLibrarySource->Release();
    mainLibrary->Release();
    itunes->Release();
    CoUninitialize();
    return 1;
}

BSTR playlistName = SysAllocString(L"SoundFrost playlist");

IITPlaylist* ownPlaylist = 0;
//TODO: But there can be additional playlists with the same name, since playlist names are not unique
hRes = playlistCollection->get_ItemByName(playlistName, &ownPlaylist);
if(hRes != S_OK || ownPlaylist == 0)
{
    //just create new
    hRes = itunes->CreatePlaylist(playlistName, &ownPlaylist);
    if(hRes != S_OK || ownPlaylist == 0)
    {
        std::cout << "Can not create playlist";

        SysFreeString(playlistName);

        playlistCollection->Release();
        mainLibrarySource->Release();
        mainLibrary->Release();
        itunes->Release();
        CoUninitialize();
        return 1;
    }
}
SysFreeString(playlistName);

ITPlaylistKind playlistKind;
hRes = ownPlaylist->get_Kind(&playlistKind);
if(hRes == S_OK && playlistKind == ITPlaylistKindUser)
{
    IITUserPlaylist* ownUserPlaylist = static_cast<IITUserPlaylist*>(ownPlaylist);
    if(ownUserPlaylist != 0)
    {
        BSTR filePath = SysAllocString(L"C:\\test.mp3");
        IITOperationStatus* status;
        //hRes = mainLibrary->AddFile(filePath, &status);
        //hRes = ownUserPlaylist->AddFile(filePath, &status);
        SysFreeString(filePath);
    }
}

playlistCollection->Release();
ownPlaylist->Release();
mainLibrarySource->Release();
mainLibrary->Release();
itunes->Release();

CoUninitialize();

return 0;

}

如果我打电话给IITLibraryPlaylist :: AddFile,那一切都是正确的。 只需取消注释95或96行进行测试。

简单的C#代码工作正常:

iTunesApp itunes = new iTunesLib.iTunesApp();
IITUserPlaylist playlist = itunes.CreatePlaylist("C# playlist") as IITUserPlaylist;
IITOperationStatus status = playlist.AddFile("C:\\test.mp3");

我无法理解为什么会这样。

1 个答案:

答案 0 :(得分:1)

你无法用COM真正做到这一点:

IITUserPlaylist* ownUserPlaylist = static_cast<IITUserPlaylist*>(ownPlaylist);

您只是将接口指针转换为另一个接口指针。您必须改为QueryInterface

HRESULT hr = ownPlaylist->QueryInterface(IID_IITUserPlaylist, (void**)&ownUserPlaylist);