IntPtr and fixed

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

    IntPtr and fixed


    Hi All

    Is IntPtr a managed or unmanaged data type in c#

    Do I need to be doing
    Intptr = IPtr
    fixed(void * ptr = (void *)IPtr


    ....


    to avoid relocation? or can I use

    void *ptr = (void *)Iptr without fixed

    thank yo
    vipin
  • Mattias Sjögren

    #2
    Re: IntPtr and fixed

    [color=blue]
    >Is IntPtr a managed or unmanaged data type in c#?[/color]

    Managed. C# only produces managed code, you can't have any unmanaged
    types.

    [color=blue]
    >Do I need to be doing
    >Intptr = IPtr;
    >fixed(void * ptr = (void *)IPtr )
    >{
    >
    >
    >....
    >}
    >
    >to avoid relocation? or can I use
    >
    >
    >void *ptr = (void *)Iptr without fixed?[/color]


    Most likely no, but it depends on what the IntPtr is pointing at. I
    assume you wouldn't have an IntPtr pointing to something managed
    unless you had already pinned it. The first piece of code won't work
    though.



    Mattias

    --
    Mattias Sjögren [MVP] mattias @ mvps.org
    http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
    Please reply only to the newsgroup.

    Comment

    • vipin

      #3
      Re: IntPtr and fixed

      IntPtr points to a View returned by calling DLLImport MapViewOfFIle from kernel32.dll

      I am compiling the code with /unsafe switch

      vipin

      Comment

      • Mattias Sjögren

        #4
        Re: IntPtr and fixed

        [color=blue]
        >IntPtr points to a View returned by calling DLLImport MapViewOfFIle from kernel32.dll.[/color]

        Then there's no need to pin anything. The handle refers to an
        unmanaged resource that's not affected by the GC anyway.



        Mattias

        --
        Mattias Sjögren [MVP] mattias @ mvps.org
        http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
        Please reply only to the newsgroup.

        Comment

        Working...