iTunes COM界面 - 无法将歌曲添加到库中

时间:2011-05-16 12:10:52

标签: c++ com interface itunes

我正在尝试将一个简单的.mp3文件添加到iTunes资料库中,但是当我调用AddFile()时,我的程序会一直崩溃。但是,当我调用get_Tracks()时,它返回一个有效的指针,所以我认为指向IITLibraryPlaylist的指针是有效的。我做错了什么?

IiTunes* p_iTunes;
IITLibraryPlaylist* p_Library;
IITOperationStatus* status;
IITTrackCollection* iTrackCollection;

CoInitialize(0);
if (FAILED(CoCreateInstance(CLSID_iTunesApp, NULL, CLSCTX_LOCAL_SERVER, IID_IiTunes, (PVOID *)&p_iTunes))){
    p_iTunes->Release();
    CoUninitialize();
}
else{
    p_iTunes->get_LibraryPlaylist(&p_Library);

    p_Library->get_Tracks(&iTrackCollection); // This works, so I suppose p_Library is valid..
    long trackCount = 0;
    iTrackCollection->get_Count(&trackCount);

    p_Library->AddFile(L"C:\\asd\asd.mp3",&status); // crashes here
}

1 个答案:

答案 0 :(得分:1)

问题是您通过WCHAR*而不是正确分配BSTRthat leads to undefined behavior

您应首先使用SysAllocString()分配BSTR(不要忘记稍后发布字符串)或者更好地使用包含类ATL::CComBSTR_bstr_t的包装类管理BSTR一生。