public static IntPtr SecureStringToCoTaskMemAnsi (SecureString s)
{
if (s == null)
throw new ArgumentNullException ("s");
int len = s.Length;
IntPtr ctm = AllocCoTaskMem (len + 1);
byte [] copy = new byte [len+1];
try {
byte [] buffer = s.GetBuffer ();
int i = 0, j = 0;
for (; i < len; i++, j += 2){
copy [i] = buffer [j+1];
buffer [j] = 0;
buffer [j+1] = 0;
}
copy [i] = 0;
copy_to_unmanaged (copy, 0, ctm, len+1);
} finally {
// Ensure that we clear the buffer.
for (int i = len; i > 0; ){
i--;
copy [i] = 0;
}
}
return ctm;
}