简体   繁体   English

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

[英]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(). 我试图将一个简单的.mp3文件添加到iTunes库中,但是当我调用AddFile()时,程序始终崩溃。 However, when I call get_Tracks() it returns a valid pointer, so I suppose the pointer to IITLibraryPlaylist is valid. 但是,当我调用get_Tracks()时,它返回一个有效的指针,因此我认为指向IITLibraryPlaylist的指针是有效的。 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 . 问题是您传递WCHAR*而不是正确分配的BSTR这导致未定义的行为

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. 您应该首先使用SysAllocString()分配BSTR (不要忘记稍后释放该字符串),或者最好使用ATL::CComBSTR_bstr_t类的包装器类来管理BSTR寿命。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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