Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / September 2004

Tip: Looking for answers? Try searching our database.

how to convert c#-bytearray to c++-char* ???

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ekim - 08 Sep 2004 20:47 GMT
hy everybody,

I'm accessing a function of a managed-c++-dll within my c#-application.
Therefore I encountered following troubles:
In my c#-app I have got a byte[]-array, which should somehow be mapped to a
char* in my managed-c++-function.
Here is the prototype:
---------------------- managed c++ class library
project -------------------------------
void MyFunction(char* buffer)
{
   buffer = "Hello from managed c++!";
}
----------------------------------------------------------------------------
------------

in c# I'm using this function like this:
-----------------------
c#-application -------------------------------------------------
[DllImport("c++ClassLib.dll")]                        // import the c++ dll
private extern static void MyFunction(ref byte[] byteStream);

within any function, for example button1-Click()
{
   byte[] byteStream = new byte[4000];        // create new byte stream
   MyFunction(ref byteStream);                    // call c++ function
   MessageBox.Show(byteStream.toString());        // test-output the data
}
----------------------------------------------------------------------------
------------

The above code works (means throws no exception or something like this), but
has not the desired effect (that is, a displayed messagebox that says "Hello
from managed c++!"). So what I want to do in the c++-dll is to write
something into the byteStream that is provided by the c#-application).

Under no circumstances I can change the byte-stream to any other datatype
(because it is provided to me by another function), nor can I change the
char*-buffer in my c++-dll, because it is used by another external function,
too.
On the other hand, I could use some "intermediate" data types like an Array
if this would make things work (that is first converting the byteStream to
the Array, call the function, and within MyFunction convert the Array back
to char*).

I appreciate your help sincerely,

ekim!
Mattias Sj?gren - 10 Sep 2004 20:53 GMT
>In my c#-app I have got a byte[]-array, which should somehow be mapped to a
>char* in my managed-c++-function.

If you're really using managed C++, why don't you wrap this in a class
and reference the assembly from C# instead of going through P/Invoke?

>void MyFunction(char* buffer)
>{
>    buffer = "Hello from managed c++!";
>}

This is briken. All your code is doing is reassigning the local copy
of the buffer pointer. To write into the existing buffer you should
use a function like memcpy or strcpy.

And to prevent a buffer overrun, you should pass in the buffer size as
an additional parameter.

>[DllImport("c++ClassLib.dll")]                        // import the c++ dll
>private extern static void MyFunction(ref byte[] byteStream);

The parameter shouldn't be declared "ref".

>On the other hand, I could use some "intermediate" data types like an Array
>if this would make things work (that is first converting the byteStream to
>the Array, call the function, and within MyFunction convert the Array back
>to char*).

You already have an array of bytes. I'm not sure why you've chosen to
name the variable byteStream, because it isn't a stream (at least not
in .NET System.IO terminology).

Mattias

Signature

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


Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.