title: iTunes COM for Windows SDK IITUserPlaylist::AddFile access violation
body: <p>I am just try to write simple program for some practice. But iTunes COM for Windows SDK threw me a little surprise.
  I am want to create playlist and add file to it but when i call IITUserPlaylist::AddFile program crash with access violation. I can all other methods like Delete etc.</p>
  
  <p>Project (MSVC10): <a href="http://www76.zippyshare.com/v/34795637/file.html" rel="nofollow">http://www76.zippyshare.com/v/34795637/file.html</a></p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;windows.h&gt;
  #include &lt;comutil.h&gt;
  #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 *)&amp;itunes);
  if(hRes != S_OK &amp;&amp; itunes == nullptr)
  {
      std::cout &lt;&lt; "Can not create iTunesApp instance" &lt;&lt; std::endl;
      CoUninitialize();
      return 1;
  }
  
  IITLibraryPlaylist* mainLibrary = 0;
  hRes = itunes-&gt;get_LibraryPlaylist(&amp;mainLibrary);
  if(hRes != S_OK || mainLibrary == 0)
  {
      std::cout &lt;&lt; "Can not get main library" &lt;&lt; std::endl;
  
      itunes-&gt;Release();
      CoUninitialize();
      return 1;
  }
  
  
  IITSource* mainLibrarySource = 0;
  hRes = itunes-&gt;get_LibrarySource(&amp;mainLibrarySource);
  if(hRes != S_OK || mainLibrarySource == 0)
  {
      std::cout &lt;&lt; "Can not get library source" &lt;&lt; std::endl;
  
      mainLibrary-&gt;Release();
      itunes-&gt;Release();
      CoUninitialize();
      return 1;
  }
  
  IITPlaylistCollection* playlistCollection = 0;
  hRes = mainLibrarySource-&gt;get_Playlists(&amp;playlistCollection);
  if(hRes != S_OK || playlistCollection == 0)
  {
      std::cout &lt;&lt; "Can not get source playlists" &lt;&lt; std::endl;
  
      mainLibrarySource-&gt;Release();
      mainLibrary-&gt;Release();
      itunes-&gt;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-&gt;get_ItemByName(playlistName, &amp;ownPlaylist);
  if(hRes != S_OK || ownPlaylist == 0)
  {
      //just create new
      hRes = itunes-&gt;CreatePlaylist(playlistName, &amp;ownPlaylist);
      if(hRes != S_OK || ownPlaylist == 0)
      {
          std::cout &lt;&lt; "Can not create playlist";
  
          SysFreeString(playlistName);
  
          playlistCollection-&gt;Release();
          mainLibrarySource-&gt;Release();
          mainLibrary-&gt;Release();
          itunes-&gt;Release();
          CoUninitialize();
          return 1;
      }
  }
  SysFreeString(playlistName);
  
  ITPlaylistKind playlistKind;
  hRes = ownPlaylist-&gt;get_Kind(&amp;playlistKind);
  if(hRes == S_OK &amp;&amp; playlistKind == ITPlaylistKindUser)
  {
      IITUserPlaylist* ownUserPlaylist = static_cast&lt;IITUserPlaylist*&gt;(ownPlaylist);
      if(ownUserPlaylist != 0)
      {
          BSTR filePath = SysAllocString(L"C:\\test.mp3");
          IITOperationStatus* status;
          //hRes = mainLibrary-&gt;AddFile(filePath, &amp;status);
          //hRes = ownUserPlaylist-&gt;AddFile(filePath, &amp;status);
          SysFreeString(filePath);
      }
  }
  
  playlistCollection-&gt;Release();
  ownPlaylist-&gt;Release();
  mainLibrarySource-&gt;Release();
  mainLibrary-&gt;Release();
  itunes-&gt;Release();
  
  CoUninitialize();
  
  return 0;
  </code></pre>
  
  <p>}</p>
  
  <p>If i call IITLibraryPlaylist::AddFile all was right.
  Just uncomment 95 or 96 line for test.</p>
  
  <p>Simple C# code works fine:</p>
  
  <pre><code>iTunesApp itunes = new iTunesLib.iTunesApp();
  IITUserPlaylist playlist = itunes.CreatePlaylist("C# playlist") as IITUserPlaylist;
  IITOperationStatus status = playlist.AddFile("C:\\test.mp3");
  </code></pre>
  
  <p>I can not understand why this is happening.</p>