How to get c# object pointer?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • violin

    #1

    How to get c# object pointer?

    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_ob j(c_struct *cstr)
    {
    return cstr->csharp_obj;
    }

    In C#, I define c lib function prototype:

    public extern void SetCCharpObj(In tPtr cStruct, IntPtr
    obj);
    public extern IntPtr GetCCharpObj(In tPtr 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



  • Mattias Sjögren

    #2
    Re: How to get c# object pointer?

    [color=blue]
    >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.[/color]

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

    [color=blue]
    >But when i debug the program, I always get a error.How can I
    >handle this scenario?[/color]

    What error?



    Mattias

    --
    Mattias Sjögren [MVP] mattias @ mvps.org

    Please reply only to the newsgroup.

    Comment

    • Stu Smith

      #3
      Re: How to get c# object pointer?

      You might also want to look up gcroot<T>.


      "violin" <anonymous@disc ussions.microso ft.com> wrote in message
      news:0bd101c3b3 4a$6e412220$a50 1280a@phx.gbl.. .[color=blue]
      > 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_ob j(c_struct *cstr)
      > {
      > return cstr->csharp_obj;
      > }
      >
      > In C#, I define c lib function prototype:
      >
      > public extern void SetCCharpObj(In tPtr cStruct, IntPtr
      > obj);
      > public extern IntPtr GetCCharpObj(In tPtr 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
      >
      >
      >[/color]


      Comment

      Working...