I am using some unmanaged code like-
[DllImport("wininet.dll")]
private extern static bool InternetGetConnectedState(out int Description, int ReservedValue);
//Creating a function that uses the API function...
public static bool IsConnectedToInternet() {
int Desc;
return InternetGetConnectedState(out Desc, 0);
}
Any suggestions on how I can dispose/cleanup this extern static object when I call Dispose?
Now, it is important to note that the garbage collector cleans and reclaims unused managed objects only. It does not clean unmanaged objects.
There are different ways to cleanup unmanaged resources: Implement IDisposable interface and Dispose method. 'using' block is also used to clean unmanaged resources.
A: Automatic way by Finalizer and GC System. Object declares a virtual method Finalize that is called by the GC before the object's memory is reclaimed by the GC and can be overridden to release unmanaged resources.
The thing you think is an 'extern static object' is not an object at all, it's just a set of instructions to the compiler/runtime about how to find a function in a DLL.
As Sander says, there's nothing to clean up.
You do not have any handles to unmanaged resources here. There is nothing to clean up.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With