Yes this is the correct way to do this.
See this post for reference Just what is an IntPtr exactly?
It would be best to wrap such uses of unmanaged resources in RAII pattern Implementing RAII in C#