Groups | Blog | Home
all groups > dotnet framework > november 2006 >

dotnet framework : how to pass a byte array by reference


sreelakshmi.rajula NO[at]SPAM gmail.com
11/27/2006 4:39:38 AM
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.
Mattias Sjögren
11/27/2006 8:54:23 PM

[quoted text, click to view]

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.


[quoted text, click to view]

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
sreelakshmi.rajula NO[at]SPAM gmail.com
11/30/2006 9:41:32 PM

[quoted text, click to view]

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

unsigned long ulBufferSize =3D 0 ;
Encoding *encoder =3D Encoding::Unicode ;
String* strtemp ;
PutBLOB();
strtemp =3D encoder->GetString(byteBuffer);
m_Blob->WriteToBuffer(static_cast<unsigned
char*>(Marshal::StringToHGlobalAnsi (strtemp).ToPointer()));
byteBuffer =3D 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