managed c++ pinned pointer cast

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

    #1

    managed c++ pinned pointer cast

    I'm trying to co-erce a __gc array of Byte to a __nogc pointer to char to
    pass to a native function call in a bit of managed c++ code like this:

    Byte field __gc[] = dynamic_cast<By te __gc[]>(record->get_Item(i)) ;
    ..
    ..
    ..
    Byte __pin * p = &field[0]; // << this is the proscribed way to pin a gc
    array
    char __nogc * pinfield = p; // << C2440 error here!

    I get the following compilation error:
    : error C2440: 'initializing' : cannot convert from 'unsigned char __pin
    *volatile ' to 'char *'

    Given that p is effectively a no-gc pointer to unsigned char, I can't
    understand why I can't assign it to pinfield - a no-gc pointer to char (i.e.
    exactly the same, just treat the chars as signed instead of unsigned).
    Can anyone explain why I can't do this and how I can achieve what I'm trying
    to please?
    cheers
    Tim

  • Marcus Heege

    #2
    Re: managed c++ pinned pointer cast

    I can't reproduce your problem. The following code compiles:

    <code>
    #using <mscorlib.dll >

    using namespace System;

    int main()
    {
    Byte bytes __gc [] = __gc new Byte __gc [100];
    Byte __pin* ppBytes = &bytes[0];
    Byte* pBytes1 = ppBytes;
    Byte* pBytes2 = static_cast<Byt e*>(ppBytes);
    //Byte* pBytes3 = (Byte*)ppBytes;
    }
    </code>

    If I uncomment the last line, I get a warning:

    test.cpp(11) : warning C4303: C-style cast from 'unsigned char __gc *' to
    'unsigned char __pin *volatile ' is deprecated, use static_cast, _try_cast
    or dynamic_cast

    Even if I modify the __pin* to __pin* volatile, it seems to be the same.

    My tests are done with VC 2003 (CL.EXE V 13.10.3077) what version are you
    using?

    Marcus Heege

    "Tim" <Tim@discussion s.microsoft.com > wrote in message
    news:22C964C9-5840-4C5F-82BE-7E24392BAADD@mi crosoft.com...[color=blue]
    > I'm trying to co-erce a __gc array of Byte to a __nogc pointer to char to
    > pass to a native function call in a bit of managed c++ code like this:
    >
    > Byte field __gc[] = dynamic_cast<By te __gc[]>(record->get_Item(i)) ;
    > .
    > .
    > .
    > Byte __pin * p = &field[0]; // << this is the proscribed way to pin a gc
    > array
    > char __nogc * pinfield = p; // << C2440 error here!
    >
    > I get the following compilation error:
    > : error C2440: 'initializing' : cannot convert from 'unsigned char __pin
    > *volatile ' to 'char *'
    >
    > Given that p is effectively a no-gc pointer to unsigned char, I can't
    > understand why I can't assign it to pinfield - a no-gc pointer to char
    > (i.e.
    > exactly the same, just treat the chars as signed instead of unsigned).
    > Can anyone explain why I can't do this and how I can achieve what I'm
    > trying
    > to please?
    > cheers
    > Tim
    >[/color]


    Comment

    • Tim

      #3
      Re: managed c++ pinned pointer cast

      Your code doesn't include the line that fails - i.e.:

      char __nogc * pinfield = p; // << C2440 error here!

      .... being the bit where I try to cast the pinned Byte pointer to a no-gc
      char pointer.

      I'm using VC 1.0 (2002).

      Tim


      "Marcus Heege" wrote:
      [color=blue]
      > I can't reproduce your problem. The following code compiles:
      >
      > <code>
      > #using <mscorlib.dll >
      >
      > using namespace System;
      >
      > int main()
      > {
      > Byte bytes __gc [] = __gc new Byte __gc [100];
      > Byte __pin* ppBytes = &bytes[0];
      > Byte* pBytes1 = ppBytes;
      > Byte* pBytes2 = static_cast<Byt e*>(ppBytes);
      > //Byte* pBytes3 = (Byte*)ppBytes;
      > }
      > </code>
      >
      > If I uncomment the last line, I get a warning:
      >
      > test.cpp(11) : warning C4303: C-style cast from 'unsigned char __gc *' to
      > 'unsigned char __pin *volatile ' is deprecated, use static_cast, _try_cast
      > or dynamic_cast
      >
      > Even if I modify the __pin* to __pin* volatile, it seems to be the same.
      >
      > My tests are done with VC 2003 (CL.EXE V 13.10.3077) what version are you
      > using?
      >
      > Marcus Heege
      >
      > "Tim" <Tim@discussion s.microsoft.com > wrote in message
      > news:22C964C9-5840-4C5F-82BE-7E24392BAADD@mi crosoft.com...[color=green]
      > > I'm trying to co-erce a __gc array of Byte to a __nogc pointer to char to
      > > pass to a native function call in a bit of managed c++ code like this:
      > >
      > > Byte field __gc[] = dynamic_cast<By te __gc[]>(record->get_Item(i)) ;
      > > .
      > > .
      > > .
      > > Byte __pin * p = &field[0]; // << this is the proscribed way to pin a gc
      > > array
      > > char __nogc * pinfield = p; // << C2440 error here!
      > >
      > > I get the following compilation error:
      > > : error C2440: 'initializing' : cannot convert from 'unsigned char __pin
      > > *volatile ' to 'char *'
      > >
      > > Given that p is effectively a no-gc pointer to unsigned char, I can't
      > > understand why I can't assign it to pinfield - a no-gc pointer to char
      > > (i.e.
      > > exactly the same, just treat the chars as signed instead of unsigned).
      > > Can anyone explain why I can't do this and how I can achieve what I'm
      > > trying
      > > to please?
      > > cheers
      > > Tim
      > >[/color]
      >
      >
      >[/color]

      Comment

      • Norman Diamond

        #4
        Re: managed c++ pinned pointer cast

        In reply to Tim, plain char is a different type from unsigned char. Pointer
        to plain char and pointer to unsigned char do not automatically convert to
        each other. You need a cast. The management disclaims all responsibility
        for this one.

        Concerning Marcus Heege's report, if one line is commented out then the
        warning seems to have its "from" and "to" backwards. The cast is converting
        an rvalue FROM a pointer to pinned type TO a pointer to managed type. Is
        this a bug in a Dutch language version of Visual Studio, a bug in Mr.
        Heege's translation, or a bug in an English language version of Visual
        Studio?

        "Marcus Heege" <NOSPAM@heege.n et> wrote in message
        news:uBQtr8LxFH A.916@TK2MSFTNG P10.phx.gbl...[color=blue]
        >I can't reproduce your problem. The following code compiles:
        >
        > <code>
        > #using <mscorlib.dll >
        >
        > using namespace System;
        >
        > int main()
        > {
        > Byte bytes __gc [] = __gc new Byte __gc [100];
        > Byte __pin* ppBytes = &bytes[0];
        > Byte* pBytes1 = ppBytes;
        > Byte* pBytes2 = static_cast<Byt e*>(ppBytes);
        > //Byte* pBytes3 = (Byte*)ppBytes;
        > }
        > </code>
        >
        > If I uncomment the last line, I get a warning:
        >
        > test.cpp(11) : warning C4303: C-style cast from 'unsigned char __gc *' to
        > 'unsigned char __pin *volatile ' is deprecated, use static_cast, _try_cast
        > or dynamic_cast
        >
        > Even if I modify the __pin* to __pin* volatile, it seems to be the same.
        >
        > My tests are done with VC 2003 (CL.EXE V 13.10.3077) what version are you
        > using?
        >
        > Marcus Heege
        >
        > "Tim" <Tim@discussion s.microsoft.com > wrote in message
        > news:22C964C9-5840-4C5F-82BE-7E24392BAADD@mi crosoft.com...[color=green]
        >> I'm trying to co-erce a __gc array of Byte to a __nogc pointer to char to
        >> pass to a native function call in a bit of managed c++ code like this:
        >>
        >> Byte field __gc[] = dynamic_cast<By te __gc[]>(record->get_Item(i)) ;
        >> .
        >> .
        >> .
        >> Byte __pin * p = &field[0]; // << this is the proscribed way to pin a
        >> gc
        >> array
        >> char __nogc * pinfield = p; // << C2440 error here!
        >>
        >> I get the following compilation error:
        >> : error C2440: 'initializing' : cannot convert from 'unsigned char __pin
        >> *volatile ' to 'char *'
        >>
        >> Given that p is effectively a no-gc pointer to unsigned char, I can't
        >> understand why I can't assign it to pinfield - a no-gc pointer to char
        >> (i.e.
        >> exactly the same, just treat the chars as signed instead of unsigned).
        >> Can anyone explain why I can't do this and how I can achieve what I'm
        >> trying
        >> to please?
        >> cheers
        >> Tim
        >>[/color]
        >
        >[/color]

        Comment

        • Tim

          #5
          Re: managed c++ pinned pointer cast

          I've tried explicit casting and it still complains. It seems quite certain
          that there is some reason why I can't do it.

          Tim

          "Norman Diamond" wrote:
          [color=blue]
          > In reply to Tim, plain char is a different type from unsigned char. Pointer
          > to plain char and pointer to unsigned char do not automatically convert to
          > each other. You need a cast. The management disclaims all responsibility
          > for this one.
          >
          > Concerning Marcus Heege's report, if one line is commented out then the
          > warning seems to have its "from" and "to" backwards. The cast is converting
          > an rvalue FROM a pointer to pinned type TO a pointer to managed type. Is
          > this a bug in a Dutch language version of Visual Studio, a bug in Mr.
          > Heege's translation, or a bug in an English language version of Visual
          > Studio?
          >
          > "Marcus Heege" <NOSPAM@heege.n et> wrote in message
          > news:uBQtr8LxFH A.916@TK2MSFTNG P10.phx.gbl...[color=green]
          > >I can't reproduce your problem. The following code compiles:
          > >
          > > <code>
          > > #using <mscorlib.dll >
          > >
          > > using namespace System;
          > >
          > > int main()
          > > {
          > > Byte bytes __gc [] = __gc new Byte __gc [100];
          > > Byte __pin* ppBytes = &bytes[0];
          > > Byte* pBytes1 = ppBytes;
          > > Byte* pBytes2 = static_cast<Byt e*>(ppBytes);
          > > //Byte* pBytes3 = (Byte*)ppBytes;
          > > }
          > > </code>
          > >
          > > If I uncomment the last line, I get a warning:
          > >
          > > test.cpp(11) : warning C4303: C-style cast from 'unsigned char __gc *' to
          > > 'unsigned char __pin *volatile ' is deprecated, use static_cast, _try_cast
          > > or dynamic_cast
          > >
          > > Even if I modify the __pin* to __pin* volatile, it seems to be the same.
          > >
          > > My tests are done with VC 2003 (CL.EXE V 13.10.3077) what version are you
          > > using?
          > >
          > > Marcus Heege
          > >
          > > "Tim" <Tim@discussion s.microsoft.com > wrote in message
          > > news:22C964C9-5840-4C5F-82BE-7E24392BAADD@mi crosoft.com...[color=darkred]
          > >> I'm trying to co-erce a __gc array of Byte to a __nogc pointer to char to
          > >> pass to a native function call in a bit of managed c++ code like this:
          > >>
          > >> Byte field __gc[] = dynamic_cast<By te __gc[]>(record->get_Item(i)) ;
          > >> .
          > >> .
          > >> .
          > >> Byte __pin * p = &field[0]; // << this is the proscribed way to pin a
          > >> gc
          > >> array
          > >> char __nogc * pinfield = p; // << C2440 error here!
          > >>
          > >> I get the following compilation error:
          > >> : error C2440: 'initializing' : cannot convert from 'unsigned char __pin
          > >> *volatile ' to 'char *'
          > >>
          > >> Given that p is effectively a no-gc pointer to unsigned char, I can't
          > >> understand why I can't assign it to pinfield - a no-gc pointer to char
          > >> (i.e.
          > >> exactly the same, just treat the chars as signed instead of unsigned).
          > >> Can anyone explain why I can't do this and how I can achieve what I'm
          > >> trying
          > >> to please?
          > >> cheers
          > >> Tim
          > >>[/color]
          > >
          > >[/color]
          >
          >[/color]

          Comment

          • Tim

            #6
            Re: managed c++ pinned pointer cast

            Hi Tim
            Have you tried reinterpret_cas t<>?
            Tim

            "Tim" wrote:
            [color=blue]
            > I've tried explicit casting and it still complains. It seems quite certain
            > that there is some reason why I can't do it.
            >
            > Tim
            >
            > "Norman Diamond" wrote:
            >[color=green]
            > > In reply to Tim, plain char is a different type from unsigned char. Pointer
            > > to plain char and pointer to unsigned char do not automatically convert to
            > > each other. You need a cast. The management disclaims all responsibility
            > > for this one.
            > >
            > > Concerning Marcus Heege's report, if one line is commented out then the
            > > warning seems to have its "from" and "to" backwards. The cast is converting
            > > an rvalue FROM a pointer to pinned type TO a pointer to managed type. Is
            > > this a bug in a Dutch language version of Visual Studio, a bug in Mr.
            > > Heege's translation, or a bug in an English language version of Visual
            > > Studio?
            > >
            > > "Marcus Heege" <NOSPAM@heege.n et> wrote in message
            > > news:uBQtr8LxFH A.916@TK2MSFTNG P10.phx.gbl...[color=darkred]
            > > >I can't reproduce your problem. The following code compiles:
            > > >
            > > > <code>
            > > > #using <mscorlib.dll >
            > > >
            > > > using namespace System;
            > > >
            > > > int main()
            > > > {
            > > > Byte bytes __gc [] = __gc new Byte __gc [100];
            > > > Byte __pin* ppBytes = &bytes[0];
            > > > Byte* pBytes1 = ppBytes;
            > > > Byte* pBytes2 = static_cast<Byt e*>(ppBytes);
            > > > //Byte* pBytes3 = (Byte*)ppBytes;
            > > > }
            > > > </code>
            > > >
            > > > If I uncomment the last line, I get a warning:
            > > >
            > > > test.cpp(11) : warning C4303: C-style cast from 'unsigned char __gc *' to
            > > > 'unsigned char __pin *volatile ' is deprecated, use static_cast, _try_cast
            > > > or dynamic_cast
            > > >
            > > > Even if I modify the __pin* to __pin* volatile, it seems to be the same.
            > > >
            > > > My tests are done with VC 2003 (CL.EXE V 13.10.3077) what version are you
            > > > using?
            > > >
            > > > Marcus Heege
            > > >
            > > > "Tim" <Tim@discussion s.microsoft.com > wrote in message
            > > > news:22C964C9-5840-4C5F-82BE-7E24392BAADD@mi crosoft.com...
            > > >> I'm trying to co-erce a __gc array of Byte to a __nogc pointer to char to
            > > >> pass to a native function call in a bit of managed c++ code like this:
            > > >>
            > > >> Byte field __gc[] = dynamic_cast<By te __gc[]>(record->get_Item(i)) ;
            > > >> .
            > > >> .
            > > >> .
            > > >> Byte __pin * p = &field[0]; // << this is the proscribed way to pin a
            > > >> gc
            > > >> array
            > > >> char __nogc * pinfield = p; // << C2440 error here!
            > > >>
            > > >> I get the following compilation error:
            > > >> : error C2440: 'initializing' : cannot convert from 'unsigned char __pin
            > > >> *volatile ' to 'char *'
            > > >>
            > > >> Given that p is effectively a no-gc pointer to unsigned char, I can't
            > > >> understand why I can't assign it to pinfield - a no-gc pointer to char
            > > >> (i.e.
            > > >> exactly the same, just treat the chars as signed instead of unsigned).
            > > >> Can anyone explain why I can't do this and how I can achieve what I'm
            > > >> trying
            > > >> to please?
            > > >> cheers
            > > >> Tim
            > > >>
            > > >
            > > >[/color]
            > >
            > >[/color][/color]

            Comment

            • Tim

              #7
              Re: managed c++ pinned pointer cast

              Thanks Tim, you're a life saver. You should be an MVP.
              Tim

              "Tim" wrote:
              [color=blue]
              > Hi Tim
              > Have you tried reinterpret_cas t<>?
              > Tim
              >
              > "Tim" wrote:
              >[color=green]
              > > I've tried explicit casting and it still complains. It seems quite certain
              > > that there is some reason why I can't do it.
              > >
              > > Tim
              > >
              > > "Norman Diamond" wrote:
              > >[color=darkred]
              > > > In reply to Tim, plain char is a different type from unsigned char. Pointer
              > > > to plain char and pointer to unsigned char do not automatically convert to
              > > > each other. You need a cast. The management disclaims all responsibility
              > > > for this one.
              > > >
              > > > Concerning Marcus Heege's report, if one line is commented out then the
              > > > warning seems to have its "from" and "to" backwards. The cast is converting
              > > > an rvalue FROM a pointer to pinned type TO a pointer to managed type. Is
              > > > this a bug in a Dutch language version of Visual Studio, a bug in Mr.
              > > > Heege's translation, or a bug in an English language version of Visual
              > > > Studio?
              > > >
              > > > "Marcus Heege" <NOSPAM@heege.n et> wrote in message
              > > > news:uBQtr8LxFH A.916@TK2MSFTNG P10.phx.gbl...
              > > > >I can't reproduce your problem. The following code compiles:
              > > > >
              > > > > <code>
              > > > > #using <mscorlib.dll >
              > > > >
              > > > > using namespace System;
              > > > >
              > > > > int main()
              > > > > {
              > > > > Byte bytes __gc [] = __gc new Byte __gc [100];
              > > > > Byte __pin* ppBytes = &bytes[0];
              > > > > Byte* pBytes1 = ppBytes;
              > > > > Byte* pBytes2 = static_cast<Byt e*>(ppBytes);
              > > > > //Byte* pBytes3 = (Byte*)ppBytes;
              > > > > }
              > > > > </code>
              > > > >
              > > > > If I uncomment the last line, I get a warning:
              > > > >
              > > > > test.cpp(11) : warning C4303: C-style cast from 'unsigned char __gc *' to
              > > > > 'unsigned char __pin *volatile ' is deprecated, use static_cast, _try_cast
              > > > > or dynamic_cast
              > > > >
              > > > > Even if I modify the __pin* to __pin* volatile, it seems to be the same.
              > > > >
              > > > > My tests are done with VC 2003 (CL.EXE V 13.10.3077) what version are you
              > > > > using?
              > > > >
              > > > > Marcus Heege
              > > > >
              > > > > "Tim" <Tim@discussion s.microsoft.com > wrote in message
              > > > > news:22C964C9-5840-4C5F-82BE-7E24392BAADD@mi crosoft.com...
              > > > >> I'm trying to co-erce a __gc array of Byte to a __nogc pointer to char to
              > > > >> pass to a native function call in a bit of managed c++ code like this:
              > > > >>
              > > > >> Byte field __gc[] = dynamic_cast<By te __gc[]>(record->get_Item(i)) ;
              > > > >> .
              > > > >> .
              > > > >> .
              > > > >> Byte __pin * p = &field[0]; // << this is the proscribed way to pin a
              > > > >> gc
              > > > >> array
              > > > >> char __nogc * pinfield = p; // << C2440 error here!
              > > > >>
              > > > >> I get the following compilation error:
              > > > >> : error C2440: 'initializing' : cannot convert from 'unsigned char __pin
              > > > >> *volatile ' to 'char *'
              > > > >>
              > > > >> Given that p is effectively a no-gc pointer to unsigned char, I can't
              > > > >> understand why I can't assign it to pinfield - a no-gc pointer to char
              > > > >> (i.e.
              > > > >> exactly the same, just treat the chars as signed instead of unsigned).
              > > > >> Can anyone explain why I can't do this and how I can achieve what I'm
              > > > >> trying
              > > > >> to please?
              > > > >> cheers
              > > > >> Tim
              > > > >>
              > > > >
              > > > >
              > > >
              > > >[/color][/color][/color]

              Comment

              Working...