title: Converting UnicodeString to PAnsiChar in Delphi XE
body: <p>In Delphi XE I am using the <a href="http://www.un4seen.com/" rel="nofollow noreferrer">BASS audio library</a>, which contains this function:</p>
  
  <pre><code>function BASS_StreamCreateURL(url: PAnsiChar; offset: DWORD; flags: DWORD; 
      proc: DOWNLOADPROC; user: Pointer):HSTREAM; stdcall; external bassdll;
  </code></pre>
  
  <p>The 'url' parameter is of type PAnsiChar, so in my code I do a cast:</p>
  
  <pre><code>FStreamHandle := BASS_StreamCreateURL(PAnsiChar( url ) [...]
  </code></pre>
  
  <p>The compiler emits a warning on this line: "suspicious typecast of string to PAnsiChar". In trying to eliminate the warning, I <a href="https://stackoverflow.com/questions/283759/convert-string-to-pansichar-in-delphi-2009">found that</a> the recommended way is to use a double cast:</p>
  
  <pre><code>FStreamHandle := BASS_StreamCreateURL(PAnsiChar( AnsiString( url )) [...]
  </code></pre>
  
  <p>This does eliminate the warning, but the BASS function now returns error code 2 ("cannot open file"), which tells me the URL string it receives is somehow broken. I cannot see what the bass DLL actually receives, but using a breakpoint in the debugger the string looks good:</p>
  
  <pre><code>var
    s : PAnsiChar;
  begin
    s := PAnsiChar( AnsiString( url ));
  </code></pre>
  
  <p>At this point string s appears fine, but the BASS function fails when I pass it. My initial code: PAnsiChar( url ) works well with BASS, but emits a warning.</p>
  
  <p>So what's the correct way of getting from UnicodeString to PAnsiChar without a warning?</p>