How to get c# object pointer?

V

violin

I want to use c lib to store c# object address,
like this:
In c lib, I wrote a structure, and two method operate on
it.
typedef struct
{
void *csharp_obj;
}c_struct;

void set_c_charp_obj(c_struct *cstr, void *csharpObj)
{
cstr->csharp_obj = csharpObj;
}

void *get_c_charp_obj(c_struct *cstr)
{
return cstr->csharp_obj;
}

In C#, I define c lib function prototype:

public extern void SetCCharpObj(IntPtr cStruct, IntPtr
obj);
public extern IntPtr GetCCharpObj(IntPtr cStruct);

then ,you will understand that I want to get C# object
addr and send it as IntPtr to c lib.so that, I store the
pointer of the c# object in c lib for future use.But when
i debug the program, I always get a error.How can I
handle this scenario?and when I fetch the pointer of c#
object, how can i cast it and use it as a object in c#?

thanks
 
M

Mattias Sjögren

then ,you will understand that I want to get C# object
addr and send it as IntPtr to c lib.so that, I store the
pointer of the c# object in c lib for future use.

Have a look at the GCHandle struct, it lets you get a handle for an
object and cast to IntPtr and back.

But when i debug the program, I always get a error.How can I
handle this scenario?

What error?



Mattias
 

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