20 Jun '13 - 05:20 *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
 
   Home   Help Search Login Register  
Pages: [1]
  Reply  |  Print  
Author Topic: IntPtr user: Cannot pass a GCHandle across AppDomains  (Read 659 times)
norbert
Posts: 33


« on: 15 Jan '12 - 13:21 »
Reply with quoteQuote

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 ?
Logged
radio42
Posts: 4030


« Reply #1 on: 15 Jan '12 - 14:47 »
Reply with quoteQuote

Yes, the class instance needs to be serilizable (contains only blittable types) and can not passed between AppDomains.
In such case you need to find another way to pass the class instance pointer to BASS, e.g. use a hash or integer value which identifies your class instance in a dictionary, list or whatsoever.
Basically don't use a class pointer by itself, but rather a kind of index.
Logged
Pages: [1]
  Reply  |  Print  
 
Jump to:  

Powered by SMF 1.1.18 | SMF © 2013, Simple Machines