How to get a handle of a object in c#

  • Thread starter Thread starter violin
  • Start date Start date
V

violin

I want to get the address of a c# object,and store that
address in a IntPtr value, like we use handle in VC6.How
can i achieve it?
 
What are you trying to do exactly? There are a couple of possible different
scenarios here.

Maybe you can just use an object ref.
If you just want a cookie, you could consider using a GCHandle.
If you want to manipulate the contents using pointers, or pass to unmanaged
code, you can pin the object using the fixed statement, or perhaps you need
some marshaling/interop help.

Some more info about what you want to do would enable us to help you better.
 
Hi,

In VC6, we have pointers that actually hold addresses in RAM, and we have
handles that identify Windows kernel, GDI and user objects like files, pens,
memory mappings etc.

So it is unclear which of them do you actully need and what is your ultimate
goal. May be we could suggest a more efficient approach if you would tell us
what do you want to do with the address of a managed object instance?
 
thank all of you to have patience to answer my question.
actually,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
 
Back
Top