Fixes #529 - uses @cwford's P/Invoke code