简体   繁体   中英

iTunes COM interface - Cannot add song to library

I'm trying to add a simple .mp3 file to the iTunes library but my program keeps crashing when I call AddFile(). However, when I call get_Tracks() it returns a valid pointer, so I suppose the pointer to IITLibraryPlaylist is valid. What am I doing wrong?

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
}

The problem is you pass WCHAR* instead of properly allocated BSTR and that leads to undefined behavior .

You should first allocate a BSTR using SysAllocString() (don't forget to release the string later) or better yet use a wrapper class like ATL::CComBSTR or _bstr_t for managing BSTR lifetime.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM