Unsafe code: Converting "byte *" to "[] byte"

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

    Unsafe code: Converting "byte *" to "[] byte"

    I've converted "[] byte" to "byte *" at times, using 'unsafe' and
    fixed { .... }, but the reverse does not seem to work.

    In this case, a C++ DLL returns a byte * and a length. What is the best
    way to convert these to straight C#-compatible straight "[] byte" arrays?

    PS: The C++ DLL is actually managed and I have access to the source.
    Perhaps there is a simpler syntax for doing the conversion there, and
    returning a C# compatible array rather than a pointer?

  • Bob Powell [MVP]

    #2
    Re: Unsafe code: Converting "byte *" to "[] byte"

    You'll need to create a byte array and copy the contents of the pointer to
    it.

    The Marshal class will enable you to do this.

    byte[] bytes=new byte[length];
    for(int i=0; i<length; i++)
    bytes[i]=Marshal.ReadBy te(fromPointer, i);

    bytes now contains the copy of the memory pointed to by fromPointer.

    Marshal.Copy goes the other way, from the byte array to the pointer. Shame
    they couldn't have written a copyfrom method.

    --
    Bob Powell [MVP]
    Visual C#, System.Drawing

    Find great Windows Forms articles in Windows Forms Tips and Tricks


    Answer those GDI+ questions with the GDI+ FAQ


    All new articles provide code in C# and VB.NET.
    Subscribe to the RSS feeds provided and never miss a new article.





    "_BNC" <_BNC@nospam.or g> wrote in message
    news:ms7gr0dn7q 7gua23f4h3mbpqk kpopl7j3s@4ax.c om...[color=blue]
    > I've converted "[] byte" to "byte *" at times, using 'unsafe' and
    > fixed { .... }, but the reverse does not seem to work.
    >
    > In this case, a C++ DLL returns a byte * and a length. What is the best
    > way to convert these to straight C#-compatible straight "[] byte" arrays?
    >
    > PS: The C++ DLL is actually managed and I have access to the source.
    > Perhaps there is a simpler syntax for doing the conversion there, and
    > returning a C# compatible array rather than a pointer?
    >[/color]


    Comment

    • Willy Denoyette [MVP]

      #3
      Re: Unsafe code: Converting &quot;byte *&quot; to &quot;[] byte&quot;


      "Bob Powell [MVP]" <bob@_spamkille r_bobpowell.net > wrote in message
      news:%23rPUwnd3 EHA.3624@TK2MSF TNGP14.phx.gbl. ..[color=blue]
      > You'll need to create a byte array and copy the contents of the pointer to
      > it.
      >
      > The Marshal class will enable you to do this.
      >
      > byte[] bytes=new byte[length];
      > for(int i=0; i<length; i++)
      > bytes[i]=Marshal.ReadBy te(fromPointer, i);
      >
      > bytes now contains the copy of the memory pointed to by fromPointer.
      >
      > Marshal.Copy goes the other way, from the byte array to the pointer. Shame
      > they couldn't have written a copyfrom method.
      >[/color]

      This has been taken care of in v2.0 using an overload of Marshal.Copy.

      Willy.


      Comment

      • _BNC

        #4
        Re: Unsafe code: Converting &quot;byte *&quot; to &quot;[] byte&quot;

        [re turning 'byte *' into C# '[] byte'

        On Thu, 9 Dec 2004 13:48:14 +0100, "Willy Denoyette [MVP]"
        <willy.denoyett e@pandora.be> wrote:
        [color=blue]
        >
        >"Bob Powell [MVP]" <bob@_spamkille r_bobpowell.net > wrote in message
        >news:%23rPUwnd 3EHA.3624@TK2MS FTNGP14.phx.gbl ...[color=green]
        >> You'll need to create a byte array and copy the contents of the pointer to
        >> it.
        >>
        >> The Marshal class will enable you to do this.
        >>
        >> byte[] bytes=new byte[length];
        >> for(int i=0; i<length; i++)
        >> bytes[i]=Marshal.ReadBy te(fromPointer, i);
        >>
        >> bytes now contains the copy of the memory pointed to by fromPointer.
        >>
        >> Marshal.Copy goes the other way, from the byte array to the pointer. Shame
        >> they couldn't have written a copyfrom method.
        >>[/color]
        >
        >This has been taken care of in v2.0 using an overload of Marshal.Copy.
        >
        >Willy.[/color]

        The horror. Runtime is bad enough already. Is there no way to do this
        without copying bytes? I'm going through both an unmanaged C++ layer and
        then a managed C++ layer before it gets to C#. Is there a simple way to
        cast this somewhere in the C++ layers?

        I'm also not sure why I'd have to use 'Marshall' to copy the array. If
        I've already got a pointer and length, I could just do the copy directly,
        right? OK, I'm probably being dense.


        Comment

        • Willy Denoyette [MVP]

          #5
          Re: Unsafe code: Converting &quot;byte *&quot; to &quot;[] byte&quot;


          "_BNC" <_BNC@nospam.or g> wrote in message
          news:hq2jr0dqql jj4f7m1duu4bv0i l98nv4991@4ax.c om...[color=blue]
          > [re turning 'byte *' into C# '[] byte'
          >
          > On Thu, 9 Dec 2004 13:48:14 +0100, "Willy Denoyette [MVP]"
          > <willy.denoyett e@pandora.be> wrote:
          >[color=green]
          >>
          >>"Bob Powell [MVP]" <bob@_spamkille r_bobpowell.net > wrote in message
          >>news:%23rPUwn d3EHA.3624@TK2M SFTNGP14.phx.gb l...[color=darkred]
          >>> You'll need to create a byte array and copy the contents of the pointer
          >>> to
          >>> it.
          >>>
          >>> The Marshal class will enable you to do this.
          >>>
          >>> byte[] bytes=new byte[length];
          >>> for(int i=0; i<length; i++)
          >>> bytes[i]=Marshal.ReadBy te(fromPointer, i);
          >>>
          >>> bytes now contains the copy of the memory pointed to by fromPointer.
          >>>
          >>> Marshal.Copy goes the other way, from the byte array to the pointer.
          >>> Shame
          >>> they couldn't have written a copyfrom method.
          >>>[/color]
          >>
          >>This has been taken care of in v2.0 using an overload of Marshal.Copy.
          >>
          >>Willy.[/color]
          >
          > The horror. Runtime is bad enough already. Is there no way to do this
          > without copying bytes? I'm going through both an unmanaged C++ layer and
          > then a managed C++ layer before it gets to C#. Is there a simple way to
          > cast this somewhere in the C++ layers?
          >
          > I'm also not sure why I'd have to use 'Marshall' to copy the array. If
          > I've already got a pointer and length, I could just do the copy directly,
          > right? OK, I'm probably being dense.
          >
          >[/color]

          There's no need to copy when passing managed types between C# and Managed
          C++. You only should copy from managed to unmanaged memory and the other way
          arround.

          Willy.


          Comment

          • Travis

            #6
            Re: Unsafe code: Converting &quot;byte *&quot; to &quot;[] byte&quot;

            I have a similar situation. C# application, unmanaged com object
            (directshow filter). Using a callback function I would like to get a copy of
            the data that filter has.

            I've been trying:

            //COM
            typedef HRESULT (* MYCALLBACK) ( IMediaSample *pSample, long lSize );
            HRESULT MyClass::Transf orm(IMediaSampl e *pMediaSample)
            {
            CheckPointer(pM ediaSample,E_PO INTER);
            if(m_callback)
            {
            m_callback(pDes tBuffer, lSourceSize);
            }
            return S_OK;
            }

            //C#
            public delegate void CallBackDelegat e(IntPtr pSample, int dataLength);
            public void CallbackFunctio n(IntPtr pSample, int dataLength)
            {
            byte [] buffer = new byte [dataLength];
            Marshal.Copy(pS ample,buffer,0, dataLength);
            }

            It crashes on the Marshal.Copy. Any suggestions on how to accomplish this?
            I have the source for both the managed and unmanaged sides so I can change
            both if necessay.


            "Willy Denoyette [MVP]" wrote:
            [color=blue]
            >
            > "_BNC" <_BNC@nospam.or g> wrote in message
            > news:hq2jr0dqql jj4f7m1duu4bv0i l98nv4991@4ax.c om...[color=green]
            > > [re turning 'byte *' into C# '[] byte'
            > >
            > > On Thu, 9 Dec 2004 13:48:14 +0100, "Willy Denoyette [MVP]"
            > > <willy.denoyett e@pandora.be> wrote:
            > >[color=darkred]
            > >>
            > >>"Bob Powell [MVP]" <bob@_spamkille r_bobpowell.net > wrote in message
            > >>news:%23rPUwn d3EHA.3624@TK2M SFTNGP14.phx.gb l...
            > >>> You'll need to create a byte array and copy the contents of the pointer
            > >>> to
            > >>> it.
            > >>>
            > >>> The Marshal class will enable you to do this.
            > >>>
            > >>> byte[] bytes=new byte[length];
            > >>> for(int i=0; i<length; i++)
            > >>> bytes[i]=Marshal.ReadBy te(fromPointer, i);
            > >>>
            > >>> bytes now contains the copy of the memory pointed to by fromPointer.
            > >>>
            > >>> Marshal.Copy goes the other way, from the byte array to the pointer.
            > >>> Shame
            > >>> they couldn't have written a copyfrom method.
            > >>>
            > >>
            > >>This has been taken care of in v2.0 using an overload of Marshal.Copy.
            > >>
            > >>Willy.[/color]
            > >
            > > The horror. Runtime is bad enough already. Is there no way to do this
            > > without copying bytes? I'm going through both an unmanaged C++ layer and
            > > then a managed C++ layer before it gets to C#. Is there a simple way to
            > > cast this somewhere in the C++ layers?
            > >
            > > I'm also not sure why I'd have to use 'Marshall' to copy the array. If
            > > I've already got a pointer and length, I could just do the copy directly,
            > > right? OK, I'm probably being dense.
            > >
            > >[/color]
            >
            > There's no need to copy when passing managed types between C# and Managed
            > C++. You only should copy from managed to unmanaged memory and the other way
            > arround.
            >
            > Willy.
            >
            >
            >[/color]

            Comment

            Working...