In the BASS.Net doc section "Interoperating with Unmanaged Code" is described how to pass an instance of an arbitrary class to a callback using a pinned GCHandle.
Apart from the fact that this seems not to be possible if the arbitrary class contains any non primitive type, there seems to be a much bigger problem, at least in my development environment (using C# in VisualStudio 2010):
I can't convert the IntPtr user data in a callback e.g. a STREAMPROC callback back to it's origin: An Exception is thrown when trying to do so:
private int LoopStreamCallback(int handle, IntPtr buffer, int length, IntPtr user){
GCHandle gch = GCHandle.FromIntPtr(user); // throws exeception
MyClass userValue = (MyClass)gch.Target;
...
}
Exception: "Cannot pass a GCHandle across AppDomains"
at System.Runtime.InteropServices.GCHandle.InternalCheckDomain(IntPtr handle)
at System.Runtime.InteropServices.GCHandle.FromIntPtr(IntPtr value)
...I have searched the internet about this error and everbody says correspondingly : 'There is nothing you can do about it (as a consumer of a 3rd party lib)'
Maybe this is true for WPF apps ?