Unmanaged code interop!

A

Atmapuri

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
 
F

Fábio Chicout

Post this into microsoft.public.dotnet.framework.interop

Att,
Fábio Chicout

Peter Duniho said:
Atmapuri said:
[...]
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top