我只是尝试编写简单的程序进行一些练习。但是 iTunes COM for Windows SDK 让我有点意外。我想创建播放列表并向其中添加文件,但是当我调用 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");
我不明白为什么会这样。