Home All Groups Group Topic Archive Search About

how to pass a byte array by reference

Author
27 Nov 2006 12:39 PM
sreelakshmi.rajula
Hi ,
  I am new to Managed c++ and C# .
  I've to use unmanaged code in C# . For that I've written a managed
wrapper. I am passing a byte array to a function and updating that byte
array. And I've to use that in my C# code.
And the link
http://groups.google.co.in/group/microsoft.public.dotnet.languages.vc/browse_thread/thread/f2af8dac6c3bd57a/6a697b4b060b8f93?lnk=st&q=C2234+%2B+managed+c%2B%2B&rnum=3&hl=en#6a697b4b060b8f93
says that
"you dont need to declare it as a reference, since in Managed C++ you
pass
everything by reference by default."

So I've tried like  foo(Byte byteBuffer __gc[])  in my MC++ code.

But when I get the value in my C# code it has not been updated.

Can any one know how to pass a byte array by reference ..

Thanks in advance....
Regards,
RSL.

Author
27 Nov 2006 7:54 PM
Mattias Sjögren
>So I've tried like  foo(Byte byteBuffer __gc[])  in my MC++ code.

That should work as long as you only want to change the elements of
the existing array. If you want to return a new array, you need to
pass it by reference.


>But when I get the value in my C# code it has not been updated.

Can you post more of your code? Perhaps the problem is elsewhere.


Mattias

--
Mattias Sjögren [C# MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Author
1 Dec 2006 5:41 AM
sreelakshmi.rajula
Mattias Sjögren wrote:
Show quote
> >So I've tried like  foo(Byte byteBuffer __gc[])  in my MC++ code.
>
> That should work as long as you only want to change the elements of
> the existing array. If you want to return a new array, you need to
> pass it by reference.
>
>
> >But when I get the value in my C# code it has not been updated.
>
> Can you post more of your code? Perhaps the problem is elsewhere.
>
>
> Mattias
>
> --
> Mattias Sjögren [C# MVP]  mattias @ mvps.org
> http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
> Please reply only to the newsgroup.

Hi Mattias,
   I could not reply to your mail as I was on vacation.
void foo(Byte byteBuffer __gc[])
{

         unsigned long ulBufferSize = 0 ;
         Encoding *encoder = Encoding::Unicode ;
         String* strtemp ;
         PutBLOB();
         strtemp = encoder->GetString(byteBuffer);
         m_Blob->WriteToBuffer(static_cast<unsigned
char*>(Marshal::StringToHGlobalAnsi  (strtemp).ToPointer()));
         byteBuffer = encoder->GetBytes(strtemp->ToCharArray());
}

Is there something wrong?If so.. please let me know. And if any one has
solution for this please suggest..Thanks in advance.

Regards,
RSL

AddThis Social Bookmark Button