From: Atmapuri on
Hi!

I have an unmanaged function:

void Fun1(void * Delegate1, void * Param);

which I call from managed code via PInvoke.
The managed Delegate1 is then called from unmanaged code
like this:

public static void Delegate1(void * Param)
{
...
}

This all works fine. The problem is that the "Param" I need
to pass to Fun1 is a big and complex managed object. If I don't pass it
to the function, I have to use global variable and the routine
is not thread safe. The Fun1 does end until all Delegate1 calls
have finished. (blocking call).

The question is therefore:

How can you pass a pointer to a managed object and have that
managed object than available within Delegate1 as the true CLR
object? If I define the type directly, the program simply halts.

Thanks!
Atmapuri





From: Peter Duniho on
Atmapuri wrote:
> [...]
> The question is therefore:
>
> How can you pass a pointer to a managed object and have that
> managed object than available within Delegate1 as the true CLR
> object? If I define the type directly, the program simply halts.

Without a concise-but-complete code example, it's difficult to know
exactly what you're doing, never mind what's wrong.

But, passing a raw _pointer_ to the managed object should work fine, as
long as you ensure that the managed object remains pinned for as long as
the unmanaged code needs to be able to use it.

I'm no interop expert, but I believe you'll have to cast the pointer
when passing it to ensure that the interop doesn't try to marshal the value.

Of course, there may be a way to have the object marshaled across the
interop boundary, but it sounds like you don't want to have to do that.
It certainly would be more complicated to do it that way, especially
given that it appears the unmanaged code doesn't actually need to access
the object.

Pete
From: Fábio Chicout on
Post this into microsoft.public.dotnet.framework.interop

Att,
F�bio Chicout

"Peter Duniho" <no.peted.spam(a)no.nwlink.spam.com> wrote in message
news:eDUoLwQoKHA.5224(a)TK2MSFTNGP05.phx.gbl...
> Atmapuri wrote:
>> [...]
>> The question is therefore:
>>
>> How can you pass a pointer to a managed object and have that
>> managed object than available within Delegate1 as the true CLR
>> object? If I define the type directly, the program simply halts.
>
> Without a concise-but-complete code example, it's difficult to know
> exactly what you're doing, never mind what's wrong.
>
> But, passing a raw _pointer_ to the managed object should work fine, as
> long as you ensure that the managed object remains pinned for as long as
> the unmanaged code needs to be able to use it.
>
> I'm no interop expert, but I believe you'll have to cast the pointer when
> passing it to ensure that the interop doesn't try to marshal the value.
>
> Of course, there may be a way to have the object marshaled across the
> interop boundary, but it sounds like you don't want to have to do that. It
> certainly would be more complicated to do it that way, especially given
> that it appears the unmanaged code doesn't actually need to access the
> object.
>
> Pete