I have a class which uses a method in user32.dll:
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern bool SetForegroundWindow(IntPtr windowHandlerPtr);
According to Effective C#, should all classes which uses unmanaged code implement both IDisposable and a finalizer. Without going into the details in that discussion, is there a need to dispose this kind of extern method declaration? If so – how would that dispose method look like?
That’s not really a “reference”, but instead a declaration. It doesn’t actually create anything, so no need to dispose (nothing to dispose, in fact).
Using a return value from unmanaged code is something you should watch, though – in this case it’s just a bool that you don’t need to worry about. But in most cases you should look at the unmanaged API’s documentation to see if it should be released somehow. If it is, then wrap that up in a class that you can dispose of properly. Here’s a decent article on IDisposable
A rule of thumb is: If it implements IDisposable always dispose of it, and if it’s an unmanaged resource, make sure it is properly released.